知道GeoTIFF图像中某一像素值的地理坐标(经度和纬度)。

问题描述 投票:1回答:1

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

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

我想知道数值为2.7的像素在该像素中的地理坐标(经度和纬度)。img.

预期的输出。

coordinates = (98.4567, 16.2888)
python gdal scikit-image python-xarray rasterio
1个回答
2
投票

你的问题是有标签 rasterio 因此,我将考虑你打开你的geotiff与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 属性(它包含了你的数据集的仿射变换矩阵,使用了 affine python包,允许将像素坐标映射到现实世界的坐标)这样。

coordinates = rio.transform.xy(
    dataset.transform,
    cell_coords[0],
    cell_coords[1],
    offset='center',
)
# (98.4567, 16.2888) in your example
© www.soinside.com 2019 - 2024. All rights reserved.