Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This is empty using Python .ImportFromEPSG(4326):

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...
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\osr.py –  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
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. –  kyle Jan 14 '13 at 20:28

2 Answers 2

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
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"]]
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\osr.py –  Dave Dec 15 '12 at 14:10

Your Answer

 
discard

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.