簡體   English   中英

兩個Shapefile的相交區域-Python

[英]Intersection Area of Two Shapefiles - Python

我在python中有兩個shapefile,我想找到它們重疊的所有空間的區域。

我可以使用來自geopandas的sjoin來獲得它們連接的區域,但是對於有多個重疊的位置,我只想保留面積最大的區域。

municipality = gpd.read_file(muni_file)
soil_type = gpp.read_file(soil)
combined = gpd.sjoin(municipality,soil_type,how="left",op="intersects")

使用OGR,我可以得到如下的多邊形面積

from osgeo import ogr

wkt = "POLYGON ((1162440.5712740074 672081.4332727483, 1162440.5712740074 647105.5431482664, 1195279.2416228633 647105.5431482664, 1195279.2416228633 672081.4332727483, 1162440.5712740074 672081.4332727483))"
poly = ogr.CreateGeometryFromWkt(wkt)

因此,我想知道是否有辦法獲取合並的shapefile並確定兩個區域相交的區域,以便我只為每個市政當局保留最大的區域。

是的,我相信您可以通過Apply進行循環組合,並獲得每個交叉點的大小。

從合並的reindex開始,因為我假設它們是sjoin()的重復項

combined = combined.reset_index()

然后定義一個輔助函數(get_size_of_intersection),然后循環循環並應用get_size_of_intersection()並創建一個新的系列,稱為intersection_size

一些注意事項:

-將具有自治市的幾何形狀

-combined將有一個稱為index_right的列/系列,它將是土壤類型的索引

-由於這些對象是我們正在處理的勻稱對象,因此我們可以利用交集()和area屬性

def get_size_of_intersection(row, soil_type):
    return row['geometry'].intersection(soil_type['geometry'].iloc[int(row['index_right'])]).area

combined['intersection_size'] = combined.apply(lambda row : 
                                       get_size_of_intersection(row, soil_type), axis=1)

我們將創建另一個名為max_intersection_size的系列。 在這里,我假設市政當局具有某種“名稱”系列,我們可以對其進行分組並應用max()

combined['max_intersection_size'] = combined.groupby('name')['intersection_size'].transform(max)

然后使用布爾索引,我們得到所需的數據

(即,intersection_size等於max_intersection_size)

filter = combined['intersection_size'] == combined['max_intersection_size']
combined[filter]

暫無
暫無

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

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