summaryrefslogtreecommitdiff
path: root/lib/utils/geometry.py
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2023-12-26 09:37:30 +0100
committerGitHub <noreply@github.com>2023-12-26 09:37:30 +0100
commitd1624fdb361470f9cb3d564cc446c876ec8f7774 (patch)
tree513d2660ca7f9efd8186b6aa23349a3a67b76fdd /lib/utils/geometry.py
parent0200ba173cd2f07db033503c4791b7d46d05d773 (diff)
Contour fill: replace buffer with offset_curve (#2654)
Diffstat (limited to 'lib/utils/geometry.py')
-rw-r--r--lib/utils/geometry.py34
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py
index 7434ae27..6ef0d439 100644
--- a/lib/utils/geometry.py
+++ b/lib/utils/geometry.py
@@ -7,7 +7,7 @@ import math
import typing
import numpy
-from shapely.geometry import LineString, LinearRing, MultiLineString, Polygon, MultiPolygon, MultiPoint, GeometryCollection
+from shapely.geometry import LineString, LinearRing, MultiLineString, MultiPolygon, MultiPoint, GeometryCollection
from shapely.geometry import Point as ShapelyPoint
@@ -103,12 +103,19 @@ def reverse_line_string(line_string):
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:
+ """Given either a MultiLineString, a single LineString or GeometryCollection, return a MultiLineString"""
+ if thing.is_empty:
return thing
+ if thing.geom_type == "LineString":
+ return MultiLineString([thing])
+ if thing.geom_type == "GeometryCollection":
+ multilinestring = []
+ for line in thing.geoms:
+ if line.geom_type == "LineString":
+ multilinestring.append(line)
+ if multilinestring:
+ return MultiLineString(multilinestring)
+ return thing
def ensure_geometry_collection(thing):
@@ -124,11 +131,18 @@ def ensure_geometry_collection(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:
+ if thing.is_empty:
return thing
+ if thing.geom_type == "Polygon":
+ return MultiPolygon([thing])
+ if thing.geom_type == "GeometryCollection":
+ multipolygon = []
+ for polygon in thing.geoms:
+ if polygon.geom_type == "Polygon":
+ multipolygon.append(polygon)
+ if multipolygon:
+ return MultiPolygon(multipolygon)
+ return thing
def cut_path(points, length):