Published
Edited
Sep 27, 2022
5 stars
Insert cell
Insert cell
Insert cell
world = d3.json("https://unpkg.com/visionscarto-world-atlas/world/110m.json")
Insert cell
borders = taggedBorders(world.objects.countries.geometries, (a, b) =>
d3.extent([a.id, b.id]).join("-")
)
Insert cell
/*
* A tagged border is an arc that belongs to two polygons with a non-null tag
* supersedes https://github.com/topojson/topojson-client/pull/18
*/
function taggedBorders(geometries, tag = (a, b) => a !== b) {
const x = [];
for (const d of geometries) {
for (const i of d.type === "MultiPolygon"
? d.arcs.flat(2)
: d.type === "Polygon"
? d.arcs.flat(1)
: [])
x[i] = d;
}

const borders = new Map();

for (let i = 0; i < x.length; i++) {
const t = x[-i - 1] ? tag(x[i], x[-i - 1]) : null;
if (t != null && t !== false) {
borders.set(t, borders.get(t) || []);
borders.get(t).push(i);
}
}

return {
type: "GeometryCollection",
geometries: Array.from(borders, ([tag, arcs]) => {
const lines = arcs.map((i) => [i]);
return lines.length === 1
? {
type: "LineString",
arcs: lines[0],
properties: { tag }
}
: {
type: "MultiLineString",
arcs: lines,
properties: { tag }
};
})
};
}
Insert cell
Insert cell
rotation = {
do {
const rotate = [(Date.now() / 200) % 360, -35, 0];
map.update(rotate);
yield rotate;
} while (true);
}
Insert cell
color = d3.scaleOrdinal(d3.schemeTableau10)
Insert cell
height = width * 0.6
Insert cell
topojson = require("topojson-server@3", "topojson-client@3")
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