From c4f20872acc0ebbd3bdbab3a0766d2473661fd93 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sat, 29 Jun 2024 07:36:57 -0600 Subject: [PATCH] FIX/MNT: Simplify project geometry handling The MultiLineString return type was a plain list, but it should be an empty MultiLineString to be consistent with the other types. Additionally, all geometry constructors take empty lists, so just use that rather than special-casing the returns. --- lib/cartopy/crs.py | 16 +++------------- lib/cartopy/tests/test_line_string.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index c24415167..bc3541987 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -918,10 +918,7 @@ def _project_multipoint(self, geometry, src_crs): geoms = [] for geom in geometry.geoms: geoms.append(self._project_point(geom, src_crs)) - if geoms: - return sgeom.MultiPoint(geoms) - else: - return sgeom.MultiPoint() + return sgeom.MultiPoint(geoms) def _project_multiline(self, geometry, src_crs): geoms = [] @@ -929,10 +926,7 @@ def _project_multiline(self, geometry, src_crs): r = self._project_line_string(geom, src_crs) if r: geoms.extend(r.geoms) - if geoms: - return sgeom.MultiLineString(geoms) - else: - return [] + return sgeom.MultiLineString(geoms) def _project_multipolygon(self, geometry, src_crs): geoms = [] @@ -940,11 +934,7 @@ def _project_multipolygon(self, geometry, src_crs): r = self._project_polygon(geom, src_crs) if r: geoms.extend(r.geoms) - if geoms: - result = sgeom.MultiPolygon(geoms) - else: - result = sgeom.MultiPolygon() - return result + return sgeom.MultiPolygon(geoms) def _project_polygon(self, polygon, src_crs): """ diff --git a/lib/cartopy/tests/test_line_string.py b/lib/cartopy/tests/test_line_string.py index a0b42ff0e..926dd5b5b 100644 --- a/lib/cartopy/tests/test_line_string.py +++ b/lib/cartopy/tests/test_line_string.py @@ -8,6 +8,7 @@ import numpy as np import pytest +import shapely import shapely.geometry as sgeom import cartopy.crs as ccrs @@ -73,6 +74,15 @@ def test_out_of_domain_efficiency(self): tgt_proj.project_geometry(line_string, src_proj) assert time.time() < cutoff_time, 'Projection took too long' + @pytest.mark.skipif(shapely.__version__ < "2", + reason="Shapely <2 has an incorrect geom_type ") + def test_multi_linestring_return_type(self): + # Check that the return type of project_geometry is a MultiLineString + # and not an empty list + multi_line_string = ccrs.Mercator().project_geometry( + sgeom.MultiLineString(), ccrs.PlateCarree()) + assert isinstance(multi_line_string, sgeom.MultiLineString) + class FakeProjection(ccrs.PlateCarree): def __init__(self, left_offset=0, right_offset=0):