簡體   English   中英

通過減去與另一個多邊形的交集來創建新的形狀多邊形

[英]Create new shapely polygon by subtracting the intersection with another polygon

我有兩個在各個部分相交的勻稱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)

多邊形重疊

我想你可以使用這兩個多邊形之間的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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM