Public
Edited
May 20, 2024
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function indexPolygon(polygon) {
switch (approach.value) {
case 'fine': {
// Index the polygon at the finest resolution
return h3.polygonToCells(polygon, config.fineRes, true);
}
case 'dual': {
// Index the polygon at two resolutions, fine and coarse
const cells = h3.polygonToCells(polygon, config.fineRes, true);
const compactCells = h3.compactCells(cells);
const coarseCells = h3.uncompactCells(
compactCells.filter(cell => h3.getResolution(cell) <= config.coarseRes),
config.coarseRes
);
const fineCells = h3.uncompactCells(
compactCells.filter(cell => h3.getResolution(cell) > config.coarseRes),
config.fineRes
);
return coarseCells.concat(fineCells);
}
case 'compact': {
// Index the polygon at various resolutions
const cells = h3.polygonToCells(polygon, config.fineRes, true);
return h3.compactCells(cells);
}
}
}
Insert cell
resolutionsToCheck = {
switch (approach.value) {
case 'fine': {
// Check only the fine resolution, that's all that's in the index
return [config.fineRes];
}
case 'dual': {
// Check either fine or coarse resolutions
return [config.coarseRes, config.fineRes];
}
case 'compact': {
// Check all resolutions from 0 to finest
return [...Array(config.fineRes + 1).keys()]
}
}
}
Insert cell
function indexLookup(point) {
const finest = h3.latLngToCell(point.lat, point.lng, config.fineRes);
for (const res of resolutionsToCheck) {
// Note that we take the parent of the finest cell, *not* just latLngToCell(res).
// This is because the coarser cells represent sets of their children, not their
// own geometry, so you could get false positives or false negatives.
const lookupCell = h3.cellToParent(finest, res);
const country = index.get(lookupCell);
if (country) return country;
}
return null;
}
Insert cell
Insert cell
clickedCountry = map.point ? indexLookup(map.point) : null;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countriesTopoJson = FileAttachment("countries.json").json()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more