-
Notifications
You must be signed in to change notification settings - Fork 2
/
shapely_ext.py
337 lines (281 loc) · 12.8 KB
/
shapely_ext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Copyright 2022 Xavier
#
# This file is part of pycut.
#
# pycut is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pycut is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pycut. If not, see <http:#www.gnu.org/licenses/>.
import shapely.geometry # type: ignore [import-untyped]
import shapely.ops # type: ignore [import-untyped]
from shapely.geometry.base import JOIN_STYLE # type: ignore [import-untyped]
from shapely_utils import ShapelyUtils
from shapely_matplotlib import MatplotLibUtils
class ShapelyMultiPolygonOffset:
"""
The class to perform an offset of a polygon.
It has to take care of the possible offseted interiors of the polygon
"""
def __init__(self, multipoly: shapely.geometry.MultiPolygon):
""" """
# input
self.multipoly = multipoly
def offset(
self,
amount: float,
side: str,
consider_interiors_offsets: bool,
resolution: int,
join_style: JOIN_STYLE,
mitre_limit: float,
) -> shapely.geometry.MultiPolygon:
"""
Generate offseted polygons.
"""
polys = []
for poly in self.multipoly.geoms:
linearring = shapely.geometry.LineString(poly.exterior)
# offset of a linearring is !BUGGY!
linestring = ShapelyUtils.linearring_to_linestring(linearring)
# unfortunately this generates others PYCUT bugs !! (pocketing - FIXME - ) so reset
linestring = linearring
# cnt = MatplotLibUtils.display(
# "linestring to offset", linestring, force=True
# )
# shapely 2.0 fix : if amount = 0
if amount != 0.0:
ext_offset = linestring.parallel_offset(
amount,
side,
resolution=resolution,
join_style=join_style,
mitre_limit=mitre_limit,
)
else:
ext_offset = shapely.geometry.LineString(linestring)
# cnt = MatplotLibUtils.display(
# f"offset {side} - as LineString|MultiLineString (from linestring)",
# ext_offset,
# force=True,
# )
# simplfy resulting offset !WICHTIG!
# print("offset: ", ext_offset)
if ext_offset.geom_type == "LineString":
# cnt = MatplotLibUtils.display(
# f"offset {side} - as LineString|MultiLineString (from linestring)",
# ext_offset,
# force=True,
# )
print(
"offset length (1)= ",
ext_offset.length,
len(list(ext_offset.coords)),
)
ext_offset = ext_offset.simplify(0.005)
print(
"offset length (2)= ",
ext_offset.length,
len(list(ext_offset.coords)),
)
# cnt = MatplotLibUtils.display(
# f"offset {side} - as LineString|MultiLineString (from linestring)",
# ext_offset,
# force=True,
# )
elif ext_offset.geom_type == "MultiLineString":
lines = []
for line in ext_offset.geoms:
# print("offset length (1)= ", line.length, len(list(line.coords)))
s_line = line.simplify(0.005)
# print("offset length (2)= ", s_line.length, len(list(s_line.coords)))
lines.append(s_line)
ext_offset = shapely.geometry.MultiLineString(lines)
# from the offseted lines, build a multipolygon that we diff with the interiors
exterior_multipoly = ShapelyUtils.build_multipoly_from_offsets([ext_offset])
# print("exterior_multipoly VALID ? ", exterior_multipoly.is_valid)
# MatplotLibUtils.display(
# "geom multipoly from ext offset", exterior_multipoly, force=True
# )
# now consider the interiors
if poly.interiors:
interior_polys = []
for interior in poly.interiors:
ipoly = shapely.geometry.Polygon(interior)
# simplify the polygon
# this may be important so that the offset becomes Ok (example: tudor [AD])
# where of offset is a MultiLineString instead of a Linestring
ipoly = ipoly.simplify(0.001)
ipoly = shapely.geometry.polygon.orient(ipoly)
interior_polys.append(ipoly)
interior_multipoly = ShapelyUtils.build_multipoly_from_list_of_polygons(
interior_polys
)
# cnt = MatplotLibUtils.display(
# "starting interior offset from", interior_multipoly, force=True
# )
# consider interiors offsets
if consider_interiors_offsets == True:
interior_multipoly = ShapelyUtils.offset_multipolygon(
interior_multipoly, amount, "right"
)
# cnt = MatplotLibUtils.display(
# "interior first offset", interior_multipoly, force=True
# )
# the diff is the solution
try:
# if not exterior_multipoly.is_valid():
# cnt = MatplotLibUtils.display(
# "diff ext", exterior_multipoly, force=True
# )
# if not interior_multipoly.is_valid():
# cnt = MatplotLibUtils.display(
# "diff int", interior_multipoly, force=True
# )
sol_poly = exterior_multipoly.difference(interior_multipoly)
# cnt = MatplotLibUtils.display(
# "diff of interior offseting", sol_poly, force=True
# )
except Exception as e:
print("ERROR difference")
print(e)
print("exterior_multipoly VALID ?", exterior_multipoly.is_valid)
print("interior_multipoly VALID ?", interior_multipoly.is_valid)
raise
if sol_poly.geom_type == "Polygon":
polys.append(sol_poly)
elif sol_poly.geom_type == "MultiPolygon":
for geom in sol_poly.geoms:
polys.append(geom)
elif sol_poly.geom_type == "GeometryCollection":
for geom in sol_poly.geoms:
if geom.geom_type == "Polygon":
polys.append(geom)
elif geom.geom_type == "MultiPolygon":
for geomc in sol_poly.geoms:
polys.append(geomc)
else: # polygon without interiors
for poly in exterior_multipoly.geoms:
if poly.geom_type == "Polygon":
polys.append(poly)
o_multipoly = ShapelyUtils.build_multipoly_from_list_of_polygons(polys)
# ensure orientation
o_multipoly = ShapelyUtils.orient_multipolygon(o_multipoly)
return o_multipoly
class ShapelyMultiPolygonOffsetInteriors:
"""
The class to perform an offset 'right' (goint to the exterior) of the interior of a polygon.
It has to take care of the offseted exterior of the polygon
"""
def __init__(self, multipoly: shapely.geometry.MultiPolygon):
""" """
self.multipoly = multipoly
def offset(
self,
amount: float,
side: str,
consider_exteriors_offsets: bool,
resolution: int,
join_style: int,
mitre_limit: float,
) -> shapely.geometry.MultiPolygon | None:
"""
main method
"""
polys = []
MatplotLibUtils.display("geometry", self.multipoly, force=False)
for poly in self.multipoly.geoms:
if not poly.interiors:
continue
linestrings = []
for interior in poly.interiors:
linestring = shapely.geometry.LineString(interior)
linestrings.append(linestring)
int_offsets = []
for linestring in linestrings:
# shapely 2.0 fix : if amount = 0
if amount != 0.0:
int_offset = linestring.parallel_offset(
amount,
side,
resolution=resolution,
join_style=join_style,
mitre_limit=mitre_limit,
)
else:
int_offset = shapely.geometry.LineString(linestring)
int_offsets.append(int_offset)
# from the offseted lines, build a multipolygon that we will diff with the exterior
interior_multipoly = ShapelyUtils.build_multipoly_from_offsets(int_offsets)
MatplotLibUtils.display(
"interior_multipoly", interior_multipoly, force=False
)
if not interior_multipoly.is_valid:
interior_multipoly = ShapelyUtils.fix_multipoly(interior_multipoly)
exterior_multipoly = ShapelyUtils.offset_multipolygon(
self.multipoly, amount, "left", consider_interiors_offsets=True
)
MatplotLibUtils.display(
"exterior_multipoly", exterior_multipoly, force=False
)
# only exterior
exterior_multipoly = ShapelyUtils.remove_multipoly_holes(exterior_multipoly)
# this simplify may be important so that the offset becomes Ok (example: letter "B")
exterior_multipoly = ShapelyUtils.simplify_multipoly(
exterior_multipoly, 0.001
)
exterior_multipoly = ShapelyUtils.orient_multipolygon(exterior_multipoly)
MatplotLibUtils.display(
"exterior_multipoly", exterior_multipoly, force=False
)
if consider_exteriors_offsets == True:
# the diff ** with ~POLY ** is the solution
try:
o_interior_is_contained_in_o_exterior = exterior_multipoly.contains(
interior_multipoly
)
print(
"XXXXXX offset -> interior_offset_is_contained_in_exterior_offset",
o_interior_is_contained_in_o_exterior,
)
if o_interior_is_contained_in_o_exterior:
sol_poly = exterior_multipoly.intersection(interior_multipoly)
else:
# big problem!
sol_poly = (
interior_multipoly # bug: can cut outside the exterior...
)
sol_poly = exterior_multipoly # TEST -> GOOD !
except Exception as e:
print("ERROR intersection")
print(e)
print("interior_multipoly VALID ?", interior_multipoly.is_valid)
print("exterior_multipoly VALID ?", exterior_multipoly.is_valid)
raise
if sol_poly.geom_type == "Polygon":
polys.append(sol_poly)
elif sol_poly.geom_type == "MultiPolygon":
for geom in sol_poly.geoms:
polys.append(geom)
elif sol_poly.geom_type == "GeometryCollection":
for geom in sol_poly.geoms:
if geom.geom_type == "Polygon":
polys.append(geom)
elif geom.geom_type == "MultiPolygon":
for poly in geom.geoms:
polys.append(poly)
else: # without exterior
for poly in interior_multipoly.geoms:
if poly.geom_type == "Polygon":
polys.append(poly)
o_multipoly = ShapelyUtils.build_multipoly_from_list_of_polygons(polys)
# ensure orientation
o_multipoly = ShapelyUtils.orient_multipolygon(o_multipoly)
return o_multipoly