Python GDAL 坐标转换报错:TypeError: Wrong number or type of arguments for overloaded function 'CoordinateTransformation_TransformPoint' 的解决方法
Python GDAL 坐标转换报错:TypeError: Wrong number or type of arguments for overloaded function 'CoordinateTransformation_TransformPoint' 的解决方法
在使用 Python GDAL 进行坐标转换时,可能会遇到以下错误:
from osgeo import osr
# ... 省略代码 ...
# 将左上角和右下角点的坐标从原始坐标系转换为目标坐标系
ul_lon, ul_lat, _ = transform.TransformPoint(ulx, uly, 0)
lr_lon, lr_lat, _ = transform.TransformPoint(lrx, lry, 0)
# ... 省略代码 ...
错误信息:
TypeError: Wrong number or type of arguments for overloaded function 'CoordinateTransformation_TransformPoint'. Possible C/C++ prototypes are:
OSRCoordinateTransformationShadow::TransformPoint(double [3])
OSRCoordinateTransformationShadow::TransformPoint(double [4])
OSRCoordinateTransformationShadow::TransformPoint(double [3],double,double,double)
OSRCoordinateTransformationShadow::TransformPoint(double [4],double,double,double,double)
错误原因:
该错误是由于 osr.CoordinateTransformation.TransformPoint 函数的参数类型错误导致的。该函数期望接收一个长度为 3 或 4 的数组(double [3] 或 double [4]),但代码中传入的是三个单独的浮点数(ulx, uly, 0)。
解决方法:
将代码中的 transform.TransformPoint(ulx, uly, 0) 和 transform.TransformPoint(lrx, lry, 0) 修改为 transform.TransformPoint((ulx, uly, 0)) 和 transform.TransformPoint((lrx, lry, 0)),即将坐标点用元组括起来作为函数的参数传入。
修改后的代码:
from osgeo import osr
# ... 省略代码 ...
# 将左上角和右下角点的坐标从原始坐标系转换为目标坐标系
ul_lon, ul_lat, _ = transform.TransformPoint((ulx, uly, 0))
lr_lon, lr_lat, _ = transform.TransformPoint((lrx, lry, 0))
# ... 省略代码 ...
通过将坐标点用元组括起来,我们可以将三个浮点数打包成一个长度为 3 的数组,满足函数的参数要求,从而解决错误。
总结:
在使用 osr.CoordinateTransformation.TransformPoint 函数进行坐标转换时,需要注意参数类型。该函数期望接收一个长度为 3 或 4 的数组,而不是三个单独的浮点数。将坐标点用元组括起来传入函数可以解决此错误。
原文地址: https://www.cveoy.top/t/topic/mTiI 著作权归作者所有。请勿转载和采集!