通过减去与另一个多边形的交集来创建新的形状多边形

时间:2016-09-12 22:06:50

标签: python python-2.7 polygon shapely

我有两个在各个部分相交的匀称MultiPolygon实例(由lon,lat点组成)。我试图循环,确定两个多边形之间是否存在交集,然后创建一个排除该交叉点的新多边形。从附图中,我基本上不希望红色圆圈与黄色轮廓重叠,我希望边缘正好是黄色轮廓开始的位置。

我已尝试按照here的说明操作,但它根本没有改变我的输出,而且我不想将它们合并到一个级联联盟中。我没有收到任何错误消息,但是当我将这些MultiPolygons添加到KML文件(只是python中的原始文本操作,没有花哨的程序)时,它们仍然显示为圆圈而没有任何修改。

# multipol1 and multipol2 are my shapely MultiPolygons
from shapely.ops import cascaded_union
from itertools import combinations
from shapely.geometry import Polygon,MultiPolygon

outmulti = []
for pol in multipoly1:
    for pol2 in multipoly2:
        if pol.intersects(pol2)==True:
            # If they intersect, create a new polygon that is
            # essentially pol minus the intersection
            intersection = pol.intersection(pol2)
            nonoverlap = pol.difference(intersection)
            outmulti.append(nonoverlap)

        else:
            # Otherwise, just keep the initial polygon as it is.
            outmulti.append(pol)

finalpol = MultiPolygon(outmulti)

Polygon Overlap

1 个答案:

答案 0 :(得分:5)

我猜你可以在这两个多边形之间使用symmetric_difference,并通过与第二个多边形的差异来实现你想要做的事情(对称差异会带给你非 - 从两个多边形重叠的部分,通过差异删除多边形2的部分。我没有测试,但它可能看起来像:

# multipol1 and multipol2 are my shapely MultiPolygons
from shapely.ops import cascaded_union
from itertools import combinations
from shapely.geometry import Polygon,MultiPolygon

outmulti = []
for pol in multipoly1:
    for pol2 in multipoly2:
        if pol.intersects(pol2)==True:
            # If they intersect, create a new polygon that is
            # essentially pol minus the intersection
            nonoverlap = (pol.symmetric_difference(pol2)).difference(pol2)
            outmulti.append(nonoverlap)

        else:
            # Otherwise, just keep the initial polygon as it is.
            outmulti.append(pol)

finalpol = MultiPolygon(outmulti)