index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. // index.ts
  2. import { polygon, featureCollection, isObject } from "@turf/helpers";
  3. import { collectionOf } from "@turf/invariant";
  4. import { cloneProperties } from "@turf/clone";
  5. import * as d3voronoi from "d3-voronoi";
  6. function coordsToPolygon(coords) {
  7. coords = coords.slice();
  8. coords.push(coords[0]);
  9. return polygon([coords]);
  10. }
  11. function voronoi2(points, options) {
  12. options = options || {};
  13. if (!isObject(options)) throw new Error("options is invalid");
  14. const bbox = options.bbox || [-180, -85, 180, 85];
  15. if (!points) throw new Error("points is required");
  16. if (!Array.isArray(bbox)) throw new Error("bbox is invalid");
  17. collectionOf(points, "Point", "points");
  18. return featureCollection(
  19. d3voronoi.voronoi().x((feature) => feature.geometry.coordinates[0]).y((feature) => feature.geometry.coordinates[1]).extent([
  20. [bbox[0], bbox[1]],
  21. [bbox[2], bbox[3]]
  22. ]).polygons(points.features).map(function(coords, index) {
  23. return Object.assign(coordsToPolygon(coords), {
  24. properties: cloneProperties(points.features[index].properties)
  25. });
  26. })
  27. );
  28. }
  29. var turf_voronoi_default = voronoi2;
  30. export {
  31. turf_voronoi_default as default,
  32. voronoi2 as voronoi
  33. };
  34. //# sourceMappingURL=index.js.map