I have a polygon vector layer with potential sample plot locations across a larger ROI. Using python's geopandas, I can randomly select a subsample of polygons from the layer based on the total number of polygons, but I want to add a condition such that no two polygons in the selection share a boundary edge (corners are OK).

I have two ideas about how to do this, but need some help implementing either one.

  1. Take the random subsample, check the whole dataset for if the condition is met, then keep taking new random subsamples until a suitable sample is chosen. (this could be an endless loop or take many iterations depending on the data)

  2. Take the random sample, check each feature in the sample for if the condition is met, then resample individual features which violate the rule until the subsample meets the condition. (seems like a better approach, but I don't know how to make it happen)


import geopandas as gpd

#read in shapefile layer
grid = gpd.read_file('grid.shp')

#take subsample of 25% of squares
subsample = grid.sample(n=int(len(grid)*0.25))

Original Layer:

enter image description here

My Result:

enter image description here

Desired Result:

enter image description here

  • 1
    Looks like you need to check if two vertices touch in order to distinguish between corner touching vs. vertical/horizontal touching.
    – Binx
    Sep 29, 2021 at 21:05

1 Answer 1


I'd use 'weeding' procedure explained in Location optimization post to create subset, where polygons do not share boundaries.

![enter image description here

enter image description here

Or even simpler 'Graph Coloring' algorithm available in networkx module:

![enter image description here

  • Thanks @FelixIP , I think I will try to use the number of neighboring polygons to constrain weed the 'worst offenders' similar to your first example.
    – Kartograaf
    Sep 29, 2021 at 23:39
  • 2
    You'll need use shared edges between polygons as graph edges, this will treat polygons with single shared vertex as not connected to each other, which is what you want.
    – FelixIP
    Sep 29, 2021 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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