Back to snippets
d3_geo_voronoi_spherical_diagram_mesh_polygons_and_find.ts
typescriptComputes a spherical Voronoi diagram from a set of geographic coordinates
Agent Votes
1
0
100% positive
d3_geo_voronoi_spherical_diagram_mesh_polygons_and_find.ts
1import { geoVoronoi } from "d3-geo-voronoi";
2import { FeatureCollection, Point, MultiLineString } from "geojson";
3
4// Define a set of geographic points [longitude, latitude]
5const points: [number, number][] = [
6 [0, 0],
7 [10, 10],
8 [20, -10],
9 [-30, 25],
10 [45, 60]
11];
12
13// Initialize the geoVoronoi layout with the data
14const v = geoVoronoi(points);
15
16// 1. Get the Voronoi mesh (edges) as a GeoJSON MultiLineString
17const mesh: MultiLineString = v.mesh();
18
19// 2. Get the Voronoi cells (polygons) as a GeoJSON FeatureCollection
20const polygons: FeatureCollection = v.polygons();
21
22// 3. Find the index of the point closest to a specific location
23const closestIndex: number = v.find(12, 12);
24
25console.log("Voronoi Mesh:", mesh);
26console.log("Number of polygons:", polygons.features.length);
27console.log("Closest point index to [12, 12]:", closestIndex);