Contour lines 0.x
A contour line joins places at the same height: the lines of a topographic map, close together where the ground is steep and far apart where it's flat. This page traces them through an elevation grid, such as one from elevation tiles, and draws them.
The example is a hill whose contours are circles, so you can check them by hand: 95 m high, falling off as a bell curve 80 m wide, on 41 × 41 cells of 10 m.
import { createGrid } from '@interstellar-tools/maps';
const values: number[] = [];
for (let row = 0; row < 41; row++)
for (let column = 0; column < 41; column++) {
const r = Math.hypot(row - 20, column - 20) * 10; // m from the top
values.push(95 * Math.exp(-(r * r) / (2 * 80 * 80)));
}
const hill = createGrid({
rows: 41,
columns: 41,
values,
cellSize: { x: 10, y: 10 }
});Every number on this page is checked by a test.
Pick the levels
contourLevels gives every height at an interval between the grid's lowest and highest, leaving out missing cells:
import { contourLevels } from '@interstellar-tools/maps';
contourLevels(hill, 20); // [20, 40, 60, 80]
contourLevels(hill, 20, 10); // [10, 30, 50, 70, 90]: through 10 mThe third argument is a height the levels pass through, 0 by default, sea level for heights. Pick an interval that gives about ten to twenty lines over the ground's relief: fewer show too little, more run together on slopes.
Mapmakers draw every fifth line bolder, as an index contour, and label it. With the levels above, that's where (level - base) / interval is a multiple of 5.
Trace and join them
contours traces each level through the grid, by marching squares: every square of four neighbouring cells the level crosses gets a line segment, put on the square's edges by linear interpolation. joinSegments joins the segments that meet end to end into lines:
import { contours, joinSegments } from '@interstellar-tools/maps';
for (const { level, segments } of contours(hill, contourLevels(hill, 20))) {
const lines = joinSegments(segments);
// 20 m: 116 segments, joined into 1 closed line of 117 points
}A closed line, round a hill or a hollow, ends on a copy of its first point. An open line stops at the grid's edge or at missing cells, and runs from one end to the other.
Points are in grid coordinates: a fractional row and column, with whole numbers at cells' centres. Multiply by the cell size for metres. Here, the 20 m line's points are 141.27 m from the top on average, against 141.22 m for the exact circle,
| Level | Segments | Mean radius | Exact radius |
|---|---|---|---|
| 20 m | 116 | 141.27 m | 141.22 m |
| 40 m | 84 | 105.24 m | 105.22 m |
| 60 m | 60 | 76.65 m | 76.69 m |
| 80 m | 36 | 46.71 m | 46.90 m |
The lines cut across the cells in straight segments, so they're closest where the cells are small against the curve: the tight 80 m ring is off by 0.2 m.
Draw a map
On a canvas, a grid position is half a cell in from the grid's edge. Fit the grid's width and height in metres to the canvas, then draw each line:
const lines = contours(hill, contourLevels(hill, 20));
const scale = Math.min(
canvas.width / (hill.columns * hill.cellSize.x),
canvas.height / (hill.rows * hill.cellSize.y)
); // pixels a metre
for (const { level, segments } of lines) {
context.lineWidth = Math.round(level / 20) % 5 === 0 ? 2 : 1; // index contours
context.beginPath();
for (const line of joinSegments(segments))
line.forEach(({ row, column }, i) => {
const x = (column + 0.5) * hill.cellSize.x * scale;
const y = (row + 0.5) * hill.cellSize.y * scale;
if (i === 0) context.moveTo(x, y);
else context.lineTo(x, y);
});
context.stroke();
}Under the lines, hillshadeGrid gives a shaded relief to draw as an image, a cell to a pixel. For a 3D view, the points' height is their level, so they go straight into a projection of the terrain with no lookup.
For latitude and longitude, take a point through the grid's geotransform and the projection's inverse.
Edges and gaps
How the lines behave
- Missing cells. A square with a missing cell has no line, so lines stop where the data does. The lines are open there.
- Saddles. Where two opposite corners of a square are above the level and the other two below, the square's mean height decides which pair the line joins.
- A height exactly at a level counts as above it, so a flat area at that height is outlined rather than filled with lines. A level at a single cell's height can give segments of length 0, which
joinSegmentsleaves out. - The grid's edge. Lines run between the centres of the outer cells, not to the grid's edge.
contours checks the grid's heights once, then skips squares wholly above or below a level, so most of the grid costs little. contourLevels refuses more than 10 000 levels, so a slip in the interval can't run a page out of memory.
From an assistant
The MCP server's contour_lines traces the contours of a grid of up to 32 × 32 cells, at an interval or at given heights, and returns them joined.
Next
- Elevation tiles: get a grid of real terrain, and put places on it.
contours,contourLevelsandjoinSegmentsin the reference.