Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#5891 closed defect (fixed)

Export projections sometimes return Unicode, e.g. EPSG:42104

Reported by: Mike Taves Owned by: hobu
Priority: normal Milestone: 2.0.0
Component: PythonBindings Version: 1.11.1
Severity: normal Keywords:


With GDAL for Python 2, several "ExportTo..." methods sometimes return a Unicode result rather than a string. This may cause issues with SetProjection, which expects a string. Specifically, EPSG:42104: NAD83 / MTM zone 8 Québec.

Consider the following Python code:

from osgeo import osr
from osgeo import gdal

srs = osr.SpatialReference()

for m in dir(srs):
    if m.startswith('ExportTo'):
        print(m + ': ' + str(type(getattr(srs, m)())))

srs_wkt = srs.ExportToWkt()

drv = gdal.GetDriverByName('MEM')
ds = drv.Create('', 2, 3)
ds.SetProjection(srs_wkt)  # raises TypeError: in method 'Dataset_SetProjection', argument 2 of type 'char const *'

With output and error:

ExportToMICoordSys: <type 'str'>
ExportToPCI: <type 'list'>
ExportToPrettyWkt: <type 'unicode'>
ExportToProj4: <type 'str'>
ExportToUSGS: <type 'list'>
ExportToWkt: <type 'unicode'>
ExportToXML: <type 'unicode'>
PROJCS["NAD83 / MTM zone 8 Qubec",GEOGCS["GRS80",DATUM["GRS_1980",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Decimal_Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-73.5],PARAMETER["scale_factor",0.9999],PARAMETER["false_easting",304800],PARAMETER["false_northing",0],UNIT["Meter",1],AUTHORITY["EPSG","42104"]]
Traceback (most recent call last):
  File "/usr/local/lib64/python/site-packages/GDAL-1.11.1-py2.6-linux-x86_64.egg/osgeo/", line 722, in SetProjection
    return _gdal.Dataset_SetProjection(self, *args)
TypeError: in method 'Dataset_SetProjection', argument 2 of type 'char const *'

Note affected methods are ExportToPrettyWkt(), ExportToWkt() and ExportToXML(). Inspecting their output, no Unicode characters were actually found! However, cubewerx_extra.wkt (line 26) appears to be Unicode, as I see "Québec" in the name.

Furthermore, should SetProjection accept Unicode inputs? A simple workaround is just to encode the string in ASCII, e.g. ds.SetProjection(srs_wkt.encode('ascii')).

Python 3 is not affected.

Change History (2)

comment:1 by Even Rouault, 8 years ago

Milestone: 2.0
Resolution: fixed
Status: newclosed

Easiest fix was r28779 "cubewerx_extra.wkt: replace Latin1 eacute character (first e in Quebec) by ASCII e (#5891)"

It appears this was the only occurrence on a non-ASCII character in WKT

comment:2 by Even Rouault, 7 years ago


Milestone renamed

Note: See TracTickets for help on using tickets.