Published
Edited
Aug 10, 2019
9 stars
Insert cell
Insert cell
Insert cell
// Fetch dem data as 256 x 256 array
// Mt. Nabewari, Japan
dem = fetchTile({z: 13, x: 7262, y: 3232})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Calculate gradient at (x, y) of elevation tile
function gradient(tile, x, y) {
// 0 < x, y < (shape of tile - 1)
const fx = Math.floor(x);
const fy = Math.floor(y);

if ((x-fx) + (y-fy) < 1) {
return {
x: tile[fy][fx+1] - tile[fy][fx],
y: tile[fy+1][fx] - tile[fy][fx]
};
} else {
return {
x: tile[fy+1][fx+1] - tile[fy+1][fx],
y: tile[fy+1][fx+1] - tile[fy][fx+1]
}
}
}
Insert cell
// 2D-array of gradients at each point of elevation tile
grads = d3.range(255).map(x => (
d3.range(255).map(y =>
gradient(dem, y, x)
)
));
Insert cell
Insert cell
imshow(dem, 1, d3.interpolateViridis)
Insert cell
Insert cell
imshow(
grads.map(row => row.map(d => Math.sqrt(d.x**2 + d.y**2))),
1,
d3.interpolateViridis
)
Insert cell
md`### 3.3. Gradient in x-direction
Extract the x-component (east-west) of gradient.
`
Insert cell
imshow(
grads.map(row => row.map(d => d.x)),
1,
d3.interpolateViridis
)
Insert cell
Insert cell
imshow(
grads.map(row => row.map(d => d.y)),
1,
d3.interpolateViridis
)
Insert cell
Insert cell
imshow(
grads.map(row => row.map(d => Math.atan2(d.y, d.x))),
1,
d3.interpolateSinebow
)
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