Search code examples
mapboxvoronoiturfjs

How to mask/trim voronoi polygons around country?


I have mapbox map with ~2000 points and I have voronoi polygons drawn around them vith turf.js, how can set boundaries around country so polygon don't stretch all over the world? I just want to mask/trim them and keep polygons around country borders.

What I already tried is using turf.js mask feature (https://turfjs.org/docs/#mask) but I lose all layers (there are no polygons/points anymore).

Here is my code on JSFiddle:
https://jsfiddle.net/eqxdjv6z/

Solution

  • Loop through each Border of each Voronoi cell and check if it crosses the outer shell. If so, set the Intersection as a new edge of the Voronoi cell and delete all edges that are outside the meshing area.

    Maybe this helps you, as well: https://discourse.mcneel.com/t/trim-voronoi-cell/78408