I am trying to preform a spatial join between two vector files where i have one polygon-grid and a classified vector-file, as shown below:

The two vector files used. Grid and classified data

I would like to be able to classify my grid according to the majority class in each cell, so if it is mostly red etc. Similar to zonal statistics or rasterstats but for vectordata.

I tried using GeoPandas's sjoin

gpd.sjoin(grid, classified_vector)

but the result is clearly not based on majority of each class. I have tried using different switches in sjoin but without any luck.

enter image description here

1 Answer 1


My answer is a slight extension to Spatially joining only features by largest overlap with sjoin in GeoPandas

Let's assume there are two polygon layers 'grid' and 'layer', see the image below.


Using the following code:

import geopandas as gpd

_layer = "C:/Downloads/geopandas/layer.shp"
_grid = "C:/Downloads/geopandas/grid.shp"

layer = gpd.read_file(_layer)
grid = gpd.read_file(_grid)

_overlay = gpd.overlay(grid, layer, how='intersection')

_overlay['area'] = _overlay['geometry'].area/10**6
_overlay.sort_values(by=['area'], inplace=True)
_overlay.drop_duplicates(subset='id', keep='last', inplace=True)

_overlay.drop(['fid', 'area', 'geometry'], inplace=True, axis=1)

grid_join = grid.join(_overlay.set_index('id'), on='id')


it is possible to achieve the desired output:


or a bit more clearer



  • Thanks! worked perfect!
    – gusbacos
    Nov 4, 2021 at 14:12

Your Answer

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

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