Published
Edited
Jun 20, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lookupMap = bartStations.features.reduce((map, feature) => {
// Make a map like {[hexagon]: [id, id, ...]}
const [lon, lat] = feature.geometry.coordinates;
const h3Index = h3.geoToH3(lat, lon, res);
if (!map[h3Index]) map[h3Index] = [];
map[h3Index].push(feature);
return map;
}, {})
Insert cell
Insert cell
function kRingResults(searchLocation) {
const lookupIndexes = kRingIndexes(searchLocation);
// Find all points of interest in the k-ring
return lookupIndexes.reduce((output, h3Index) => [...output, ...(lookupMap[h3Index] || [])], []);
}
Insert cell
function kRingIndexes(searchLocation, pad = 0) {
const origin = h3.geoToH3(searchLocation.lat, searchLocation.lng, res);
// Transform the radius from km to grid distance
const e = h3.edgeLength(res, h3.UNITS.km);
const a = (Math.sqrt(3.0)*e)/2.0
const edgeIfEIsReallyA = (2*a)/Math.sqrt(3.0)

const calcApothem = Math.max(...h3.kRingDistances(origin, 1)[1].map(
oneAwayH3 => h3.pointDist(h3.h3ToGeo(oneAwayH3), h3.h3ToGeo(origin), h3.UNITS.km) / 2))
const calcEdge = Math.min(...h3.kRingDistances(origin, 2)[2].map(
twoAwayH3 => h3.pointDist(h3.h3ToGeo(twoAwayH3), h3.h3ToGeo(origin), h3.UNITS.km) / 3))
const calcEdgeFromApothem = (2*calcApothem)/Math.sqrt(3.0)

console.log("edge:" + e)
console.log("apothem:" + a)
console.log("calcApothem:" + calcApothem)
// Was trying to get R k_ring radius, messed up and typed this in and it looks good for r k_ring radius...
// const radius = ((Math.sqrt(3) * searchRadiusKm) / (h3.edgeLength(res, h3.UNITS.km) * 3 )) - .5 + pad;
const radiusIfEven = Math.ceil(((searchRadiusKm - calcEdge) / calcEdge) / (3.0/2.0));
const radiusIfOdd = Math.ceil(((((searchRadiusKm - calcEdge) / calcEdge) * 2.0) + 1.0)/3.0);
const longRadius = Math.ceil((searchRadiusKm - calcApothem)/(2.0*calcApothem));
// console.log(radiusIfEven - radiusIfOdd)
const radius = (radiusIfEven % 2 === 0 ? radiusIfEven : radiusIfOdd) + 1;
console.log("k:" + longRadius)
return h3.kRing(origin, longRadius + pad);
}
Insert cell
Insert cell
function haversineResults(searchLocation) {
return bartStations.features.filter(
feature => haversineDistance(
[searchLocation.lng, searchLocation.lat],
feature.geometry.coordinates,
{units: 'kilometers'}
) < searchRadiusKm
);
}
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
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