Commit d9c1dc93 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Merge branch 'enhancement/add_auto_setter_GDAL_DATA' into bugfix/fix_get_overlap_polygon

parents ad3f4d04 27a6cc05
Pipeline #1140 passed with stages
in 5 minutes and 31 seconds
# -*- coding: utf-8 -*-
import os
import sys
import re
__author__ = "Daniel Scheffler"
def find_epsgfile():
"""Locate the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg')."""
try:
epsgfile = os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg')
assert os.path.exists(epsgfile)
except (KeyError, AssertionError):
try:
from pyproj import __file__ as pyprojpath
epsgfile = os.path.join(os.path.dirname(pyprojpath), 'data/epsg')
assert os.path.exists(epsgfile)
except (ImportError, AssertionError):
epsgfile = '/usr/local/share/proj/epsg'
if not os.path.exists(epsgfile):
raise RuntimeError('Could not locate epsg file for converting WKT to EPSG code. '
'Please make sure that your GDAL_DATA environment variable is properly set and the '
'pyproj library is installed.')
return epsgfile
def try2set_GDAL_DATA():
"""Try to set the 'GDAL_DATA' environment variable in case it is unset or invalid."""
if 'GDAL_DATA' not in os.environ or not os.path.isdir(os.environ['GDAL_DATA']):
is_anaconda = 'conda' in sys.version or 'Continuum' in sys.version or \
re.search('conda', sys.executable, re.I)
if is_anaconda:
if sys.platform in ['linux', 'linux2']:
GDAL_DATA = os.path.join(os.path.dirname(sys.executable), "..", "share", "gdal")
else:
GDAL_DATA = os.path.join(os.path.dirname(sys.executable), "Library", "share", "gdal")
else:
GDAL_DATA = os.path.join("usr", "local", "share", "gdal") if sys.platform in ['linux', 'linux2'] else ''
if os.path.isdir(GDAL_DATA):
os.environ['GDAL_DATA'] = GDAL_DATA
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__author__ = "Daniel Scheffler"
import re import re
import os
import pyproj import pyproj
from typing import Union from typing import Union
...@@ -20,6 +18,13 @@ except ImportError: ...@@ -20,6 +18,13 @@ except ImportError:
import gdalnumeric import gdalnumeric
from gdalconst import * from gdalconst import *
from ..environment import gdal_env
__author__ = "Daniel Scheffler"
# try to set GDAL_DATA if unnot set or invalid
gdal_env.try2set_GDAL_DATA()
def get_proj4info(ds=None, proj=None): def get_proj4info(ds=None, proj=None):
# type: (gdal.Dataset,str) -> str # type: (gdal.Dataset,str) -> str
...@@ -106,7 +111,12 @@ def EPSG2Proj4(EPSG_code): ...@@ -106,7 +111,12 @@ def EPSG2Proj4(EPSG_code):
if EPSG_code is not None: if EPSG_code is not None:
srs = osr.SpatialReference() srs = osr.SpatialReference()
srs.ImportFromEPSG(EPSG_code) srs.ImportFromEPSG(EPSG_code)
return srs.ExportToProj4() proj4 = srs.ExportToProj4()
if not proj4:
raise EnvironmentError(gdal.GetLastErrorMsg())
return proj4
else: else:
return '' return ''
...@@ -114,15 +124,10 @@ def EPSG2Proj4(EPSG_code): ...@@ -114,15 +124,10 @@ def EPSG2Proj4(EPSG_code):
def EPSG2WKT(EPSG_code): def EPSG2WKT(EPSG_code):
# type: (int) -> str # type: (int) -> str
if EPSG_code is not None: if EPSG_code is not None:
# GDAL_DATA_tmp = None
# if 'GDAL_DATA' in os.environ:
# GDAL_DATA_tmp = os.environ['GDAL_DATA']
# del os.environ['GDAL_DATA']
srs = osr.SpatialReference() srs = osr.SpatialReference()
srs.ImportFromEPSG(EPSG_code) srs.ImportFromEPSG(EPSG_code)
wkt = srs.ExportToWkt() wkt = srs.ExportToWkt()
# if GDAL_DATA_tmp:
# os.environ['GDAL_DATA'] = GDAL_DATA_tmp
if not wkt: if not wkt:
raise EnvironmentError(gdal.GetLastErrorMsg()) raise EnvironmentError(gdal.GetLastErrorMsg())
...@@ -131,25 +136,6 @@ def EPSG2WKT(EPSG_code): ...@@ -131,25 +136,6 @@ def EPSG2WKT(EPSG_code):
return '' return ''
def _find_epsgfile():
"""Locate the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg')."""
try:
epsgfile = os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg')
assert os.path.exists(epsgfile)
except (KeyError, AssertionError):
try:
from pyproj import __file__ as pyprojpath
epsgfile = os.path.join(os.path.dirname(pyprojpath), 'data/epsg')
assert os.path.exists(epsgfile)
except (ImportError, AssertionError):
epsgfile = '/usr/local/share/proj/epsg'
if not os.path.exists(epsgfile):
raise RuntimeError('Could not locate epsg file for converting WKT to EPSG code. '
'Please make sure that your GDAL_DATA environment variable is properly set and the '
'pyproj library is installed.')
return epsgfile
def WKT2EPSG(wkt, epsgfile=''): def WKT2EPSG(wkt, epsgfile=''):
# type: (str) -> Union[int, None] # type: (str) -> Union[int, None]
""" Transform a WKT string to an EPSG code """ Transform a WKT string to an EPSG code
...@@ -184,7 +170,7 @@ def WKT2EPSG(wkt, epsgfile=''): ...@@ -184,7 +170,7 @@ def WKT2EPSG(wkt, epsgfile=''):
if ac is None: # try brute force approach by grokking proj epsg definition file if ac is None: # try brute force approach by grokking proj epsg definition file
p_out = p_in.ExportToProj4() p_out = p_in.ExportToProj4()
if p_out: if p_out:
epsgfile = epsgfile or _find_epsgfile() epsgfile = epsgfile or gdal_env.find_epsgfile()
with open(epsgfile) as f: with open(epsgfile) as f:
for line in f: for line in f:
if line.find(p_out) != -1: if line.find(p_out) != -1:
...@@ -216,4 +202,9 @@ def get_prjLonLat(fmt='wkt'): ...@@ -216,4 +202,9 @@ def get_prjLonLat(fmt='wkt'):
assert re.search('wkt', fmt, re.I) or re.search('Proj4', fmt, re.I), 'unsupported output format' assert re.search('wkt', fmt, re.I) or re.search('Proj4', fmt, re.I), 'unsupported output format'
srs = osr.SpatialReference() srs = osr.SpatialReference()
srs.ImportFromEPSG(4326) srs.ImportFromEPSG(4326)
return srs.ExportToWkt() if re.search('wkt', fmt, re.I) else srs.ExportToProj4() out = srs.ExportToWkt() if re.search('wkt', fmt, re.I) else srs.ExportToProj4()
if not out:
raise EnvironmentError(gdal.GetLastErrorMsg())
return out
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment