Skip to content

Commit

Permalink
FIX/MNT: Simplify project geometry handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greglucas committed Jun 29, 2024
1 parent 738bba5 commit c4f2087
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/cartopy/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,33 +918,23 @@ 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 = []
for geom in geometry.geoms:
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 = []
for geom in geometry.geoms:
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):
"""
Expand Down
10 changes: 10 additions & 0 deletions lib/cartopy/tests/test_line_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
import pytest
import shapely
import shapely.geometry as sgeom

import cartopy.crs as ccrs
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c4f2087

Please sign in to comment.