简体   繁体   English

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

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

Suppose I have a GeoTIFF image img with dimensions (1,3,3).假设我有一个尺寸为 (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]]])

I want to know the geographic coordinates(longitude and latitude) of the pixel whose value is 2.7 within the img .我想知道img中值为 2.7 的像素的地理坐标(经度和纬度)。

Expected output:预期 output:

coordinates = (98.4567, 16.2888)

Your question is having the tag rasterio so i will consider you opened your geotiff with rasterio in that vein:你的问题是标签rasterio所以我会考虑你用 rasterio 打开你的 geotiff :

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]])

You have to retrieve the indexes (row and column) that correspond to the value you are looking for:您必须检索与您要查找的值相对应的索引(行和列):

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

Then use the transform attribute of your dataset (it contains the affine transformation matrix, using affine python package, allowing to map pixel coordinates to real world coordinates) like this:然后使用数据集的transform属性(它包含仿射变换矩阵,使用affine python package,允许将 map 像素坐标转换为真实世界坐标),如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM