Geographic Information Systems Asked on March 26, 2021
I would like to join two GeoDataFrames with polygon geometries (purple and green, see screenshot below). However, I would like to get the sum of all intersecting green areas per purple feature they are intersecting with.
I tried
import geopandas as gpd
gpd.sjoin(purple, green, how="inner", op='intersects')
but that will just tell me which features are intersecting rather than returning the sum of the intersecting green areas. How would I do this in Python?
Intersect and merge results back to the green polygons:
import geopandas as gpd
dfpurple = gpd.read_file(r"C:GISdatatestdatageopandas_sjoin_interaerabuffer.shp")
dfgreen = gpd.read_file(r"C:GISdatatestdatageopandas_sjoin_interaeracounties.shp")
id_col = 'kkod' #Unique identifier to tell the green areas apart
dfpurp_diss = gpd.GeoDataFrame(gpd.GeoSeries([geom for geom in dfpurple.unary_union.geoms])) #Dissolve the purple overlaps
dfpurp_diss.geometry = dfpurp_diss[0] #Set a geometry column
dfpurp_diss.crs = dfgreen.crs #My crs was undefined
intersect = gpd.overlay(dfgreen, dfpurp_diss, how='intersection') #Intersect
intersect['area'] = intersect.geometry.area #Calculate area column
interarea = intersect.groupby(id_col)['area'].sum() #Sum per id_col
dfgreen = dfgreen.merge(right=interarea, how='left', left_on=id_col, right_index=True) #Merge/Join the data back to the original data
Correct answer by BERA on March 26, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP