I have GeoDataFrame that contains multiple lat/long coordinates, these coordinates belong to a raster image and I'm trying to get the pixel (x, y) values for each lat/long. I have tried multiple approaches the last one was the following:
import affine
import numpy as np
def retrieve_pixel_value(geo_coord, data_source):
"""Return floating-point value that corresponds to given point."""
x, y = geo_coord[0], geo_coord[1]
forward_transform =
affine.Affine.from_gdal(*data_source.GetGeoTransform())
#print(forward_transform)
reverse_transform = ~forward_transform
#print(reverse_transform)
px, py = reverse_transform * (x, y)
print(px, py)
px, py = int(px + 0.5), int(py + 0.5)
print(px, py)
pixel_coord = px, py
data_array = np.array(data_source.GetRasterBand(1).ReadAsArray())
return data_array[pixel_coord[0]][pixel_coord[1]]
but I'm getting index out of range, which makes sense since the values of pixel_coords[0]
and pixel_coord[1]
are (-1465570, 7463711)
I also tried another approach and had the same results
I have to say I have a basic understanding of working with GIS data, but I'm not quite comfortable with this concept, yet.
Why I'm getting Index out of range?
Also is there a way to implement with other libraries than GDAL?
question from:
https://stackoverflow.com/questions/65921784/lat-long-coordinates-to-pixel-returning-out-of-range-indices 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…