5

This is empty using Python .ImportFromEPSG(4326):

from osgeo import osr
#this fails:
testSR = osr.SpatialReference()
testSR.ImportFromEPSG(4326)
print testSR.ExportToPrettyWkt()
#why did the import from EPSG fail above?
testSR.SetWellKnownGeogCS("WGS84")
print testSR.ExportToPrettyWkt()
GEOGCS["WGS 84",
DATUM["WGS_1984", and so on...
asked Dec 15, 2012 at 5:33
6
  • Do you need to get the list using Python? Or would just getting the list work? Commented Dec 15, 2012 at 7:06
  • What version of ogr are you using? Commented Dec 15, 2012 at 14:00
  • help(osr) Help on module osr: NAME osr - # import osgeo.osr as a convenience FILE c:\python27\arcgis10.1\lib\site-packages\osr.py Commented Dec 15, 2012 at 14:28
  • testSR.ImportFromEPSG('4326')? Do you need to pass a str instead of an int? - Also, make sure to go back and accept some answers to your other questions. Commented Jan 14, 2013 at 14:40
  • 1
    Make sure GDAL_DATA is set to the proper location. It may be that the EPSG import is failing because it can't find the files. Commented Jan 14, 2013 at 20:28

2 Answers 2

9

First, the real error was skipped, since ImportFromEPSG returned a non-zero error code:

from osgeo import osr
testSR = osr.SpatialReference()
res = testSR.ImportFromEPSG(4326)
if res != 0:
 raise RuntimeError(repr(res) + ': could not import from EPSG')
print testSR.ExportToPrettyWkt()

Now the cause. GDAL needs an environment variable GDAL_DATA to find and use projection info. If it is not available, then some things stop working. The SRID look-up codes are in GDAL_DATA, for instance. Check if you have it:

import os
'GDAL_DATA' in os.environ

if False, it should be added to either your system's environment variables, or you can add it in run-time:

if 'GDAL_DATA' not in os.environ:
 os.environ['GDAL_DATA'] = r'/path/to/gdal_data'
answered Feb 13, 2013 at 19:25
5
  • If you set GDAL_DATA at run-time, you need to do that before you import osr and gdal Commented Oct 28, 2016 at 12:51
  • also, what is the difference between GDAL_PATH and GDAL_DATA Commented Aug 24, 2017 at 14:01
  • Very helpful, my path on Ubuntu is /usr/share/gdal/2.1 Commented Aug 24, 2017 at 14:04
  • 1
    @RutgerH you've found a typo (there is no such thing as "GDAL_PATH"), all fixed, thanks! Commented Aug 25, 2017 at 3:32
  • Anyway I had RuntimeError: 7: could not import from EPSG... and False when checking if 'GDAL_DATA' in os.environ. Although I specified os.environ['GDAL_DATA'] = r'/usr/share/gdal/2.2/' What's wrong? Commented Aug 29, 2018 at 12:24
1

You'll have to define test1 first

from osgeo import osr
test1 = osr.SpatialReference() #define test1
test1.ImportFromEPSG(4326)
print test1.ExportToPrettyWkt()

Only then can you call the ExportToPrettyWkt() function. Which should result in the following output:

GEOGCS["WGS 84",
 DATUM["WGS_1984",
 SPHEROID["WGS 84",6378137,298.257223563,
 AUTHORITY["EPSG","7030"]],
 AUTHORITY["EPSG","6326"]],
 PRIMEM["Greenwich",0,
 AUTHORITY["EPSG","8901"]],
 UNIT["degree",0.0174532925199433,
 AUTHORITY["EPSG","9122"]],
 AUTHORITY["EPSG","4326"]]
answered Dec 15, 2012 at 7:18
7
  • This returns an empty spatial reference: import osr testSR = osr.SpatialReference() testSR.ImportFromEPSG(4326) print testSR.ExportToPrettyWkt() Commented Dec 15, 2012 at 13:56
  • You mean the print statement does not print anything for you? Commented Dec 15, 2012 at 13:57
  • Correct. testSR.ImportFromEPSG(4326); txtTest = testSR.ExportToPrettyWkt(); len(txtTest) is zero while testSR.SetWellKnownGeogCS("WGS84"); txtTest = testSR.ExportToPrettyWkt() returns a string of 357 chars Commented Dec 15, 2012 at 14:04
  • What is your question btw? Now I'm confused. Is it to fix the error or to get the list of ESPG values? Where can I get a list of EPSG values for common spatial references? Also, how did you install your osr and what version are you using? Commented Dec 15, 2012 at 14:07
  • testSR.ImportFromEPSG(4326) returns an empty spatial reference osr is from: c:\python27\arcgis10.1\lib\site-packages\osr.py Commented Dec 15, 2012 at 14:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.