Skip to content

Commit befade1

Browse files
committed
Add diagnostics functions: proj4 ver & Inv. Hammer
Refactored package_version function to move proj4 version checking into proj4_version function. check_proj_inv_hammer function added to check if the inverse of the hammer projection is available (which should be in the yet to be released proj.4 version 4.9.3). check_proj_inv_hammer() and proj4_version() are intended to end up in setup.py, but may provide some use in the main basemap library. Due to that reason, the imports are mostly self-contained for the function.
1 parent ef557d7 commit befade1

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

lib/mpl_toolkits/basemap/diagnostic.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
These are diagnostic and debugging functions for basemap.
33
"""
44

5+
def proj4_version():
6+
"""
7+
Gives the proj.4 library's version number. (requires pyproj to be installed)
8+
9+
returns string, so proj.4 version 4.9.3 will return "4.9.3"
10+
"""
11+
import pyproj
12+
13+
# Get PROJ4 version in a floating point number
14+
proj4_ver_num = pyproj.Proj(proj='latlong').proj_version
15+
16+
# reformats floating point number into string (4.90 becomes '4.9.0')
17+
# Exploits single number version numbers for proj4,
18+
# This will need likely need to be change at some point as proj.4 version 4.10.0???
19+
return '.'.join( str(int(proj4_ver_num*100)) )
20+
21+
522
def package_versions():
623
"""
724
Gives version information for dependent packages.
@@ -13,7 +30,7 @@ def package_versions():
1330

1431
from matplotlib import __version__ as matplotlib_version
1532
from numpy import __version__ as numpy_version
16-
import pyproj
33+
from pyproj import __version__ as pyproj_version
1734
from shapefile import __version__ as pyshp_version
1835

1936
import _geoslib
@@ -35,10 +52,6 @@ def package_versions():
3552
pil_version = 'not installed'
3653
pillow_version = 'not installed'
3754

38-
# Get PROJ.4 version info in a floating point number
39-
proj_ver_num = pyproj.Proj(init='epsg:4326').proj_version
40-
# reformats floating point number into string (4.90 becomes '4.9.0')
41-
proj4_version = '.'.join(list(str(int(proj_ver_num*100))))
4255

4356
BasemapPackageVersions = namedtuple(
4457
'BasemapPackageVersions',
@@ -51,11 +64,44 @@ def package_versions():
5164
basemap = basemap_version,
5265
matplotlib = matplotlib_version,
5366
numpy = numpy_version,
54-
pyproj = pyproj.__version__,
67+
pyproj = pyproj_version,
5568
pyshp = pyshp_version,
56-
PROJ4 = proj4_version,
69+
PROJ4 = proj4_version(),
5770
GEOS = _geoslib.__geos_version__,
5871
# optional dependencies below
5972
OWSLib = OWSLib_version,
6073
PIL = pil_version,
6174
Pillow = pillow_version)
75+
76+
def check_proj_inv_hammer(segfault_protection_off=False):
77+
"""
78+
Check if the inverse of the hammer projection is supported by installed
79+
version of PROJ4.
80+
81+
segfault_protection_off=True - Turns off the protection from a segfault.
82+
BE CAREFUL setting this to True.
83+
84+
returns True - inverse hammer is supported
85+
False - inverse hammer is not supported
86+
"Unknown" - support is Unknown
87+
"""
88+
from distutils.version import LooseVersion
89+
from pyproj import __version__ as pyproj_version
90+
91+
if LooseVersion(proj4_version()) > LooseVersion('4.9.2'):
92+
return True
93+
94+
# pyproj_version_tup = version_str_to_tuple(pyproj_version)
95+
if LooseVersion(pyproj_version) > LooseVersion('1.9.5.1') \
96+
or segfault_protection_off is True:
97+
from pyproj import Proj
98+
hammer = Proj(proj='hammer')
99+
100+
x, y = hammer(-30.0, 40.0)
101+
try:
102+
lon, lat = hammer(x, y, inverse=True)
103+
return True
104+
except RuntimeError:
105+
return False
106+
107+
return 'Unknown'

0 commit comments

Comments
 (0)