使用 Python 2.7.9 和 GDAL 1.11.1,使用 miniconda 进行包管理 --
执行此坐标点的简单重新投影会导致如下所述的错误。
我对 GDAL 比较陌生,所以我检查了代码是否来自 Python GDAL/OGR 1.0 Cookbook产生相同的问题,并且确实如此:
from osgeo import ogr
from osgeo import osr
source = osr.SpatialReference()
source.ImportFromEPSG(2927)
target = osr.SpatialReference()
target.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(source, target)
point = ogr.CreateGeometryFromWkt("POINT (1120351.57 741921.42)")
point.Transform(transform)
print point.ExportToWkt()
这是错误:
/opt/miniconda/envs/pygeo/lib/python2.7/site-packages/osgeo/ogr.pyc in Transform(self, *args)
4880 OGRERR_NONE on success or an error code.
4881 """
-> 4882 return _ogr.Geometry_Transform(self, *args)
4883
4884 def GetSpatialReference(self, *args):
TypeError: in method 'Geometry_Transform', argument 2 of type 'OSRCoordinateTransformationShadow *'
CoordinateTransform 是 C++ OSRCoordinateTransformationShadow 类的代理,由 SWIG 生成。 根据 source code for osgeo.ogr.Geometry (Point 是什么),正确的类型被传递给了 Transform 方法。
最佳猜测:这是否是由于使用的 _ogr 版本太旧导致的,因此 _ogr.Geometry_Transform(self, *args) 的实现期望不同? _ogr 是另一个 SWIG 生成的代理,我猜是 OGR 类?
最佳答案
每个刚接触 GDAL 的人都必须学习的东西:分配一个错误处理程序。 (例如:http://pcjericks.github.io/py-gdalogr-cookbook/gdal_general.html#install-gdal-ogr-error-handler)
分配了错误处理程序后,输出包括对错误的解释。在这种情况下,它是:“无法加载 PROJ.4 库 (libproj.so),创建 OGRCoordinateTransformation 失败。”
希望传授启用 GDAL 错误处理的知识能帮助其他可能偶然发现这个问题的人。
关于python - GDAL 重投影错误 : in method 'Geometry_Transform' , 类型 'OSRCoordinateTransformationShadow *' 的参数 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27572190/