[英]Intersection Area of 'n' shapefiles - Python
I have 'n' shapefiles and where they intersect i need to sum a specific attribute.我有'n'个shapefile,它们相交的地方我需要总结一个特定的属性。 In some points they all intersects, in other points only some of them intersects while in others i don't have any intersection.
在某些点上,它们都相交,在其他点上,只有其中一些相交,而在其他点上,我没有任何相交。 My final results would be a single shapefile (union of the 'n' shapefiles') where for each intersection area i get the sum of a specific attribute.
我的最终结果将是一个单一的 shapefile(“n”个 shapefile 的联合),对于每个交叉区域,我得到一个特定属性的总和。 Is there an easy way to do it, without converting to raster?
有没有一种简单的方法可以做到这一点,而无需转换为栅格? I already tried with the Geopandas "Overlay" function but without iterating many times i can only get the area where all the shapefiles intersect and not where only some of them intersect.
我已经尝试过使用 Geopandas “Overlay” function 但没有多次迭代,我只能得到所有 shapefile 相交的区域,而不是只有其中一些相交的区域。
you can find the shapefiles here: http://www.mediafire.com/folder/2ckfxkfda0asm/shapes您可以在此处找到 shapefile: http://www.mediafire.com/folder/2ckfxkfda0asm/shapes
import pandas as pd
import geopandas as gpd
g1 = gpd.GeoDataFrame.from_file("shape1.shp")
g2 = gpd.GeoDataFrame.from_file("shape2.shp")
g3 = gpd.GeoDataFrame.from_file("shape3.shp")
g1['number'] = pd.to_numeric(g1['number'])
g2['number'] = pd.to_numeric(g2['number'])
g3['number'] = pd.to_numeric(g3['number'])
df = pd.concat([g1,g2,g3])
intersection = g1
simmetric = g1
lista = [g2,g3]
for i in lista:
intersection = gpd.overlay(intersection, i, how='intersection').fillna(0)
intersection.index = pd.RangeIndex(len(intersection.index))
intersection.loc[:,'number'] = intersection.loc[:,'number_1'].add(intersection.loc[:,'number_2'])
intersection.drop(["number_1", "number_2"], axis = 1, inplace = True)
simmetric = gpd.overlay(simmetric, i, how='symmetric_difference').fillna(0)
simmetric.loc[:,'number'] = simmetric.loc[:,'number_1'].add(simmetric.loc[:,'number_2'])
simmetric.drop(["number_1", "number_2"], axis = 1, inplace = True)
final_result = pd.concat([simmetric,intersection])
final_result.to_file("result.shp")
I think the problem is that you did not properly save intersection
and simmertric
an output geodataframe.我认为问题在于您没有正确保存
intersection
并simmertric
一个 output 地理数据框。 You would have to initialize final_result
before the for loop, and concat with itself inside the for loop.您必须在 for 循环之前初始化
final_result
,并在 for 循环内与自身连接。
from shapely.geometry import Polygon
import geopandas as gpd
import pandas as pd
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(-1,-1), (1,-1), (1,1), (-1,1)]),
Polygon([(1,1), (3,1), (3,3), (1,3)])])
polys3 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)]),
Polygon([(6,6), (8,6),(8,8),(6,8)])])
df1 = gpd.GeoDataFrame({'geometry': polys1, 'rate_cars':[10,20]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'rate_cars':[100,200]})
df3 = gpd.GeoDataFrame({'geometry': polys3, 'rate_cars':[1,2,3]})
merge = pd.concat([df1,df2, df3],ignore_index=True)
out = gpd.GeoDataFrame()
for df in [df1,df2,df3]:
#find which records dont come from the current iteration
mask = merge.geom_equals(df)
tmp = merge.copy()
tmp = tmp[~mask]
#find any non intersection features
mask1 = tmp.intersects(df)
#non intersectiong features
non_inter = gpd.overlay(tmp, df, how='symmetric_difference')
non_inter.loc[:,'total'] = non_inter.sum(axis=1)
non_inter = non_inter.drop(['rate_cars_1', 'rate_cars_2'], axis=1)
non_inter = non_inter[['total', 'geometry']]
non_inter.loc[:,'grid_feature'] = 'street'
non_inter = non_inter[['total','grid_feature','geometry']]
#find intersections
res_intersection = gpd.overlay(tmp, df, how='intersection')
res_intersection.loc[:,'total'] = res_intersection.sum(axis=1)
res_intersection = res_intersection.drop(['rate_cars_1', 'rate_cars_2'], axis=1)
res_intersection = res_intersection[['total', 'geometry']]
res_intersection.loc[:,'grid_feature'] = 'intersection'
res_intersection = res_intersection[['total','grid_feature','geometry']]
out = pd.concat([out, res_intersection, non_inter])
out = out.drop_duplicates()
Where out returns
:从哪里
out returns
:
total grid_feature geometry
0 110.0 intersection POLYGON ((0.00000 1.00000, 1.00000 1.00000, 1....
1 210.0 intersection POLYGON ((1.00000 1.00000, 1.00000 2.00000, 2....
2 11.0 intersection POLYGON ((1.00000 1.00000, 1.00000 2.00000, 2....
3 220.0 intersection POLYGON ((2.00000 3.00000, 3.00000 3.00000, 3....
4 21.0 intersection POLYGON ((2.00000 3.00000, 3.00000 3.00000, 3....
5 22.0 intersection POLYGON ((3.00000 3.00000, 3.00000 4.00000, 4....
0 100.0 street POLYGON ((-1.00000 -1.00000, -1.00000 1.00000,...
1 200.0 street MULTIPOLYGON (((1.00000 2.00000, 1.00000 3.000...
2 1.0 street MULTIPOLYGON (((1.00000 2.00000, 1.00000 3.000...
3 2.0 street POLYGON ((3.00000 4.00000, 3.00000 5.00000, 5....
4 3.0 street POLYGON ((6.00000 6.00000, 6.00000 8.00000, 8....
5 10.0 street MULTIPOLYGON (((0.00000 1.00000, 0.00000 2.000...
6 20.0 street MULTIPOLYGON (((2.00000 3.00000, 2.00000 4.000...
0 110.0 intersection POLYGON ((0.00000 0.00000, 0.00000 1.00000, 1....
1 200.0 intersection POLYGON ((-1.00000 -1.00000, -1.00000 1.00000,...
2 210.0 intersection POLYGON ((1.00000 2.00000, 2.00000 2.00000, 2....
3 220.0 intersection POLYGON ((2.00000 2.00000, 2.00000 3.00000, 3....
4 400.0 intersection POLYGON ((1.00000 1.00000, 1.00000 3.00000, 3....
5 201.0 intersection POLYGON ((1.00000 1.00000, 1.00000 3.00000, 3....
1 20.0 street POLYGON ((2.00000 3.00000, 2.00000 4.00000, 4....
2 2.0 street POLYGON ((3.00000 3.00000, 3.00000 5.00000, 5....
0 11.0 intersection POLYGON ((1.00000 2.00000, 2.00000 2.00000, 2....
1 21.0 intersection POLYGON ((2.00000 2.00000, 2.00000 3.00000, 3....
3 2.0 intersection POLYGON ((1.00000 1.00000, 1.00000 3.00000, 3....
4 22.0 intersection POLYGON ((3.00000 4.00000, 4.00000 4.00000, 4....
5 4.0 intersection POLYGON ((3.00000 3.00000, 3.00000 5.00000, 5....
6 6.0 intersection POLYGON ((6.00000 6.00000, 6.00000 8.00000, 8....
0 10.0 street POLYGON ((0.00000 0.00000, 0.00000 2.00000, 1....
2 100.0 street POLYGON ((-1.00000 -1.00000, -1.00000 1.00000,...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.