Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX/MNT: Simplify project geometry handling #2407

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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