3

I have NumPy ndarray with shape (1063,2116,12) (has 12 bands). The ndarray has non geographical data, but I do have its' transform affine and the crs. I want to give this geographical data to the ndarray I have. However, I keep getting as a result blank image.

#details regard my data:
#I have dictionary ("all_images") with two keys, each key has two values: the image as ndarray and the bbox of the image, which has geographical data, as shapely geometry

Here is my code,based on this example:


from rasterio.warp import reproject, Resampling
from rasterio import Affine

for i in np.arange(1,len(all_images)+1):
#this is the image:
    img_bbox=all_images[i][0]

#this is the shapely geoemtry bbox:
    bbox_shapely=all_images[i][1]

#get the coordinates in order to create transform using sentinelhub
    minx, miny, maxx, maxy = bbox_shapely.geometry.bounds
    bbox_coords_wgs84=[minx, miny, maxx, maxy]
    bbox = BBox(bbox=bbox_coords_wgs84, crs=CRS.WGS84)

#get tranform wih sentinelhub 
    transf = bbox.get_transform_vector(resx=10, resy=10)
    print(transf)

#get in gdal as rasterio requires
    transf= Affine.from_gdal(*transf)
    print(transf)

#create destination array for the reproject data
    destination = np.zeros(all_images[i][0].shape, np.uint8)

    reproject(
        img_bbox,
        destination,
        src_transform=transf,
        src_crs={'init': 'EPSG:4326'},
        dst_transform=transf,
        dst_crs={'init': 'EPSG:4326'},
        resampling=Resampling.nearest)
    assert destination.any()
    assert not destination.all()

However,when I show the result I get blank image,that seems to not be projected:

#the image:
plt.imshow(all_images[i][0][:,:,0])

enter image description here

plt.imshow(destination[:,:,0])

enter image description here

and if I use show it doesn't work at all:

show((destination, 2), cmap='viridis')
>>>AttributeError: 'numpy.ndarray' object has no attribute 'read'

I know I can also save it as tiff but in this case I prefer not to save it yet at this point as I want to do more processing on the images in the dictionary.

Where is my mistake?How can I reproject my ndarrays?

Edit: adding here my transform data:

transf = bbox.get_transform_vector(resx=10, resy=10)
print(transf)
>>>(-45.53101999856795, 10.0, 0, -10.3395264554344, 0, -10.0)

transf= Affine.from_gdal(*transf)
print(transf)
>>>
| 10.00, 0.00,-45.53|
| 0.00,-10.00,-10.34|
| 0.00, 0.00, 1.00|

1 Answer 1

1
+100

It seems that your code specifies the same CRS for src_crs and dst_crs. If you don't want to reproject to a different coordinate system, but just want to rasterio.open with the CRS information added, you can write to memory and open instead of writing to a file.

import rasterio
import rasterio.plot
from rasterio import MemoryFile
from rasterio.profiles import DefaultGTiffProfile

profile = DefaultGTiffProfile(res=res,
                              transform=transform,
                              dtype=array.dtype.name,
                              count=array.shape[0],
                              height=array.shape[1],
                              width=array.shape[2])
with MemoryFile() as memfile:
    with memfile.open(**profile) as dataset:
        dataset.write(array)
    with memfile.open() as dataset:
        rasterio.plot.show(dataset)
        # do something what you want to..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.