4

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...
  • 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. – user10353 Jan 14 '13 at 20:28
7

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'
  • If you set GDAL_DATA at run-time, you need to do that before you import osr and gdal – Dmitri Chubarov Oct 28 '16 at 12:51
  • also, what is the difference between GDAL_PATH and GDAL_DATA – RutgerH Aug 24 '17 at 14:01
  • Very helpful, my path on Ubuntu is /usr/share/gdal/2.1 – RutgerH Aug 24 '17 at 14:04
  • 1
    @RutgerH you've found a typo (there is no such thing as "GDAL_PATH"), all fixed, thanks! – Mike T Aug 25 '17 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? – Jane Aug 29 '18 at 12:24
1

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"]]
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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