1

I have grid map that contains cells. Each cell is represented as a row in the dataframe, and each row contains the average population "pop" attribute.

enter image description here

What I need is that for each of these cells, calculate the sum of "pop" of every other cell including itself within a set radius (200m in this case), and assign that sum to a new attribute "coverage".

enter image description here

For example, the yellow dot will represent cell x. Cell x will be given a new attribute "coverage" which gets the sum of all "pop" including itself within the red circle. How do I then do this for all cells?

1
  • 2
    What have you tried?
    – Taras
    Nov 30, 2021 at 9:26

1 Answer 1

2

You can buffer your cells to create the coverage areas, then use a spatial join to link each coverage area with the cells it contains.

import geopandas
from shapely.geometry import Point

# create example data
cells = geopandas.GeoDataFrame({
    'index': [0,1,2,3],
    'pop': [1,2,3,4],
    'geometry': [
        Point([0,0]),
        Point([0,1]),
        Point([1,0]),
        Point([1,1])
    ]
}).set_index('index')

# create "coverage" buffers
coverage = cells[['geometry']].copy()
# take care here - your data should be in a projected CRS
# if you want to interpret this as a distance in meters
coverage['geometry'] = coverage.buffer(1.1) 

# join cells with coverage, then groupby and sum
cell_coverage_pop = cells.sjoin(coverage, predicate='within') \
    .groupby('index_right') \
    .sum()
# rename index and column name
cell_coverage_pop.index.rename('index', inplace=True)
cell_coverage_pop.rename(columns={'pop': 'coverage'}, inplace=True)
# cell_coverage_pop has "index" index and "coverage" 
# with population total

# join back to original cells if desired
cells = cells.join(cell_coverage_pop)
# cells now has "index" index, and original "pop", original 
# "geometry" and total "coverage"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.