Skip to content

Commit

Permalink
FIX: Handle invalid geometries for rectangular projections
Browse files Browse the repository at this point in the history
Rectangular projections have an extra fast-path check to see
if points can be transformed faster. This check requires valid
geometries though, so ignore this case when dealing with invalid
geometries.
  • Loading branch information
greglucas committed Oct 14, 2023
1 parent 53260b6 commit ee56e62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/cartopy/tests/test_line_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ def test_nan_end(self):
assert not any(np.isnan(coord)), \
'Unexpected NaN in projected coords.'

def test_nan_rectangular(self):
# Make sure rectangular projections can handle invalid geometries
projection = ccrs.Robinson()
line_string = sgeom.LineString([(0, 0), (1, 1), (np.nan, np.nan),
(2, 2), (3, 3)])
multi_line_string = projection.project_geometry(line_string,
ccrs.PlateCarree())
assert len(multi_line_string.geoms) == 2


class TestMisc:
def test_misc(self):
Expand Down
5 changes: 4 additions & 1 deletion lib/cartopy/trace.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,10 @@ def project_linear(geometry not None, src_crs not None,
cdef bool geom_fully_inside = False
if isinstance(dest_projection, (ccrs._RectangularProjection, ccrs._WarpedRectangularProjection)):
dest_line = sgeom.LineString([(x[0], x[1]) for x in dest_coords])
geom_fully_inside = gp_domain.covers(dest_line)
if dest_line.is_valid:
# We can only check for covers with valid geometries
# some have nans/infs at this point still
geom_fully_inside = gp_domain.covers(dest_line)

lines = LineAccumulator()
for src_idx in range(1, src_size):
Expand Down

0 comments on commit ee56e62

Please sign in to comment.