使用LineString拆分多多边形后对多边形分组

2024-10-02 18:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望通过一条线串拆分一个多多边形(代表一个有岛屿的国家),从而将该县一分为二

from shapely.ops import split
collection_of_polyogns = split(country,line)

此结果是GeometryCollection对象中的一组多边形。如何将结果分组为两个多多边形对象,每个对象都包含各自一半的多边形

更新

问题是: Determine the "left" and "right" side of a split shapely geometry提供了一个很好的解决方案,其中从结果中的每个多边形中提取一个点,以查看它与拆分线串组合时是否形成顺时针或逆时针线串。但是我想用质心代替每个多边形,因为它保证不在分割线上


Tags: of对象fromimport代表国家多边形country
1条回答
网友
1楼 · 发布于 2024-10-02 18:22:28

因此,首先要创建多多边形,然后拆分多边形:

from shapely.ops import split
from shapely.geometry import Point, LineString, Polygon, MultiPolygon
#Somewhere you get your country and your line vars
collection_of_polygons = split(country,line)

然后,将国家/地区的原始边界绘制为多边形,并使用直线执行相同的分割:

bounds = country.bounds
p1 = Point(bounds[0],bounds[-1])
p2 = Point(bounds[0],bounds[1])
p3 = Point(bounds[-2],bounds[1])
p4 = Point(bounds[-2],bounds[-1])

point_list = [p1, p2, p3, p4, p1]
bounds_poly = Polygon(point_list)

boundaries = shapely.ops.split(bounds_poly,line)

制作两个列表,并在分割的国家多边形上迭代,以检查它们是否位于边界分割多边形的第一个多边形内:

group1, group2 = [],[]
for x in collection_of_polygons:
    if x.within(boundaries[0]):
        group1.append(x)
    else:
        group2.append(x)

最后,将两个列表指定为两个不同的多多边形:

group_multi1 = MultiPolygon(group1)
group_multi2 = MultiPolygon(group2)

相关问题 更多 >