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 d8fcd1d
Show file tree
Hide file tree
Showing 2 changed files with 10 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
7 changes: 7 additions & 0 deletions lib/cartopy/tests/test_line_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def test_out_of_domain_efficiency(self):
tgt_proj.project_geometry(line_string, src_proj)
assert time.time() < cutoff_time, 'Projection took too long'

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 d8fcd1d

Please sign in to comment.