From e7aef2c1b454144c330372e4e9fad1cbba2846d1 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Wed, 26 Jan 2022 21:10:15 -0700 Subject: [PATCH] ENH: Allow a single geometry in add_geometries Allow single geometries to be added to a geoaxes, even though the method is plural. --- lib/cartopy/feature/__init__.py | 2 ++ lib/cartopy/tests/mpl/test_axes.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/cartopy/feature/__init__.py b/lib/cartopy/feature/__init__.py index e7685522e..86d4b7670 100644 --- a/lib/cartopy/feature/__init__.py +++ b/lib/cartopy/feature/__init__.py @@ -214,6 +214,8 @@ def __init__(self, geometries, crs, **kwargs): """ super().__init__(crs, **kwargs) + if isinstance(geometries, sgeom.base.BaseGeometry): + geometries = [geometries] self._geoms = tuple(geometries) def geometries(self): diff --git a/lib/cartopy/tests/mpl/test_axes.py b/lib/cartopy/tests/mpl/test_axes.py index a431ce989..904274ffe 100644 --- a/lib/cartopy/tests/mpl/test_axes.py +++ b/lib/cartopy/tests/mpl/test_axes.py @@ -13,6 +13,7 @@ import pytest import cartopy.crs as ccrs +import cartopy.feature as cfeature from cartopy.mpl.geoaxes import InterProjectionTransform, GeoAxes @@ -108,6 +109,14 @@ def test_styler_kwarg(self, ShapelyFeature, add_feature_method): add_feature_method.assert_called_once_with( ShapelyFeature(), styler=mock.sentinel.styler) + @pytest.mark.natural_earth + def test_single_geometry(self): + # A single geometry is acceptable + proj = ccrs.PlateCarree() + ax = GeoAxes(plt.figure(), [0, 0, 1, 1], + map_projection=proj) + ax.add_geometries(next(cfeature.COASTLINE.geometries()), crs=proj) + @cleanup def test_geoaxes_subplot():