[GIS] Get the correct transform from bounding box with Rasterio

affine transformationextentspythonrasteriosentinel-hub

I have image as numpy array with no projection or geographical data and I have information of the bouding box of this numpy array (the image suppose to match perfectly the bbox).
The bbox is sentinelhub.geometry.BBox type.

I have the following informtion about the bbox:

bbox_size
>>>(850, 1279) #it has 1279 rows and 850 columns, not to be confused

bbox_coords_wgs84
>>>[-56.413932589, -12.93287647, -56.335907465, -12.81704823]  #lower left, upper right

The problem is that whenever I'm trying to save my numpy array, I believe there is problem with the transform as I get it very disorted.
I'm using the following for the transform:

transform=rasterio.transform.from_bounds(*bbox, width= bbox_size[1], height= bbox_size[0])
transform
>>>Affine(6.1004788115715275e-05, 0.0, -56.413932589,
       0.0, -0.00013626851764705978, -12.81704823)

and the result look like this : (the red crosses are where there real lower left and upper right suppose to be:
enter image description here

Why is this happens? I believe there is something very basic that i'm missing here

Best Answer

You confused the height & width parameters and are passing them reversed. You use columns=height and rows=width when you should be using columns=width and rows=height. Change your code to:

transform=rasterio.transform.from_bounds(*bbox, width=bbox_size[0], height= bbox_size[1])
Related Question