summaryrefslogtreecommitdiff
path: root/lib/utils/clamp_path.py
diff options
context:
space:
mode:
authorLex Neva <lexelby@users.noreply.github.com>2024-06-20 10:45:53 -0700
committerGitHub <noreply@github.com>2024-06-20 19:45:53 +0200
commite8017e0bcc0be2ef128bcea187f6528ed0a3d197 (patch)
tree8196bb69d5ce4fee839056f24abe181fd773afb3 /lib/utils/clamp_path.py
parentc017cae01aa33a876795ec5da1438e8ec0f11fc5 (diff)
auto-fill pull compensation (#2891)
Diffstat (limited to 'lib/utils/clamp_path.py')
-rw-r--r--lib/utils/clamp_path.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/utils/clamp_path.py b/lib/utils/clamp_path.py
index 0f58f83c..fcb34a4b 100644
--- a/lib/utils/clamp_path.py
+++ b/lib/utils/clamp_path.py
@@ -1,5 +1,6 @@
from shapely.geometry import LineString, MultiPolygon
from shapely.geometry import Point as ShapelyPoint
+from shapely.ops import nearest_points
from shapely.prepared import prep
from .geometry import (Point, ensure_geometry_collection,
@@ -14,6 +15,9 @@ def path_to_segments(path):
def segments_to_path(segments):
"""Convert a list of contiguous LineStrings into a list of Points."""
+ if not segments:
+ return []
+
coords = [segments[0].coords[0]]
for segment in segments:
@@ -68,6 +72,22 @@ def find_border(polygon, point):
return polygon.exterior
+def clamp_fully_external_path(path, polygon):
+ """Clamp a path that lies entirely outside a polygon."""
+
+ start = ShapelyPoint(path[0])
+ end = ShapelyPoint(path[-1])
+
+ start_on_outline = nearest_points(start, polygon.exterior)[1].buffer(0.01, resolution=1)
+ end_on_outline = nearest_points(end, polygon.exterior)[1].buffer(0.01, resolution=1)
+
+ border_pieces = ensure_multi_line_string(polygon.exterior.difference(MultiPolygon((start_on_outline, end_on_outline)))).geoms
+ border_pieces = fix_starting_point(border_pieces)
+ shorter = min(border_pieces, key=lambda piece: piece.length)
+
+ return adjust_line_end(shorter, start)
+
+
def clamp_path_to_polygon(path, polygon):
"""Constrain a path to a Polygon.
@@ -143,4 +163,7 @@ def clamp_path_to_polygon(path, polygon):
else:
was_inside = False
+ if not result:
+ return clamp_fully_external_path(path, polygon)
+
return segments_to_path(result)