Published
Edited
Aug 10, 2022
2 forks
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 = 1) {
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)

console.log("edge:" + e)
console.log("apothem:" + a)
// 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 - e) / e) / (3.0/2.0));
const radiusIfOdd = Math.ceil(((((searchRadiusKm - e) / e) * 2.0) + 1.0)/3.0);
const longRadius = Math.ceil((searchRadiusKm - a)/(2.0*a));
// console.log(radiusIfEven - radiusIfOdd)
const radius = radiusIfEven % 2 === 0 ? radiusIfEven : radiusIfOdd;
console.log("k:" + longRadius)
return h3.kRing(origin, longRadius);
}
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