Val H
Val H

Reputation: 527

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?

Upvotes: 5

Views: 6116

Answers (3)

nadya
nadya

Reputation: 240

With help of the answer of jeremy that GDAL fails to load its information, I just edited the code to specify directly the PROJ.4 parameters from the EPSG webside and it runs

#target.ImportFromEPSG(4326)
target.ImportFromProj4('+proj=longlat +datum=WGS84 +no_defs')

Upvotes: 0

jeremy
jeremy

Reputation: 10057

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/'

Upvotes: 1

Val H
Val H

Reputation: 527

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.

Upvotes: 4

Related Questions