Skip to content

Commit

Permalink
FIX/TST: Use nearby boundary rather than extended segments
Browse files Browse the repository at this point in the history
We don't "project" by a factor of a two to find the boundary
intersection, rather we project along the boundary to the nearest
point. So really we just want to make sure that our cuts are "close"
to the boundary, but we don't care about the segments being extended
by a given fraction.

This is relevant when the final two coordinates are close to each
other and thus that segment is not projected very far, yet it is
quite close to the boundary.
  • Loading branch information
greglucas committed Jun 9, 2023
1 parent c3f19cc commit 8b0b7eb
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/cartopy/tests/test_linear_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@ def test_cuts(self):
assert len(multi_line_string.geoms) > 1
assert not rings

def assert_intersection_with_boundary(segment_coords):
# Double the length of the segment.
start = segment_coords[0]
end = segment_coords[1]
end = [end[i] + 2 * (end[i] - start[i]) for i in (0, 1)]
extended_segment = sgeom.LineString([start, end])
# And see if it crosses the boundary.
intersection = extended_segment.intersection(projection.boundary)
assert not intersection.is_empty, 'Bad topology near boundary'

# Each line resulting from the split should start and end with a
# segment that crosses the boundary when extended to double length.
def assert_close_to_boundary(xy):
# Are we close to the boundary, which we are considering within
# a fraction of the x domain limits
limit = (projection.x_limits[1] - projection.x_limits[0]) / 1e4
assert sgeom.Point(*xy).distance(projection.boundary) < limit, \
'Bad topology near boundary'

# Each line resulting from the split should be close to the boundary.
# (This is important when considering polygon rings which need to be
# attached to the boundary.)
for line_string in multi_line_string.geoms:
coords = list(line_string.coords)
assert len(coords) >= 2
assert_intersection_with_boundary(coords[1::-1])
assert_intersection_with_boundary(coords[-2:])
assert_close_to_boundary(coords[0])
assert_close_to_boundary(coords[-1])

def test_out_of_bounds(self):
# Check that a ring that is completely out of the map boundary
Expand Down

0 comments on commit 8b0b7eb

Please sign in to comment.