Spaces:
Running
Running
| const h3 = require('h3-js'); | |
| const DEFAULT_RES = 9; // ~150m hex edge — good for foot-traffic granularity | |
| function cellsForRoute(route, resolution = DEFAULT_RES) { | |
| if (!Array.isArray(route) || route.length === 0) return []; | |
| const set = new Set(); | |
| for (const pt of route) { | |
| const lat = Number(pt?.latitude); | |
| const lng = Number(pt?.longitude); | |
| if (!isFinite(lat) || !isFinite(lng)) continue; | |
| set.add(h3.latLngToCell(lat, lng, resolution)); | |
| } | |
| return [...set]; | |
| } | |
| function cellsForBbox(sw, ne, resolution = DEFAULT_RES) { | |
| if (!sw || !ne) return []; | |
| const polygon = [[ | |
| [sw.lat, sw.lng], | |
| [sw.lat, ne.lng], | |
| [ne.lat, ne.lng], | |
| [ne.lat, sw.lng], | |
| [sw.lat, sw.lng], | |
| ]]; | |
| return h3.polygonToCells(polygon, resolution); | |
| } | |
| function boundary(cellId) { | |
| // returns array of [lat, lng] vertices | |
| return h3.cellToBoundary(cellId); | |
| } | |
| module.exports = { DEFAULT_RES, cellsForRoute, cellsForBbox, boundary }; | |