我在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和两个相交的区域放在一起,这样我只保留每个城市的最大值。
答案 0 :(得分:2)
是的,我相信你可以循环通过申请并获得每个交叉点的大小。
首先使用合并后的重新索引,假设它们与sjoin重复()
combined = combined.reset_index()
然后定义一个辅助函数(get_size_of_intersection)然后我们通过组合循环并应用get_size_of_intersection()并创建一个名为intersection_size的新系列
一些注意事项:
-combined将具有市政的几何
-combined将有一个名为index_right的列/系列,它将是soil_type的索引
- 因为这些是我们正在处理的形状对象,我们可以利用intersection()和区域属性
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]