Published unlisted
Edited
May 3, 2020
Insert cell
Insert cell
data = fetch(
"https://raw.githubusercontent.com/vega/vega/master/docs/data/annual-precip.json"
).then(d => d.json())
Insert cell
{
const svg = d3.create("svg").attr("viewBox", [0, 0, 960, 600]);
const path = d3.geoPath(projection);
yield svg.node();

svg
.append("g")
.selectAll("path")
.data(contours)
.join("path")
.attr("d", path)
.attr("fill", d => color(d.value));
svg
.append("g")
.append("path")
.datum(land)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", .5)
.attr("d", path);
}
Insert cell
projection = d3.geoAzimuthalEqualArea().rotate([-180, 243, 178])
Insert cell
contours = d3
.contours()
.thresholds(10)
.size([data.width, data.height])(data.values.map(d => Math.min(3000, d)))
.map(d => Object.assign({ value: d.value }, invert(d)))
Insert cell
function invert(d) {
const n = data.width,
m = data.height;
const shared = {};

let p = {
type: "Polygon",
coordinates: d3.merge(
d.coordinates.map(polygon => {
return polygon.map(ring => {
return ring
.map(point => {
point[0] *= data.scale[0];
point[1] *= data.scale[1];
point[0] += data.translate[0];
point[1] += data.translate[1];
return point;
})
.reverse();
});
})
)
};

// Record the y-intersections with the antimeridian.
p.coordinates.forEach(ring => {
ring.forEach(p => {
if (p[0] === -180) shared[p[1]] |= 1;
else if (p[0] === 180) shared[p[1]] |= 2;
});
});

// Offset any unshared antimeridian points to prevent their stitching.
p.coordinates.forEach(ring => {
ring.forEach(p => {
if ((p[0] === -180 || p[0] === 180) && shared[p[1]] !== 3) {
p[0] = p[0] === -180 ? -179.9995 : 179.9995;
}
});
});

p = d3.geoStitch(p);

// If the MultiPolygon is empty, treat it as the Sphere.
return p.coordinates.length
? { type: "Polygon", coordinates: p.coordinates }
: { type: "Sphere" };
}
Insert cell
color = d3.scaleSequential(d3.interpolateBuPu).domain([0, 3000])
Insert cell
d3 = require("d3@5", "d3-geo-projection@2")
Insert cell
land = fetch(
"https://unpkg.com/visionscarto-world-atlas@0.0.6/world/110m_land.geojson"
).then(d => d.json())
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