Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

python - lat/long coordinates to pixel returning out of range indices

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...