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...
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_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 importosr
andgdal
Dima Chubarov– Dima Chubarov2016年10月28日 12:51:44 +00:00Commented Oct 28, 2016 at 12:51 -
also, what is the difference between GDAL_PATH and GDAL_DATARutgerH– RutgerH2017年08月24日 14:01:05 +00:00Commented Aug 24, 2017 at 14:01
-
Very helpful, my path on Ubuntu is /usr/share/gdal/2.1RutgerH– RutgerH2017年08月24日 14:04:38 +00:00Commented Aug 24, 2017 at 14:04
-
1@RutgerH you've found a typo (there is no such thing as "GDAL_PATH"), all fixed, thanks!Mike T– Mike T2017年08月25日 03:32:27 +00:00Commented 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?Jane– Jane2018年08月29日 12:24:32 +00:00Commented Aug 29, 2018 at 12:24
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"]]
-
This returns an empty spatial reference: import osr testSR = osr.SpatialReference() testSR.ImportFromEPSG(4326) print testSR.ExportToPrettyWkt()Dave– Dave2012年12月15日 13:56:03 +00:00Commented Dec 15, 2012 at 13:56
-
You mean the print statement does not print anything for you?R.K.– R.K.2012年12月15日 13:57:58 +00:00Commented 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 charsDave– Dave2012年12月15日 14:04:52 +00:00Commented 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?R.K.– R.K.2012年12月15日 14:07:21 +00:00Commented 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.pyDave– Dave2012年12月15日 14:10:19 +00:00Commented Dec 15, 2012 at 14:10
testSR.ImportFromEPSG('4326')
? Do you need to pass astr
instead of anint
? - Also, make sure to go back and accept some answers to your other questions.