Hi @reutkeller,
Here are a couple of suggestions:
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:
I hope this helps.
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.shapeh0],ndvi.shapeh1])
#crs_img='EPSG:4326'
with rasterio.open('test1.tif',
'w',
driver='GTiff',
height=ndvi.shapeh0],
width=ndvi.shapeh1],
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')>
The pyproj_crs
is a method, so you should use pyproj_crs()
.
Cheers,
Matej
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,1973 , 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,1973)
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.shapeh0],
width=ndvi.shapeh1],
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.
Thank you for the help
Reut
Be careful of the units. You data is in WGS84 (units are degrees), while you trying to use the resolution in metres.
I have tried to insert the degrees, as I understand, if I use sentinel1image now with 10 meters resolution it should look like this:
trans=bbox.get_transform_vector(0.00009009009009,0.00009009009009)
but still I get it wrong . I believe I miss understood you regard the meters and degrees?
To be honest, I’d be using eo-learn, where ExportToTiff functionality takes care of all these issues.
But if you have a quick look at that code, you will see that it uses width and height (as opposed to resx/resy):
crs = bbox.crs.pyproj_crs()
transform = rasterio.transform.from_bounds(*bbox, width=width, height=height)
Perhaps you could try that?
Hi,
I am not trying to push my own writing, but perhaps this blog helps you:
https://towardsdatascience.com/geo-referencing-a-figure-as-an-image-fc5e77caa00b
In the post, I try to explain how to handle different coordinate systems, and what the effect of the transformation is.
Kind regards,
Gijs