Skip to content

Documentation / @interstellar-tools/maps / contourLevels

Function: contourLevels() ​

ts
function contourLevels(
  grid: GridInterface,
  interval: number,
  base?: number
): number[];

Defined in: terrain/contour-levels.ts:57

Find the contour levels at an interval through a grid: every height b+kΔ between its lowest and highest values, for contours.

k=⌈zmin−bΔ⌉,…,⌊zmax−bΔ⌋

Where:

  • Δ is the interval and b the base, a height the levels pass through
  • zmin and zmax are the grid's lowest and highest values, leaving out missing cells

Units

  • Inputs: interval, base and the grid's values in the same units, m for an elevation grid.
  • Output: the levels, lowest first.

Notes

  • Each level is b+kΔ, worked out on its own, so rounding doesn't build up along the list. Levels are still floating point: 0.1 × 3 is 0.30000000000000004. Round them for labels.
  • A level exactly at the lowest or highest value is included, as far as rounding goes. The one at the highest value only outlines the cells at that height.
  • A grid with no values, only missing cells, has no levels.
  • Mapmakers' index contours, drawn bolder every fifth line, are the levels where k is a multiple of 5: (level - base) / interval % 5 === 0, rounded.

Parameters ​

ParameterTypeDefault valueDescription
gridGridInterfaceundefinedThe grid, such as heights in m.
intervalnumberundefinedHeight between levels, in the grid's units (finite, > 0).
base?number0A height the levels pass through, in the grid's units (finite). Defaults to 0, sea level for heights.

Returns ​

number[]

The levels, lowest first.

Throws ​

  • If a size, cell size, interval or base is not finite, or a value is infinite.
  • If the grid's parts don't fit together, interval is <= 0, or it would give more than 10 000 levels.

Example ​

ts
// Every 100 m over terrain from 1702 m to 4352 m
contourLevels(grid, 100); // [1800, 1900, …, 4300]

// Every 100 m, offset by 50 m
contourLevels(grid, 100, 50); // [1750, 1850, …, 4350]

const lines = contours(grid, contourLevels(grid, 100)).map(
  ({ level, segments }) => ({
    level,
    lines: joinSegments(segments)
  })
);

See ​

contours