Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is empty using Python .ImportFromEPSG(4326):

import osr
#this fails:
testSR = osr.SpatialReference()
print testSR.ExportToPrettyWkt()

#why did the import from EPSG fail above?
print testSR.ExportToPrettyWkt()
DATUM["WGS_1984", and so on...
share|improve this question
Do you need to get the list using Python? Or would just getting the list work? – R.K. Dec 15 '12 at 7:06
What version of ogr are you using? – R.K. Dec 15 '12 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\ – Dave Dec 15 '12 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. – Jay Laura Jan 14 '13 at 14:40
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. – kyle Jan 14 '13 at 20:28

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_PATH' 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'
share|improve this answer

You'll have to define test1 first

import osr
test1 = osr.SpatialReference() #define test1
print test1.ExportToPrettyWkt()

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

        SPHEROID["WGS 84",6378137,298.257223563,
share|improve this answer
This returns an empty spatial reference: import osr testSR = osr.SpatialReference() testSR.ImportFromEPSG(4326) print testSR.ExportToPrettyWkt() – Dave Dec 15 '12 at 13:56
You mean the print statement does not print anything for you? – R.K. Dec 15 '12 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 – Dave Dec 15 '12 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? – R.K. Dec 15 '12 at 14:07
testSR.ImportFromEPSG(4326) returns an empty spatial reference osr is from: c:\python27\arcgis10.1\lib\site-packages\ – Dave Dec 15 '12 at 14:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.