Skip to content

Commit 116aefb

Browse files
committed
Merge pull request #281 from micahcochran/patch-1
Add diagnostics functions: proj4 ver. & Inv. Hammer
2 parents ef557d7 + 7c6d2f0 commit 116aefb

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

lib/mpl_toolkits/basemap/diagnostic.py

Lines changed: 54 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,45 @@ 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=True):
77+
"""
78+
Check if the inverse of the hammer projection is supported by installed
79+
version of PROJ4.
80+
81+
segfault_protection True (default) - test while protecting from segfault
82+
False - testing that might cause Python to segfault.
83+
BE CAREFUL setting this flag to False!
84+
If it segfaults, this the inverse hammer is not supported.
85+
86+
returns True - inverse hammer is supported
87+
False - inverse hammer is not supported
88+
"Unknown" - support is Unknown
89+
"""
90+
from distutils.version import LooseVersion
91+
from pyproj import __version__ as pyproj_version
92+
93+
if LooseVersion(proj4_version()) > LooseVersion('4.9.2'):
94+
return True
95+
96+
if LooseVersion(pyproj_version) > LooseVersion('1.9.5.1') \
97+
or segfault_protection is False:
98+
from pyproj import Proj
99+
hammer = Proj(proj='hammer')
100+
101+
x, y = hammer(-30.0, 40.0)
102+
try:
103+
lon, lat = hammer(x, y, inverse=True)
104+
return True
105+
except RuntimeError:
106+
return False
107+
108+
return 'Unknown'

0 commit comments

Comments
 (0)