An approximate approach could be to:
- extract the outer boundary of individual components of the
MultiPolygon
of interest
- expand-shrink each of the outer boundaries in order to fill "holes" which these boundaries approximately encompass, e.g., to handle a "doughnut with a cut"
- merge all geometries obtained in previous step
For example:
#!/usr/bin/env python
from shapely.geometry import MultiPolygon, Polygon
from shapely.ops import cascaded_union
a = 0.25
delta = 0.49
P = MultiPolygon([
(
((0,0),(0,3),(3,3),(3,2-delta),(2,2-delta),(2,2),(1,2),(1,1),(2,1),(2,1+delta),(3,1+delta),(3,0),(0,0)),
[((a, a), (1-a,a), (1-a,1-a), (a,1-a), (a,a))]
)
])
eps = 0.01
omega = cascaded_union([
Polygon(component.exterior).buffer(eps).buffer(-eps) for component in P
])
for x,y in zip(*omega.exterior.coords.xy):
print(x, y)
The MultiPolygon
P
looks like:
while the script listed above produces as expected an approximate square with side of length 3, i.e., it fills the hole in the lower-left corner as well as the "empty space" in the center of the MultiPolygon
which is rendered equivalent to a hole in the expand-shrink procedure with sufficient high value of parameter eps
.