繁体   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