diff options
Diffstat (limited to 'lib/utils')
| -rw-r--r-- | lib/utils/clamp_path.py | 9 | ||||
| -rw-r--r-- | lib/utils/geometry.py | 22 |
2 files changed, 27 insertions, 4 deletions
diff --git a/lib/utils/clamp_path.py b/lib/utils/clamp_path.py index f6db66a8..0f58f83c 100644 --- a/lib/utils/clamp_path.py +++ b/lib/utils/clamp_path.py @@ -1,6 +1,9 @@ -from shapely.geometry import LineString, Point as ShapelyPoint, MultiPolygon +from shapely.geometry import LineString, MultiPolygon +from shapely.geometry import Point as ShapelyPoint from shapely.prepared import prep -from .geometry import Point, ensure_geometry_collection + +from .geometry import (Point, ensure_geometry_collection, + ensure_multi_line_string) def path_to_segments(path): @@ -122,7 +125,7 @@ def clamp_path_to_polygon(path, polygon): if not exit_point.intersects(entry_point): # Now break the border into pieces using those points. border = find_border(polygon, exit_point) - border_pieces = border.difference(MultiPolygon((entry_point, exit_point))).geoms + border_pieces = ensure_multi_line_string(border.difference(MultiPolygon((entry_point, exit_point)))).geoms border_pieces = fix_starting_point(border_pieces) # Pick the shortest way to get from the exiting to the diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py index 739f9660..24cf8459 100644 --- a/lib/utils/geometry.py +++ b/lib/utils/geometry.py @@ -8,7 +8,7 @@ import typing import numpy from shapely.geometry import (GeometryCollection, LinearRing, LineString, - MultiLineString, MultiPolygon) + MultiLineString, MultiPoint, MultiPolygon) from shapely.geometry import Point as ShapelyPoint @@ -159,6 +159,26 @@ def ensure_multi_polygon(thing, min_size=0): return multi_polygon +def ensure_multi_point(thing): + """Given a shapely geometry, return a MultiPoint""" + multi_point = MultiPoint() + if thing.is_empty: + return multi_point + if thing.geom_type == "MultiPoint": + return thing + elif thing.geom_type == "Point": + return MultiPoint([thing]) + elif thing.geom_type == "GeometryCollection": + points = [] + for shape in thing.geoms: + if shape.geom_type == "Point": + points.append(shape) + elif shape.geom_type == "MultiPoint": + points.extend(shape.geoms) + return MultiPoint(points) + return multi_point + + def cut_path(points, length): """Return a subsection of at the start of the path that is length units long. |
