はい、API に一貫性がありません。ラスター (データ ソース) には、GetProjection()
代わりに (WKT を返す) メソッドがあります。
ここにあなたが望むことをする関数があります(ここから引き出されます):
def extract_point_from_raster(point, data_source, band_number=1):
"""Return floating-point value that corresponds to given point."""
# Convert point co-ordinates so that they are in same projection as raster
point_sr = point.GetSpatialReference()
raster_sr = osr.SpatialReference()
raster_sr.ImportFromWkt(data_source.GetProjection())
transform = osr.CoordinateTransformation(point_sr, raster_sr)
point.Transform(transform)
# Convert geographic co-ordinates to pixel co-ordinates
x, y = point.GetX(), point.GetY()
forward_transform = Affine.from_gdal(*data_source.GetGeoTransform())
reverse_transform = ~forward_transform
px, py = reverse_transform * (x, y)
px, py = int(px + 0.5), int(py + 0.5)
# Extract pixel value
band = data_source.GetRasterBand(band_number)
structval = band.ReadRaster(px, py, 1, 1, buf_type=gdal.GDT_Float32)
result = struct.unpack('f', structval)[0]
if result == band.GetNoDataValue():
result = float('nan')
return result
そのドキュメントは次のとおりです(hereから取得):
spatial.extract_point_from_raster(point, data_source, band_number=1)
data_source は GDAL ラスターであり、point は OGR ポイント オブジェクトです。この関数は、ポイントに最も近い data_source の指定されたバンドのピクセルの値を返します。
point と data_source は同じ参照系にある必要はありませんが、両方に適切な空間参照が定義されている必要があります。
ポイントがラスターに収まらない場合、RuntimeError が発生します。