Public
Edited
Sep 14, 2022
10 stars
Insert cell
Insert cell
{
const projection = d3.geoMercator().fitExtent(
[
[0, 0],
[width, 650]
],
contours[0]
);
const path = d3.geoPath(projection);

const svg = DOM.svg(width, 650);

d3.select(svg)
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.selectAll()
.data(contours)
.join("path")
.attr("fill", (d) => color(d.value))
.attr("d", path);

return svg;
}
Insert cell
n = 152 // guessed from points.length
Insert cell
m = 106
Insert cell
contours = d3
.contours()
.thresholds(20)
.size([n, m])(grid.Z.map((d) => d))
.map(project) // reproject to WGS84
Insert cell
Insert cell
projection = ([x, y]) => {
const x0 = clamp(Math.floor(x), 0, n - 1),
dx = x - x0;
const x1 = clamp(Math.ceil(x), 0, n - 1),
rx = dx / (x1 - x);
const y0 = clamp(Math.floor(y), 0, m - 1),
dy = y - y0;
const y1 = clamp(Math.ceil(y), 0, m - 1),
ry = dy / (y1 - y);

// bilinear interpolation
const qx = i(
i(grid.X[x0 + n * y0], grid.X[x1 + n * y0], rx),
i(grid.X[x0 + n * y1], grid.X[x1 + n * y1], rx),
ry
);
const qy = i(
i(grid.Y[x0 + n * y0], grid.Y[x0 + n * y1], ry),
i(grid.Y[x1 + n * y0], grid.Y[x1 + n * y1], ry),
rx
);

return [qx, qy];

// linear interpolation
function i(a, b, r) {
return a === b ? a : (a + b * r) / (r + 1);
}

function clamp(x, lo, hi) {
return x < lo ? lo : x > hi ? hi : x;
}
}
Insert cell
color = d3
.scaleSequential(d3.interpolateMagma)
.domain(d3.extent(contours, (d) => d.value))
Insert cell
function project(d) {
d = {
...d,
coordinates: d.coordinates.map((poly) =>
poly.map((ring) => ring.map(projection).reverse())
)
};
return d;
}
Insert cell
points = FileAttachment("data-issue-64@1.json")
.json()
.then((d) => d.data.data.json)
Insert cell
grid = ({
X: points.map((d) => d[0]),
Y: points.map((d) => d[1]),
Z: points.map((d) => d[2])
})
Insert cell
Insert cell
Plot.carto({
height: 500,
marks: [
Plot.points(points, {
lonLat: (d) => d,
fill: (d) => d[2] || 20, // in orange, the null data
r: 1
})
]
})
Insert cell
import { Plot } from "@fil/plot-carto-0-5"
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