I’m trying to understand better how the projection works with the images I download.
When I use rasterio image it keeps the coordinates system data per pixel so I can plot my shapefile on top of it.
When I use the plot_image function I don’t see the coordinates, and any activity I do later with the image is as numpy and then I lose to CRS.
I s there any way to save the CRS data for the image so I can reprojec tthe image after working on it as numpy array?
If you download an image from Sentinel Hub service in tiff format the image will be georeferenced. If you download using sentinelhub-py you have an option to save data as shown in this example. This will save the original georeferenced tiff image.
In memory, sentinelhub-py decodes an image into a normal numpy array without geoinformation. But you also have an instance of sentinelhub.BBox object which was used to request an image from Sentinel Hub service and it defines coordinates and CRS of a downloaded image. Therefore you can collect geo information from it and pass it to rasterio functions. For that the following methods should be useful:
Thank you for your answer, it helped a lot.
so now , i’m trying the BBox.crs.pyproj_crs but not sure how to use it as when I run it I get:
bbox.crs.pyproj_crs
>>>
<bound method CRS.pyproj_crs of CRS('4326')>
and ofcurse when I try to save it as raster with rasterio it doesn’t understand it:
import rasterio
from rasterio.transform import from_origin
transform=from_bounds(minx,maxy,pixel_size,pixel_size,ndvi.shape[0],ndvi.shape[1])
#crs_img='EPSG:4326'
with rasterio.open('test1.tif',
'w',
driver='GTiff',
height=ndvi.shape[0],
width=ndvi.shape[1],
count=1,
dtype=ndvi.dtype,
crs=crss,
nodata=None, # change if data has nodata value
transform=transform) as dst:
dst.write(ndvi, 1)
>>>
CRSError: CRS is invalid: <bound method CRS.pyproj_crs of CRS('4326')>
Thank you.
and just to make sure I understood correct,
in bbox.get_transform_vector() , How do I know what is the resx and resy? is it based on the image resolution? e.g if I use sentinel2 it will be 10,10 , or landsat will be 30,30…
I’m asking because when I do it as you mentioned I get weird image:
original image:
img_crs=bbox.crs.pyproj_crs()
transform=bbox.get_transform_vector(10,10)
transform=rasterio.Affine(-101.7359960059834, 10.0, 0, 20.8312118894487, 0, -10.0) #this equal to the tuple from get_transform_vector
with rasterio.open('test1.tif',
'w',
driver='GTiff',
height=ndvi.shape[0],
width=ndvi.shape[1],
count=1,
dtype=ndvi.dtype,
crs=img_crs,
nodata=None, # change if data has nodata value
transform=transform) as dst:
dst.write(ndvi, 1)
when I open this image I get this:
so i’m not sure if the problem is resx resy or osmething else.