From 784eefaabecb97db9ab3251482a8af1affe031e2 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 6 Apr 2016 02:59:15 -0400 Subject: [PATCH 1/3] Fix proj4 version check for Robinson projection. Single digit regex matches will erroneously match versions such as 4.10 as 4.1 or 14.8 as 4.8, which may trigger the Robinson projection warning even for new-and-fixed proj4 releases. --- lib/cartopy/crs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 1f1d6e9df..90dbcb1f2 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -1454,10 +1454,10 @@ def __init__(self, central_longitude=0, globe=None): # 40 deg N introduced by incomplete fix to issue #113 (see # https://trac.osgeo.org/proj/ticket/113). import re - match = re.search(r"\d\.\d", PROJ4_RELEASE) + match = re.search(r"\d+\.\d+", PROJ4_RELEASE) if match is not None: - proj4_version = float(match.group()) - if 4.8 <= proj4_version < 4.9: + proj4_version = tuple(int(v) for v in match.group().split('.')) + if (4, 8) <= proj4_version < (4, 9): warnings.warn('The Robinson projection in the v4.8.x series ' 'of Proj.4 contains a discontinuity at ' '40 deg latitude. Use this projection with ' From e565c8a6892ce6209429c33558e1d12c68b163a1 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 6 Apr 2016 03:15:37 -0400 Subject: [PATCH 2/3] Warn when using AzimuthalEquidistant with old proj.4. Old proj.4 has an issue with the AzimuthalEquidistant transformation past 90 degrees. --- lib/cartopy/crs.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 90dbcb1f2..ea16df127 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -1748,6 +1748,23 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0, default globe is created. """ + # Warn when using Azimuthal Equidistant with proj4 < 4.9.2 due to + # incorrect transformation past 90 deg distance (see + # https://github.com/OSGeo/proj.4/issues/246). + import re + match = re.search(r"\d+\.\d+.\d+", PROJ4_RELEASE) + if match is not None: + proj4_version = tuple(int(v) for v in match.group().split('.')) + if proj4_version < (4, 9, 2): + warnings.warn('The Azimuthal Equidistant projection in Proj.4 ' + 'older than 4.9.2 incorrectly transforms points ' + 'farther than 90 deg from the origin. Use this ' + 'projection with caution.') + else: + warnings.warn('Cannot determine Proj.4 version. The Azimuthal ' + 'Equidistant projection may be unreliable and ' + 'should be used with caution.') + proj4_params = [('proj', 'aeqd'), ('lon_0', central_longitude), ('lat_0', central_latitude), ('x_0', false_easting), ('y_0', false_northing)] From e77ceadbaf6513c8bbfa688fc348fc729b7294de Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 11 Apr 2016 17:21:47 -0400 Subject: [PATCH 3/3] Factor out duplicate parsing of proj.4 version. --- lib/cartopy/_crs.pyx | 6 ++++++ lib/cartopy/crs.py | 15 +++++---------- lib/cartopy/tests/__init__.py | 9 --------- lib/cartopy/tests/crs/test_robinson.py | 3 +-- lib/cartopy/tests/mpl/test_gridliner.py | 1 - lib/cartopy/tests/mpl/test_mpl_integration.py | 6 +++--- lib/cartopy/tests/mpl/test_shapely_to_mpl.py | 1 - 7 files changed, 15 insertions(+), 26 deletions(-) diff --git a/lib/cartopy/_crs.pyx b/lib/cartopy/_crs.pyx index 8f17d65b7..81a321c05 100644 --- a/lib/cartopy/_crs.pyx +++ b/lib/cartopy/_crs.pyx @@ -22,6 +22,7 @@ The CRS class is the base-class for all projections defined in :mod:`cartopy.crs """ from collections import OrderedDict +import re import warnings import numpy as np @@ -51,6 +52,11 @@ cdef double NAN = float('nan') PROJ4_RELEASE = pj_get_release() if six.PY3: PROJ4_RELEASE = PROJ4_RELEASE.decode() +_match = re.search(r"\d+\.\d+.\d+", PROJ4_RELEASE) +if _match is not None: + PROJ4_VERSION = tuple(int(v) for v in _match.group().split('.')) +else: + PROJ4_VERSION = () class Proj4Error(Exception): diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index ea16df127..96e7c7f63 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -33,7 +33,7 @@ from shapely.prepared import prep import six -from cartopy._crs import CRS, Geocentric, Geodetic, Globe, PROJ4_RELEASE +from cartopy._crs import CRS, Geocentric, Geodetic, Globe, PROJ4_VERSION import cartopy.trace @@ -1454,10 +1454,8 @@ def __init__(self, central_longitude=0, globe=None): # 40 deg N introduced by incomplete fix to issue #113 (see # https://trac.osgeo.org/proj/ticket/113). import re - match = re.search(r"\d+\.\d+", PROJ4_RELEASE) - if match is not None: - proj4_version = tuple(int(v) for v in match.group().split('.')) - if (4, 8) <= proj4_version < (4, 9): + if PROJ4_VERSION != (): + if (4, 8) <= PROJ4_VERSION < (4, 9): warnings.warn('The Robinson projection in the v4.8.x series ' 'of Proj.4 contains a discontinuity at ' '40 deg latitude. Use this projection with ' @@ -1751,11 +1749,8 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0, # Warn when using Azimuthal Equidistant with proj4 < 4.9.2 due to # incorrect transformation past 90 deg distance (see # https://github.com/OSGeo/proj.4/issues/246). - import re - match = re.search(r"\d+\.\d+.\d+", PROJ4_RELEASE) - if match is not None: - proj4_version = tuple(int(v) for v in match.group().split('.')) - if proj4_version < (4, 9, 2): + if PROJ4_VERSION != (): + if PROJ4_VERSION < (4, 9, 2): warnings.warn('The Azimuthal Equidistant projection in Proj.4 ' 'older than 4.9.2 incorrectly transforms points ' 'farther than 90 deg from the origin. Use this ' diff --git a/lib/cartopy/tests/__init__.py b/lib/cartopy/tests/__init__.py index 0fa7c787d..95018127c 100644 --- a/lib/cartopy/tests/__init__.py +++ b/lib/cartopy/tests/__init__.py @@ -24,15 +24,6 @@ import shutil import types -from cartopy._crs import PROJ4_RELEASE as _PROJ4_RELEASE - - -_match = re.search(r"\d\.\d", _PROJ4_RELEASE) -if _match is not None: - _proj4_version = float(_match.group()) -else: - _proj4_version = 0.0 - @contextlib.contextmanager def temp_dir(suffix=None): diff --git a/lib/cartopy/tests/crs/test_robinson.py b/lib/cartopy/tests/crs/test_robinson.py index 3b1905f31..29000d182 100644 --- a/lib/cartopy/tests/crs/test_robinson.py +++ b/lib/cartopy/tests/crs/test_robinson.py @@ -33,7 +33,6 @@ from numpy.testing import assert_array_almost_equal as assert_arr_almost_eq import cartopy.crs as ccrs -from cartopy.tests import _proj4_version _NAN = float('nan') @@ -41,7 +40,7 @@ _CRS_ROB = ccrs.Robinson() # Increase tolerance if using older proj.4 releases -_TOL = -1 if _proj4_version < 4.9 else 7 +_TOL = -1 if ccrs.PROJ4_VERSION < (4, 9) else 7 def test_transform_point(): diff --git a/lib/cartopy/tests/mpl/test_gridliner.py b/lib/cartopy/tests/mpl/test_gridliner.py index 409c2e5e5..bcbc43d31 100644 --- a/lib/cartopy/tests/mpl/test_gridliner.py +++ b/lib/cartopy/tests/mpl/test_gridliner.py @@ -32,7 +32,6 @@ from cartopy.mpl.geoaxes import GeoAxes from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER -from cartopy.tests import _proj4_version from cartopy.tests.mpl import ImageTesting diff --git a/lib/cartopy/tests/mpl/test_mpl_integration.py b/lib/cartopy/tests/mpl/test_mpl_integration.py index 50f5e2944..e12be1132 100644 --- a/lib/cartopy/tests/mpl/test_mpl_integration.py +++ b/lib/cartopy/tests/mpl/test_mpl_integration.py @@ -32,11 +32,10 @@ import cartopy.crs as ccrs -from cartopy.tests import _proj4_version from cartopy.tests.mpl import ImageTesting -_ROB_TOL = 0.5 if _proj4_version < 4.9 else 0.1 +_ROB_TOL = 0.5 if ccrs.PROJ4_VERSION < (4, 9) else 0.1 @ImageTesting(['global_contour_wrap']) @@ -119,7 +118,8 @@ def test_global_scatter_wrap_no_transform(): plt.scatter(x, y, c=data) -@ImageTesting(['global_map'], tolerance=16 if _proj4_version < 4.9 else 0.1) +@ImageTesting(['global_map'], + tolerance=16 if ccrs.PROJ4_VERSION < (4, 9) else 0.1) def test_global_map(): ax = plt.axes(projection=ccrs.Robinson()) # ax.coastlines() diff --git a/lib/cartopy/tests/mpl/test_shapely_to_mpl.py b/lib/cartopy/tests/mpl/test_shapely_to_mpl.py index 976e2e3d7..c29030703 100644 --- a/lib/cartopy/tests/mpl/test_shapely_to_mpl.py +++ b/lib/cartopy/tests/mpl/test_shapely_to_mpl.py @@ -28,7 +28,6 @@ import cartopy.crs as ccrs import cartopy.mpl.patch as cpatch -from cartopy.tests import _proj4_version from cartopy.tests.mpl import ImageTesting