Public
Edited
May 13, 2024
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countries = FileAttachment("countries@1.geojson").json()
Insert cell
Insert cell
exploded = {
const parts = countries.features.map(c => turf.flatten(c).features).flat()
parts.map((f,i) => {
f.properties = {...f.properties,id: i}
return f
})
return turf.featureCollection(parts)
}
Insert cell
Insert cell
bboxes = exploded.features.map(f => turf.bboxPolygon(turf.bbox(f),{properties: f.properties}))
Insert cell
Insert cell
tree = {
const bush = new rbush();
bush.load(
bboxes.map((b) => {
const [minX, minY, maxX, maxY] = b.bbox;
const { name, id } = b.properties;
return { minX, minY, maxX, maxY, name, id };
})
);
return bush;
}
Insert cell
Insert cell
// since it's a point, we build a 1-dimensional "search re"ctangle"
treeSearch = tree.search({
minX: ref.lng,
minY: ref.lat,
maxX: ref.lng,
maxY: ref.lat
})
Insert cell
Insert cell
bboxMatch = {
if (mapReady) {
const matchBoxes = treeSearch.map((t) => {
const { minX, minY, maxX, maxY, name, id } = t;
return turf.bboxPolygon([minX, minY, maxX, maxY], {
properties: {
name,
id
}
});
});
map.getSource("bboxMatch").setData(turf.featureCollection(matchBoxes));
return matchBoxes;
} else {
return [];
}
}
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

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