Fil / d3-geo-voronoi

Voronoi / Delaunay tessellations on the sphere
ISC License
229 stars 24 forks source link

Bounding box support for geoVoronoi #46

Closed jrauschenbusch closed 2 years ago

jrauschenbusch commented 2 years ago

Currently one is able to generate Voronoi polygons by passing a set of points. Unfortunately there is no support for bounding box clipping like the Delaunay.vornoi(bbox) or the turf.voronoi(points, { bbox }) function. Is it possible that this functionality can be added or is there another way to achieve this?

Fil commented 2 years ago

Good question! It makes sense to clip spherical coordinates to a rectangular bounding-box is when the projection is cylindrical (Mercator or equirectangular, for example). Otherwise the clipped area can be unexpected:

turf.voronoi example

Of course, if the region you're working on is very small, it's a good idea to project first with a locally conformal projection, then compute the voronoi tessellation (with clipping) on the resulting pixel coordinates. turf.voronoi doesn't do this, and as a consequence is not accurate outside of the equatorial region if you feed it spherical coordinates.

When you need a large region of the globe and want a correct spherical voronoi, you can use d3-geo-voronoi, and then clip its polygons to a rectangle. It can be done with the clipExtent option of d3-geo's projections:

d3.geoMercator()
  .fitExtent([[10, 10],[630, 490]], points)
  .clipExtent([[1, 1], [639, 499]])

a

jrauschenbusch commented 2 years ago

One last question. Since my main goal is to produce a GeoJSON output from a BBox bounded Voronoi diagram (for further processing) and not to produce a d3 rendering, is there an easy approach to achieve? I mean the mentioned clipping based on a d3 projection only works for a d3 rendering context, right?

Fil commented 2 years ago

I knew you'd want that :)

untitled (12)

https://observablehq.com/@fil/clipped-geovoronoi

jrauschenbusch commented 2 years ago

@Fil Thank you so much! You made my day! The world needs more developers like you!