0

I'm trying to get some voronoi polygons from a set of coordinates distributed within a country, I don't need said polygons to extend out of the country, I tried using extend_to from voronoi_polygons documentation and downloaded an shp file from gadm and reading it with fiona as a geometry, but when passing it as argument it won't take it in count and the voronoi polygons will extend out of the country.

with fiona.open("./gadm41_BOL_0.shp") as file:
    country_shape = shape(file[0]["geometry"])

data = pd.read_csv('data.csv')
points = MultiPoint(data[['long', 'lat']].values)

vor_polygons = voronoi_polygons(points, extend_to=country_shape)

It's worth noting that when displaying country_shape on a jupyter notebook it'll show a small contour / silhouette of the country properly, might as well add that the type of country_shape is shapely.geometry.multipolygon.MultiPolygon, I'm also sure with a high degree of confidence that none of the coordinates in data.csv is outside the country, hence extend_to shouldn't fail.

1 Answer 1

2

If you have a close look at the documentation of shapely.voronoi_polygons, you can see for extend_to that the "diagram will be extended to cover the envelope of this geometry". The "envelope" is synonym to the "bounds" or "minimum bounding rectange", so it is normal that the voronoi polygons don't follow the exact boundaries of the country. Relevant extract from the documentation:

extend_to:  Geometry or array_like, optional: 
    If provided, the diagram will be extended to cover the 
    envelope of this geometry (unless this envelope is smaller than the 
    input geometry)

If you want the voronoi polygons to be clipped to the country, you can calculate the intersection between them:

vor_polygons_in_country = shapely.intersection(vor_polygons, country_shape)
1
  • 1
    Certainly clipping is the keyword, I've also tried voronoi_diagram from shapely.ops, which has the argument envelop that should clip to a geometry but doesn't work either, solution was headed towards your suggestion though, not as simple as a single intersection but rather a loop where I intersect every Polygon and MultiPolygon from vor_polygons with the country shape. Thanks for the help! Commented Apr 18, 2023 at 18:55

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.