Coverage for py_tools_ds/environment/gdal_env.py: 25%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

28 statements  

1# -*- coding: utf-8 -*- 

2 

3# py_tools_ds - A collection of geospatial data analysis tools that simplify standard 

4# operations when handling geospatial raster and vector data as well as projections. 

5# 

6# Copyright (C) 2016-2021 

7# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de) 

8# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam, 

9# Germany (https://www.gfz-potsdam.de/) 

10# 

11# This software was developed within the context of the GeoMultiSens project funded 

12# by the German Federal Ministry of Education and Research 

13# (project grant code: 01 IS 14 010 A-C). 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26 

27import os 

28import sys 

29import re 

30 

31__author__ = "Daniel Scheffler" 

32 

33 

34def find_epsgfile(): 

35 """Locate the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg').""" 

36 try: 

37 epsgfile = os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg') 

38 assert os.path.exists(epsgfile) 

39 except (KeyError, AssertionError): 

40 try: 

41 from pyproj import __file__ as pyprojpath 

42 epsgfile = os.path.join(os.path.dirname(pyprojpath), 'data/epsg') 

43 assert os.path.exists(epsgfile) 

44 except (ImportError, AssertionError): 

45 epsgfile = '/usr/local/share/proj/epsg' 

46 if not os.path.exists(epsgfile): 

47 raise RuntimeError('Could not locate epsg file for converting WKT to EPSG code. ' 

48 'Please make sure that your GDAL_DATA environment variable is properly set and the ' 

49 'pyproj library is installed.') 

50 return epsgfile 

51 

52 

53def try2set_GDAL_DATA(): 

54 """Try to set the 'GDAL_DATA' environment variable in case it is unset or invalid.""" 

55 if 'GDAL_DATA' not in os.environ or not os.path.isdir(os.environ['GDAL_DATA']): 

56 is_anaconda = 'conda' in sys.version or 'Continuum' in sys.version or \ 

57 re.search('conda', sys.executable, re.I) 

58 if is_anaconda: 

59 if sys.platform in ['linux', 'linux2']: 

60 GDAL_DATA = os.path.join(os.path.dirname(sys.executable), "..", "share", "gdal") 

61 else: 

62 GDAL_DATA = os.path.join(os.path.dirname(sys.executable), "Library", "share", "gdal") 

63 else: 

64 GDAL_DATA = os.path.join("usr", "local", "share", "gdal") if sys.platform in ['linux', 'linux2'] else '' 

65 

66 if os.path.isdir(GDAL_DATA): 

67 os.environ['GDAL_DATA'] = GDAL_DATA