2017-06-01 360 views
0

我有兩個python的shapefile,我想找到它們重疊的所有空間的區域。兩個Shapefile的交集區域 - Python

我可以使用來自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) 

所以我想知道如果有一種方法把我的組合shape文件,並有區裏的兩個相交,使我只保留每個城市的最大值。

回答

1

是的,我相信你可以循環通過適用的組合,並獲得每個交叉點的大小。

開始與聯合重建索引因爲我假設他們是從sjoin重複()

combined = combined.reset_index() 

然後定義一個輔助函數(get_size_of_intersection),那麼我們將環通相結合並應用get_size_of_intersection()並創建一個新的系列名爲intersection_size

一些注意事項:

-combined將有市的幾何形狀

-combined將有一列/一系列名爲index_right這將是soil_type指數

- 由於這些都是我們正在與我們打交道可以利用路口()和小區物業勻稱的對象

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]