Published
Edited
Aug 14, 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 = 0) {
const origin = h3.geoToH3(searchLocation.lat, searchLocation.lng, res);
const e = h3.edgeLength(res, h3.UNITS.km);
// Transform the radius from km to grid distance
// 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 radius = (((searchRadiusKm - e)/e)) * (2/3) + pad;

let k = 1;
let foundh3Inside = true;
const h3s = [origin];
while (foundh3Inside) {
foundh3Inside = false;
const newH3s = h3.hexRing(origin, k);
newH3s.forEach(newH3 => {
const h3Center = h3.h3ToGeo(newH3);
if(h3.pointDist([searchLocation.lat, searchLocation.lng], h3Center, h3.UNITS.km) < (searchRadiusKm + e)) {
foundh3Inside = true;
h3s.push(newH3);
}
});
k++;
}

return h3s;
}
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