Published
Edited
Sep 18, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
hexagons = {
const layer = {};
for (const feature of sfTravelTimes.features) {
const hexes = spiderPolyfill(feature, h3Resolution);
hexes.forEach(h3Index => {
layer[h3Index] = feature.properties.travelTime;
})
}
return normalizeLayer(layer);
}
Insert cell
function spiderPolyfill(feature, resolution) {
const outerLoop = feature.geometry.coordinates[0][0];
const set = new Set();
// Initial starting set - 1-ring around every vertex
for (const [lng, lat] of outerLoop) {
const hex = h3.geoToH3(lat, lng, resolution);
h3.kRing(hex, 1).forEach(neighbor => iterativeSpider(feature, set, neighbor));
}
return [...set];
}
Insert cell
function recursiveSpider(feature, set, hex) {
if (!set.has(hex)) {
const [centerLat, centerLng] = h3.h3ToGeo(hex);
if (pointInPoly([centerLng, centerLat], feature)) {
set.add(hex);
h3.kRing(hex, 1).forEach(neighbor => recursiveSpider(feature, set, neighbor));
}
}
}
Insert cell
function iterativeSpider(feature, set, start) {
const stack = [start];
let hex;
while(hex = stack.pop()) {
if (!set.has(hex)) {
const [centerLat, centerLng] = h3.h3ToGeo(hex);
if (pointInPoly([centerLng, centerLat], feature)) {
set.add(hex);
h3.kRing(hex, 1).forEach(neighbor => stack.push(neighbor));
}
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pointInPoly = {
const module = await require('https://bundle.run/@turf/boolean-point-in-polygon@6.0.1');
return module.default;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more