0

I have a pair of latitude-longitude coordinates:

lat, lon = 59.87115844471202, 10.656472637769236

I have a URL for a TIFF (this - it's public).

I open it with rasterio (install: pip install rasterio), and find the pixels corresponding to my coordinates above:

from rasterio import transform
row, col = transform.rowcol(src.transform, lon, lat)
print(row, col) # prints 464 1576

(For more details on doing this with rasterio, see this.)

I would like to do this using NumPy. I tried:

np.array(src.transform).reshape(3,3) @ np.array([[lat*np.pi/180], # Returns: array([[10.00022706],
                                                 [lon*np.pi/180], #                 [60.00008722],
                                                 [1]])            #                 [ 1.        ]])

and also

np.array(src.transform).reshape(3,3) @ np.array([[lat], # Returns: array([[10.02473798],
                                                 [lon], #                 [59.99717876],
                                                 [1]])  #                 [ 1.        ]])

None of them is anywhere near close to 464 1576, returned by rasterio.transform.rowcol() above.

How can I use the matrix np.array(src.transform).reshape(3,3) to get row-col from lat-lon?


np.array(src.transform).reshape(3,3) looks like this:

array([[ 4.16666667e-04,  0.00000000e+00,  9.99979167e+00],
       [ 0.00000000e+00, -2.77777778e-04,  6.00001389e+01],
       [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])

More about the @ operator is here (it's basically a shorthand for np.matmul()).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.