summaryrefslogtreecommitdiff
path: root/lib/utils/clamp_path.py
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2023-05-29 21:55:17 -0400
committerKaalleen <reni@allenka.de>2023-07-01 08:23:46 +0200
commitf9438ac9ff5886f25e4ad18271769ef77de806d0 (patch)
treef77709caf7b6232421e0788f6cbf6fbb93771291 /lib/utils/clamp_path.py
parentec68c17a05aa4a3d75fcab069a3b065f473574af (diff)
return early for paths already inside
Diffstat (limited to 'lib/utils/clamp_path.py')
-rw-r--r--lib/utils/clamp_path.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/utils/clamp_path.py b/lib/utils/clamp_path.py
index f1fc7765..f9b8991a 100644
--- a/lib/utils/clamp_path.py
+++ b/lib/utils/clamp_path.py
@@ -66,6 +66,8 @@ def find_border(polygon, point):
def clamp_path_to_polygon(path, polygon):
"""Constrain a path to a Polygon.
+ The path is expected to have at least some part inside the Polygon.
+
Description: https://gis.stackexchange.com/questions/428848/clamp-linestring-to-polygon
"""
@@ -76,6 +78,10 @@ def clamp_path_to_polygon(path, polygon):
# border and returns the pieces in the same order as the original path.
split_path = ensure_geometry_collection(LineString(path).difference(polygon.boundary))
+ if len(split_path.geoms) == 1:
+ # The path never intersects with the polygon, so it's entirely inside.
+ return path
+
# Add the start and end points to avoid losing part of the path if the
# start or end coincides with the polygon boundary
split_path = [ShapelyPoint(start), *split_path.geoms, ShapelyPoint(end)]