[英]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 的像素的地理坐標(經度和緯度)。
預期 output:
coordinates = (98.4567, 16.2888)
你的問題是標簽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]])
您必須檢索與您要查找的值相對應的索引(行和列):
cell_coords = np.where(img == 2.7)
# (array([1]), array([2]))
然后使用數據集的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.