3

For my process I make a spatial join between a polygon grid and points :

join = gpd.sjoin(rotate_grid, gdf, op='contains')

It's look like this.

enter image description here

the result is a grid where each square of my grid is multiplied by as many points that are contained.

enter image description here

My goal is to have only one square and mean of a particular column, NdviM. First I just tried to dissolve square without mean calcul with

dissol = join.dissolve(by='geometry')

But it returns me KeyError: 'geometry'

Somebody knows how to do this ?

1 Answer 1

6

Pandas and geopandas allows you to group by attributes and aggregate them:

# we'll make a string column for the wKT geom
gdf['WKT'] = gdf['geometry'].apply(lambda x: str(x))

grouped_gdf = gdf.groupby('WKT').mean().reset_index()
result_gdf  = grouped_gdf[['WKT', 'your_column']]

# then rebuild geometry from WKT
from shapely.wkt import loads

result_gdf['geometry'] = result_gdf['WKT'].apply(lambda x: loads(x))
result_gdf = geopandas.GeoDataFrame(result_gdf)
1
  • 1
    It works great, awesome !
    – Tim C.
    Commented Jan 30, 2018 at 16:02

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.