Published
Edited
Nov 28, 2020
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Calculate lat/lon of pixel (pX, pY) on tile tileZ/tileX/tileY
function pixelOnTileToLatLon(pX, pY, tileZ, tileX, tileY) {
const L = 85.05112878;
const x = 256 * tileX + pX;
const y = 256 * tileY + pY;

const lon = 180 * (x / (2**(tileZ+7)) - 1);
const lat = (180/Math.PI) * math.asin(math.tanh(
-Math.PI/(2**(tileZ+7)) * y + math.atanh(math.sin(L * Math.PI/180))
));
return {lat: lat, lon: lon}
}
Insert cell
Insert cell
data = transformContour(contours)
Insert cell
function transformContour(contours) {
return contours.map(({type, value, coordinates}) => (
{type, value, coordinates: coordinates.map(rings => (
rings.map(points => (
points.map(([x, y]) => {
const coord = pixelOnTileToLatLon(x, y, tile.z, tile.x, tile.y);
return [coord.lon, coord.lat];
})
))
))}
));
}
Insert cell
Insert cell
function overlayContour() {
data.forEach((d, i) => {
map.addSource(`contour${i}`, {
type: "geojson",
data: d,
});
map.addLayer({
"id": `contourLayer${i}`,
"type": "line",
"source": `contour${i}`,
"layout": {},
"paint": {
"line-width": 2,
"line-color": color(d.value),
"line-opacity": 0.8
}
});
})
}
Insert cell
map.on("load", overlayContour);
Insert cell
// Manually update map on slider event
d3.select("#interval").on("change", () => {
// Remove old contours
map.getStyle().layers.forEach(l => {
if (l.id.startsWith("contourLayer")) {
map.removeLayer(l.id);
}
});
Object.keys(map.getStyle().sources).forEach(k => {
if (k.startsWith("contour")) {
map.removeSource(k)
}
})

overlayContour();
});
Insert cell
Insert cell
tile = ({z: 13, x: 7262, y: 3232})
Insert cell
// Fetch dem data as 256 x 256 array
dem = fetchTile(tile)
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