'n'个形状文件的交集区域-Python

时间:2019-11-21 11:02:19

标签: python shapefile geopandas

我有'n'个shapefile,它们相交的位置需要加总一个特定的属性。在某些点上它们都相交,在另一些点上它们中只有一些相交,而在另一些点我没有任何相交。我的最终结果将是单个shapefile(“ n” shapefile的联合),其中对于每个交集区域,我都会得到特定属性的总和。有没有一种简单的方法,而无需转换为栅格?我已经尝试过Geopandas的“覆盖”功能,但是如果不进行多次迭代,我只能得到所有shapefile相交的区域,而不能只看到其中一些相交的区域。

您可以在此处找到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")

example

1 个答案:

答案 0 :(得分:0)

我认为问题在于您没有正确保存intersectionsimmertric输出地理数据框。您必须在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()  


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,...