在 Python 中将多多边形转换为多边形 [英] Convert Multipolygon to Polygon in Python
问题描述
是否可以使用
一个近似的方法可以是:
- 提取感兴趣的
MultiPolygon
的各个组件的外边界 - 扩大-收缩每个外边界以填充这些边界大致包含的洞",例如,处理带切口的甜甜圈"
- 合并上一步获得的所有几何图形
例如:
#!/usr/bin/env pythonfrom shapely.geometry import MultiPolygon, Polygon从 shapely.ops 导入级联_联合a = 0.25增量 = 0.49P = 多多边形([(((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))])])每股收益 = 0.01欧米茄 = 级联_联合([P 中的组件的 Polygon(component.exterior).buffer(eps).buffer(-eps)])对于 zip(*omega.exterior.coords.xy) 中的 x,y:打印(x,y)
MultiPolygon
P
看起来像:
虽然上面列出的脚本按预期生成了边长为 3 的近似正方形,即,它填充了左下角的孔以及 MultiPolygon<中心的空白空间"/code> 等效于扩展-收缩过程中的一个洞,参数
eps
的值足够高.
Is it possible to convert a Multipolygon
into a Polygon
which fills all holes or missing inner areas using Shapely? I have been trying since a while but I can't find it in the documentation. The following image I show an example of a multipolygon with those holes I want to fill, and those squares I want to remove.
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
.
这篇关于在 Python 中将多多边形转换为多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!