了解 GeoTIFF 图像中特定像素值的地理坐标(经度和纬度)

Know the geographic coordinates (longitude and latitude) of a particular pixel value in a GeoTIFF image

假设我有一张尺寸为 (1,3,3) 的 GeoTIFF 图像 img

img = np.array([[[1.0, 2.3, 3.3],
                 [2.4, 2.6, 2.7],
                 [3.4, 4.2, 8.9]]])

我想知道img.

中值为2.7的像素的地理坐标(经纬度)

预期输出:

coordinates = (98.4567, 16.2888)

你的问题有标签 rasterio 所以我会认为你打开了你的 geotiff with rasterio :

import rasterio as rio
import numpy as np

dataset = rio.open('file.tif', 'r')
img = dataset.read(1)
# array([[1. , 2.3, 3.3],
#       [2.4, 2.6, 2.7],
#       [3.4, 4.2, 8.9]])

您必须检索与您要查找的值相对应的索引(行和列):

cell_coords = np.where(img == 2.7)
# (array([1]), array([2]))

然后使用 transform attribute of your dataset (it contains the affine transformation matrix, using affine python 包,允许将像素坐标映射到现实世界坐标)像这样:

coordinates = rio.transform.xy(
    dataset.transform,
    cell_coords[0],
    cell_coords[1],
    offset='center',
)
# (98.4567, 16.2888) in your example