summaryrefslogtreecommitdiff
path: root/lib/utils/geometry.py
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2022-05-03 16:58:55 -0400
committerKaalleen <reni@allenka.de>2022-05-04 19:18:33 +0200
commitaeeaf72338e2d7645309725be641d552a3c56190 (patch)
tree2d28e60d4c3f6f80ad98df506c92358074124448 /lib/utils/geometry.py
parent5a69fa3e9c582a3bc21660342cea35837ae1eb9a (diff)
wip
Diffstat (limited to 'lib/utils/geometry.py')
-rw-r--r--lib/utils/geometry.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py
index ed1e2c0e..86205f02 100644
--- a/lib/utils/geometry.py
+++ b/lib/utils/geometry.py
@@ -5,7 +5,7 @@
import math
-from shapely.geometry import LineString, LinearRing
+from shapely.geometry import LineString, LinearRing, MultiLineString, Polygon, MultiPolygon, GeometryCollection
from shapely.geometry import Point as ShapelyPoint
@@ -66,6 +66,35 @@ def reverse_line_string(line_string):
return LineString(line_string.coords[::-1])
+def ensure_multi_line_string(thing):
+ """Given either a MultiLineString or a single LineString, return a MultiLineString"""
+
+ if isinstance(thing, LineString):
+ return MultiLineString([thing])
+ else:
+ return thing
+
+
+def ensure_geometry_collection(thing):
+ """Given either some kind of geometry or a GeometryCollection, return a GeometryCollection"""
+
+ if isinstance(thing, (MultiLineString, MultiPolygon)):
+ return GeometryCollection(thing.geoms)
+ elif isinstance(thing, GeometryCollection):
+ return thing
+ else:
+ return GeometryCollection([thing])
+
+
+def ensure_multi_polygon(thing):
+ """Given either a MultiPolygon or a single Polygon, return a MultiPolygon"""
+
+ if isinstance(thing, Polygon):
+ return MultiPolygon([thing])
+ else:
+ return thing
+
+
def cut_path(points, length):
"""Return a subsection of at the start of the path that is length units long.