From 142ef30c16f53b3d00b9ca9c9b34a5d925cff72e Mon Sep 17 00:00:00 2001 From: Martin Yeo Date: Fri, 11 Oct 2024 14:58:56 +0100 Subject: [PATCH] Make Iris backwards compatible with Cartopy. --- docs/src/whatsnew/latest.rst | 7 ++++--- lib/iris/plot.py | 22 ++++++++++++++++++++++ requirements/py310.yml | 2 +- requirements/py311.yml | 2 +- requirements/py312.yml | 2 +- requirements/pypi-core.txt | 2 +- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 5748f510c5..1944ad198f 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -78,9 +78,7 @@ This document explains the changes made to Iris for this release 🔗 Dependencies =============== -#. `@trexfeathers`_ introduced a ``!=0.23`` Cartopy pin, to avoid - a previous bug of titles clashing with axis labels. - (`cartopy#2390`_, :pull:`6171`) +#. N/A 📚 Documentation @@ -95,6 +93,9 @@ This document explains the changes made to Iris for this release in Iris v3.10.0, :pull:`5948`) to use the same statistical repeat strategy as timing benchmarks. (:pull:`5981`) +#. `@trexfeathers`_ adapted Iris to work with Cartopy v0.24. (:pull:`6171`, + :pull:`6172`) + .. comment Whatsnew author names (@github name) in alphabetical order. Note that, diff --git a/lib/iris/plot.py b/lib/iris/plot.py index f787ad4326..89acc9764c 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -12,6 +12,7 @@ import datetime import warnings +import cartopy import cartopy.crs as ccrs from cartopy.geodesic import Geodesic import cartopy.mpl.geoaxes @@ -25,6 +26,7 @@ import matplotlib.transforms as mpl_transforms import numpy as np import numpy.ma as ma +from packaging.version import Version import iris.analysis.cartography as cartography import iris.coord_systems @@ -43,6 +45,26 @@ PlotDefn = collections.namedtuple("PlotDefn", ("coords", "transpose")) +class _GeoAxesPatched(cartopy.mpl.geoaxes.GeoAxes): + # Workaround for a bug where titles collide with axis labels (cartopy#2390) + # Bug is only present in Cartopy v0.23, so this will only be invoked for + # that version. + def _draw_preprocess(self, renderer): + super()._draw_preprocess(renderer) + + for artist in self.artists: + if hasattr(artist, "_draw_gridliner"): + # Note this is only necessary since Cartopy v0.23, but is not + # wasteful for earlier versions as _draw_gridliner() includes + # a check for whether a draw is necessary. + artist._draw_gridliner(renderer=renderer) + + +cartopy_version = Version(cartopy.__version__) +if cartopy_version.major == "0" and cartopy_version.minor == "23": + cartopy.mpl.geoaxes.GeoAxes = _GeoAxesPatched + + def _get_plot_defn_custom_coords_picked(cube, coords, mode, ndims=2): def names(coords): result = [] diff --git a/requirements/py310.yml b/requirements/py310.yml index afcb01a426..e9246a990e 100644 --- a/requirements/py310.yml +++ b/requirements/py310.yml @@ -11,7 +11,7 @@ dependencies: - setuptools-scm >=7 # Core dependencies. - - cartopy >=0.21,!=0.23 + - cartopy >=0.21 - cf-units >=3.1 - cftime >=1.5 - dask-core >=2022.9.0,!=2024.8.0 diff --git a/requirements/py311.yml b/requirements/py311.yml index db05d4549a..dad2080efa 100644 --- a/requirements/py311.yml +++ b/requirements/py311.yml @@ -11,7 +11,7 @@ dependencies: - setuptools-scm >=7 # Core dependencies. - - cartopy >=0.21,!=0.23 + - cartopy >=0.21 - cf-units >=3.1 - cftime >=1.5 - dask-core >=2022.9.0,!=2024.8.0 diff --git a/requirements/py312.yml b/requirements/py312.yml index 82383315c7..6e1d4be351 100644 --- a/requirements/py312.yml +++ b/requirements/py312.yml @@ -11,7 +11,7 @@ dependencies: - setuptools-scm >=7 # Core dependencies. - - cartopy >=0.21,!=0.23 + - cartopy >=0.21 - cf-units >=3.1 - cftime >=1.5 - dask-core >=2022.9.0,!=2024.8.0 diff --git a/requirements/pypi-core.txt b/requirements/pypi-core.txt index 6db77efaa3..36baaec1b9 100644 --- a/requirements/pypi-core.txt +++ b/requirements/pypi-core.txt @@ -1,4 +1,4 @@ -cartopy>=0.21,!=0.23 +cartopy>=0.21 cf-units>=3.1 cftime>=1.5.0 dask[array]>=2022.9.0,!=2024.8.0