2

I have several polygons with holes and I would like to fill the gaps with sizes smaller than a certain area

import geopandas as gpd
from shapely.geometry import Polygon

# Initializing a polygon with two holes
# See: https://stackoverflow.com/questions/48770822/how-to-make-holes-in-a-polygon-in-shapely-python-having-polygons
outer = Polygon(((0,0),(20,0),(20,20),(0,20),(0,0)))
inners = (Polygon(((4,4),(4,6),(6,6),(6,4),(4,4))), Polygon(((7,7),(7,8),(8,8),(8,7),(7,7))))
p = Polygon(outer.exterior.coords, [inner.exterior.coords for inner in inners])
gs = gpd.GeoSeries({0: p})

For example in this example I would like to eliminate the gaps with an area less than 2

1

You just need to include a conditional to your list comprehension: if inner.area>2

p = Polygon(outer.exterior.coords,
            [inner.exterior.coords for inner in inners if inner.area>2])

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.