Skip to content

Commit

Permalink
Merge pull request #2407 from greglucas/multi-line-return
Browse files Browse the repository at this point in the history
FIX/MNT: Simplify project geometry handling
  • Loading branch information
dopplershift authored Jul 8, 2024
2 parents 009196f + c4f2087 commit ca2832e
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 ca2832e

Please sign in to comment.