2017-06-01 43 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]