Skip to main content
Notice removed Draw attention by ReutKeller
Bounty Ended with AMA's answer chosen by ReutKeller
Tweeted twitter.com/StackGIS/status/1414192766981181449
Notice added Draw attention by ReutKeller
Bounty Started worth 100 reputation by ReutKeller
added 307 characters in body
Source Link
ReutKeller
  • 2.2k
  • 4
  • 37
  • 91

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|

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|
deleted 4 characters in body; edited title
Source Link
Taras
  • 34.1k
  • 4
  • 73
  • 148

Reproject numpyNumPy ndarray without saving as tiffTIFF

I have numpyNumPy 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 I want to give this geographical data to the nd-arrayndarray I have. However, I keep getting as a result blank image.

#details regard my data:
#I have dictionrydictionary ("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 geoemtrygeometry

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?

Reproject numpy ndarray without saving as tiff

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 nd-array I have. However, I keep getting as a result blank image.

#details regard my data:
#I have dictionry ("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 geoemtry

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?

Reproject NumPy ndarray without saving as TIFF

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?

Source Link
ReutKeller
  • 2.2k
  • 4
  • 37
  • 91

Reproject numpy ndarray without saving as tiff

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 nd-array I have. However, I keep getting as a result blank image.

#details regard my data:
#I have dictionry ("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 geoemtry

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?