简体   繁体   English

两个Shapefile的相交区域-Python

[英]Intersection Area of Two Shapefiles - Python

I have two shapefiles in python and I would like to find the area of all spaces they overlap. 我在python中有两个shapefile,我想找到它们重叠的所有空间的区域。

I can use sjoin from geopandas to get areas where they join, but for locations where there are multiple overlaps I would like to only keep the one with the largest area. 我可以使用来自geopandas的sjoin来获得它们连接的区域,但是对于有多个重叠的位置,我只想保留面积最大的区域。

municipality = gpd.read_file(muni_file)
soil_type = gpp.read_file(soil)
combined = gpd.sjoin(municipality,soil_type,how="left",op="intersects")

With OGR I can get the area of a polygon as below 使用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)

So I am wondering if there is a way to take my combined shapefile and have the area where the two intersect so that I only keep the maximum one for each municipality. 因此,我想知道是否有办法获取合并的shapefile并确定两个区域相交的区域,以便我只为每个市政当局保留最大的区域。

Yes, I believe you can loop thru combined via apply and get the size of each of the intersections. 是的,我相信您可以通过Apply进行循环组合,并获得每个交叉点的大小。

start with reindex of combined as I'm assuming they're are duplicates from sjoin() 从合并的reindex开始,因为我假设它们是sjoin()的重复项

combined = combined.reset_index()

Then define a helper function (get_size_of_intersection) then we'll loop thru combined and apply get_size_of_intersection() and create a new series called intersection_size 然后定义一个辅助函数(get_size_of_intersection),然后循环循环并应用get_size_of_intersection()并创建一个新的系列,称为intersection_size

some notes: 一些注意事项:

-combined will have the geometry of municipality -将具有自治市的几何形状

-combined will have a column/series called index_right which will be the index of soil_type -combined将有一个称为index_right的列/系列,它将是土壤类型的索引

-since these are shapely objects we're dealing with we can leverage intersection() and the area property -由于这些对象是我们正在处理的匀称对象,因此我们可以利用交集()和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)

We'll create another series called max_intersection_size. 我们将创建另一个名为max_intersection_size的系列。 Here I'm assuming municipality has some sort of 'name' series that we can group on and apply max() 在这里,我假设市政当局具有某种“名称”系列,我们可以对其进行分组并应用max()

combined['max_intersection_size'] = combined.groupby('name')['intersection_size'].transform(max)

Then using boolean indexing we get our desired data 然后使用布尔索引,我们得到所需的数据

(ie where intersection_size equals max_intersection_size) (即,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