I am working with .tif image files containing geospatial data, focusing on the elevation of terrain. I am able to get the elevation of a point by using array indexes of dem1_band (as seen below), but I would want to get elevation based on latitude and longitude. Is there a way to do so with the rasterio library?
dem1 = rasterio.open('/content/DEMs/dem1.tif')
dem1_band = dem1.read(1).astype('float64')
print(dem1_band[3000,3000])
>>451.13458251953125
Yes. From https://gis.stackexchange.com/a/346458/55948:
import numpy as np
import rasterio
xs = np.array([130.5, 146.0])
ys = np.array([-25.5, -42.0])
with rasterio.open("my.tif") as src:
rows, cols = rasterio.transform.rowcol(src.transform, xs, ys)