两个shapefile的相交区域-Python

两个shapefile的相交区域-Python,python,geopandas,Python,Geopandas,我在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,

我在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)

因此,我想知道是否有一种方法可以使用我的组合形状文件,并拥有两个相交的区域,以便我只保留每个市政的最大值

是的,我相信您可以通过应用循环通过组合,并获得每个交叉口的大小

从combined的reindex开始,因为我假设它们是来自sjoin()的重复项

然后定义一个辅助函数(get_size_of_intersection),然后我们将循环组合并应用get_size_of_intersection(),并创建一个名为intersection_size的新系列

filter = combined['intersection_size'] == combined['max_intersection_size']
combined[filter]
一些注意事项:

-组合将具有城市的几何结构

-合并后将有一列/系列称为index_right,这将是土壤类型的索引

-因为这些是我们正在处理的形状优美的对象,所以我们可以利用intersection()和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的reindex开始,因为我假设它们是来自sjoin()的重复项

然后定义一个辅助函数(get_size_of_intersection),然后我们将循环组合并应用get_size_of_intersection(),并创建一个名为intersection_size的新系列

filter = combined['intersection_size'] == combined['max_intersection_size']
combined[filter]
一些注意事项:

-组合将具有城市的几何结构

-合并后将有一列/系列称为index_right,这将是土壤类型的索引

-因为这些是我们正在处理的形状优美的对象,所以我们可以利用intersection()和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()

然后使用布尔索引,我们得到我们想要的数据

(即交叉点大小等于最大交叉点大小)