edu*_*dub 9 python polygon shapely
我有两个在各个部分相交的匀称MultiPolygon实例(由lon,lat点组成).我正在尝试循环,确定两个多边形之间是否存在交集,然后创建一个排除该交集的新多边形.从附图中,我基本上不希望红色圆圈与黄色轮廓重叠,我希望边缘正好是黄色轮廓开始的位置.
我已经尝试按照这里的说明进行操作,但它根本不会改变我的输出,而且我不想将它们合并到一个级联联合中.我没有收到任何错误消息,但是当我将这些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)
Run Code Online (Sandbox Code Playgroud)
我想你可以使用这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)
Run Code Online (Sandbox Code Playgroud)