【问题标题】:Create new shapely polygon by subtracting the intersection with another polygon通过减去与另一个多边形的交点来创建新的匀称多边形
【发布时间】:2017-01-20 10:23:57
【问题描述】:

我有两个在不同部分相交的形状优美的 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)

【问题讨论】:

  • 我投票结束这个问题为“不可重现”,因为问题中的代码是正确和 OP commented 错误在别处。

标签: python polygon shapely


【解决方案1】:

我猜你可以在这两个多边形之间使用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)

【讨论】:

  • 谢谢,这有帮助!我意识到这个错误部分是因为我后来在我的代码中设置了一个不同的循环......糟糕。
  • 知道如何将重叠部分保持为多边形
  • @Faisal 根据我的理解(并且没有更多信息)我猜你只是想要交集(所以这个问题的代码类似于pol.intersection(pol2)
猜你喜欢
  • 2013-12-11
  • 2013-01-05
  • 1970-01-01
  • 2015-08-08
  • 2013-01-19
  • 2011-08-07
  • 1970-01-01
  • 2016-09-02
  • 2018-03-09
相关资源
最近更新 更多