1

I have the NumPy array and its bounding box coordinates. I have tried to convert it into raster using rasterio, based on this answer, and it did save it as raster, but when I use rasterio.show the coordinates are very wrong.

This is the script I have used:

bbox_coords_wgs84=[-101.7359960059834, 20.21904081937658, -100.5717967351885, 20.8312118894487]

#variables for the projection:
minx=bbox_coords_wgs84[0]
maxy=bbox_coords_wgs84[3]
pixel_size= 10

#according to the post on GIS SO:

import rasterio
from rasterio.transform import from_origin

transform=from_origin(minx,maxy,pixel_size,pixel_size)
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=crs_img,
                    nodata=None, # change if data has nodata value
                    transform=transform) as dst:
        dst.write(ndvi, 1)
 
#display the results:

from matplotlib import pyplot
from rasterio.plot import show

src = rasterio.open('test1.tif')
show(src)

enter image description here

As you can see, the numbers are absolutely not the correct coordinates.

My end goal: to be able to reproject the NumPy array into WGS84 correctly.

*This post relates also to this post

3
  • You could use gdal translate if you want to transform the raster image or pyproj to transform the coordinates in the numpy array Commented Jan 3, 2021 at 14:03
  • 1
    You do not report that this is a suite of Reproject a NumPy array with affine transform where you use rasterio.transform.from_bounds and not rasterio.transform.from_origin
    – gene
    Commented Jan 3, 2021 at 14:11
  • @gene I have edited now with reference
    – ReutKeller
    Commented Jan 3, 2021 at 14:14

1 Answer 1

2

You do not report that this is a suite of Reproject a NumPy array with affine transform where you use rasterio.transform.from_bounds

From rasterio.transform module

rasterio.transform.from_bounds(west, south, east, north, width, height)
Return an Affine transformation given bounds, width and height.
Return an Affine transformation for a georeferenced raster given its bounds west, south, east, north and its width and height in number of pixels.

And

rasterio.transform.from_origin(west, north, xsize, ysize)
Return an Affine transformation given upper left and pixel sizes.
Return an Affine transformation for a georeferenced raster given the coordinates of its upper left corner west, north and pixel sizes xsize, ysize.

It is not the same thing and the results are different

rasterio.transform.from_bounds( -101.7359960059834,20.21904081937658,-100.5717967351885,20.8312118894487,1103,2039)
Affine(0.0010554843796871222, 0.0, -101.7359960059834,
   0.0, -0.0003002310299519955, 20.8312118894487)

rasterio.transform.from_origin(-101.7359960059834,20.8312118894487,10,10)
Affine(10.0, 0.0, -101.7359960059834,
   0.0, -10.0, 20.8312118894487)

New

The four corners of the raster from the bound (width = 1103, height= 2039)

fig,ax = plt.subplots()
ax.plot(0,0,'ro')
ax.plot(1103,0,'bo')
ax.plot(0,2039,'go')
ax.plot(1103,2039,'co')
plt.show()

enter image description here

The transformation

 trans = rasterio.transform.from_bounds(-101.7359960059834,20.21904081937658-100.5717967351885,20.8312118894487,1103,2039)

 
trans*(0,0)
(-101.7359960059834, 20.8312118894487)
trans*(1103,0) 
(-100.5717967351885, 20.8312118894487)
trans*(0,2039) 
(-101.7359960059834, 20.21904081937658)
trans*(1103,2039)
(-100.5717967351885, 20.21904081937658)

fig,ax = plt.subplots()
ax.plot(*(trans*(0,0)),'ro')
ax.plot(*(trans*(1103,0)),'bo')
ax.plot(*(trans*(0,2039)),'go')
ax.plot(*(trans*(1103,2039)),'co')
plt.show()

enter image description here

3
  • sorry , I feel now more confused. How can I georeference my numpy array correctly? which one should I use?
    – ReutKeller
    Commented Jan 3, 2021 at 14:51
  • rasterio.transform.from_bounds if you use bounds
    – gene
    Commented Jan 3, 2021 at 14:54
  • look above in New
    – gene
    Commented Jan 3, 2021 at 16:23

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.