I have a large polygon file, small polygon file and points file. What I do here is loop through large polygons to find which small polygons intersect. Then calculate the area of each small polygon within the large one. And then I loop through the small polygons to find points statistics in each of them.
I have found number_of_somethin value in each small polygon. And the question would be how to can I sum all number_of_somethin small polygons values within the large polygon and store the results in original large_polygon file as a new column, let's say large_polygon['smth_sum']?
With df_res_2.loc[idx, 'smth'] = number_of_somethin I get number_of_somethin values in each small polygon inside the large ones. Now I need to sum them in large_polygon['smth_sum']
Note: FID is the id for large polygons and ID is the id for small polygons
import geopandas as gpd
small_polygon = gpd.read_file(r'R:\...\small.shp')
large_polygon = gpd.read_file(r'R:\...\large.shp')
points = gpd.read_file(r'R:\...\points.shp')
SmallJoin =gpd.sjoin(small_polygon, large_polygon)[['FID', 'ID', 'someValue','geometry']]
for i in large_polygon.index:
df_i = SmallJoin[SmallJoin['FID'] == i]
# i do something here, f.e. calculate small polgyon area
df_res = gpd.overlay(large_polygon, df_i, how='intersection')
df_res['area'] = round((df_res.apply(lambda row: row.geometry.area, axis=1)), 4)
# now i know area for each small polygon within large polygon
df_res_2 = df_res[df_res['FID_1'] == i]
# now point statistics in small polygons
PointsJoin =gpd.sjoin(points, df_res)[['ID','someAttribute', 'someAttribute2','geometry']]
for idx, val in df_res_2['ID'].items():
df_idx = PointsJoin[PointsJoin['ID'] == val]
number_of_somethin = df_idx ['someAttribute'] + 121 + df_idx['someAttribute2']
df_res_2.loc[idx, 'smth'] = number_of_somethin
I had a few ideas how to do this, but none of them are not wokring, so I assume that there is some other way.
large_polygon.loc[i, 'smth_sum'] = df_res_2['smth']
large_polygon.loc[i, 'smth_sum'] = df_res_2['smth'].sum()
large_polygon['smth_sum'] = large_polygon[large_polygon['FID'] == df_res_2['FID_1'].sum()]

groupby()effectively same technique that is used to calculate overlaps in this question stackoverflow.com/questions/70051743/…