Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDAL reprojection error: in method 'Geometry_Transform', argument 2 of type 'OSRCoordinateTransformationShadow *'

Using Python 2.7.9 with GDAL 1.11.1, with miniconda for package management --

Performing this a simple reprojection of a coordinate point causes the error described below.

I am relatively new to GDAL, so I checked to see if the code from the Python GDAL/OGR 1.0 Cookbook produces the same issue, and it does:

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

This is the error:

/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 is a proxy for the C++ OSRCoordinateTransformationShadow class, generated by SWIG. Per the source code for osgeo.ogr.Geometry (what Point is), the correct types were passed to the Transform method.

Best guess: Could this be caused by using a version of _ogr that is too old, and so the implementation of _ogr.Geometry_Transform(self, *args) is expecting a different? _ogr is another SWIG-generated proxy, I'm guessing for the OGR class?

like image 584
Val H Avatar asked Dec 19 '14 18:12

Val H


2 Answers

What everyone new to GDAL must learn: assign an error handler. (example: http://pcjericks.github.io/py-gdalogr-cookbook/gdal_general.html#install-gdal-ogr-error-handler)

With an error handler assigned, the output includes the explanation for the error. In this case, it was: "Unable to load PROJ.4 library (libproj.so), creation of OGRCoordinateTransformation failed."

Hopefully, imparting the knowledge of enabling GDAL error handling will help others who may stumble upon this very issue.

like image 98
Val H Avatar answered Sep 19 '22 21:09

Val H


Similar information can be found on a rasterio FAQ and in unable to load "gcs.csv" file in gdal.

I encountered this problem when running GDAL transformations in my Anaconda3 QGIS environment. The problem is that the coordinate system informations were not loading through the GDAL_DATA environment variable.

To remedy, locate where the directory containing gcs.csv exists within your system (potentially ".../Library/share/gdal"). Add this to your environment prior to importing GDAL & other dependents.

import os

os.environ['GDAL_DATA'] = r'/path/to/dir/'
like image 22
jeremy Avatar answered Sep 21 '22 21:09

jeremy