[英]Find lat/long coordinates from pixel point in .geotiff using python and gdal
[英]Find pixel coordinates from lat/long point in .geotiff using python and gdal
我有描述 a.geotiff 圖像中一個點的 position 的(緯度,經度)坐標。
我希望找到圖像內經緯度的等效像素坐標。
我通過以下指令從命令行使用 gdaltransform 成功:
gdaltransform -i -t_srs epsg:4326 /path/imagename.tiff
-17.4380493164062 14.6951949085676
但我想從 python 代碼中檢索這種類型的等價物。 我嘗試了以下方法:
from osgeo import osr
source = osr.SpatialReference()
source.ImportFromUrl(path + TIFFFilename)
target = osr.SpatialReference()
target.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(target,source )
point_xy = np.array(transform.TransformPoint(-17.4380493164062,14.6951949085676))
但它返回此錯誤:
NotImplementedError: Wrong number or type of arguments for overloaded function 'CoordinateTransformation_TransformPoint'.
Possible C/C++ prototypes are:
OSRCoordinateTransformationShadow::TransformPoint(double [3])
OSRCoordinateTransformationShadow::TransformPoint(double [3],double,double,double)
我究竟做錯了什么? 我試圖解決這個錯誤,但沒有任何成功。 還有其他方法嗎?
編輯1:
我通過終端中的 gdaltransform 命令實現了一次轉換:
gdaltransform -i -t_srs epsg:4326 /path/image.tiff
-17.4380493164062 14.6951949085676
由於我需要以 pythonic 方式檢索像素,因此我嘗試使用子進程調用命令,例如:
# TRY 1:
subprocess.run(['gdaltransform','-i',' -t_srs','epsg:4326','/pat/img.tiff\n'], stdout=subprocess.PIPE)
# TRY 2 :
cmd = '''gdaltransform -i -t_srs epsg:4326 /home/henri/Work/imdex_visio/AllInt/Dakar_X118374-118393_Y120252-120271_PHR1A_2016-03-10T11_45_39.781Z_Z18_3857.tiff
-17.4380493164062 14.6951949085676'''
subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)
但它不起作用。 也許是因為命令本身的行為方式,比如實際上並沒有返回結果並結束自己,而是顯示結果並保持忙碌。
根據食譜,您正在翻轉使用變換和點。 您應該在給定變換的點上調用變換,而不是相反。 似乎您正在翻轉源和目標,但是您這樣做了兩次,所以它會起作用。
但是我相信target.ImportFromUrl(path + TIFFFilename)
將不起作用。 相反,您可以使用 gdal 從 geotiff 中提取空間參考。
像下面這樣的東西應該工作
from osgeo import osr, ogr, gdal
# Extract target reference from the tiff file
ds = gdal.Open(path + TIFFFilename)
target = osr.SpatialReference(wkt=ds.GetProjection())
source = osr.SpatialReference()
source.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(source, target)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(-17.4380493164062, 14.6951949085676)
point.Transform(transform)
print(point.GetX(), point.GetY())
這為您提供了 geotiffs 參考中的坐標,但這不是像素坐標。
要將點轉換為像素,您可以使用以下內容(根據您在世界上的位置,可能必須翻轉線的減號)
def world_to_pixel(geo_matrix, x, y):
"""
Uses a gdal geomatrix (gdal.GetGeoTransform()) to calculate
the pixel location of a geospatial coordinate
"""
ul_x= geo_matrix[0]
ul_y = geo_matrix[3]
x_dist = geo_matrix[1]
y_dist = geo_matrix[5]
pixel = int((x - ul_x) / x_dist)
line = -int((ul_y - y) / y_dist)
return pixel, line
所以你的最終代碼看起來像
from osgeo import osr, ogr, gdal
def world_to_pixel(geo_matrix, x, y):
"""
Uses a gdal geomatrix (gdal.GetGeoTransform()) to calculate
the pixel location of a geospatial coordinate
"""
ul_x= geo_matrix[0]
ul_y = geo_matrix[3]
x_dist = geo_matrix[1]
y_dist = geo_matrix[5]
pixel = int((x - ul_x) / x_dist)
line = -int((ul_y - y) / y_dist)
return pixel, line
# Extract target reference from the tiff file
ds = gdal.Open(path + TIFFFilename)
target = osr.SpatialReference(wkt=ds.GetProjection())
source = osr.SpatialReference()
source.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(source, target)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(-17.4380493164062, 14.6951949085676)
point.Transform(transform)
x, y = world_to_pixel(ds.GetGeoTransform(), point.GetX(), point.GetY())
print(x, y)
建議的解決方案可能在大多數情況下都有效,因為行/列旋轉通常為零,但至少應該檢查或更好地包括:
import numpy as np
from osgeo import gdal
def world_to_pxl(gt, x, y):
# 'Affine transformation': W = A * pxl + ul
# world[2, 1] = a[2, 2] * pxl[2, 1] + upper_left[2,1]
world = np.array([[x], [y]]) # world coordinates
upper_left = np.array(
[
[gt[0]], [gt[3]] # upper left corner of image
]
)
a = np.array([[gt[1], gt[2]],
[gt[4], gt[5]]])
# Reformulate: A^-1 * (W - ul) = pxl
pxl = np.matmul(np.linalg.inv(a), (world - upper_left))
row = pxl[0] # x_pixel
col = pxl[1] # y_line
return row, col
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.