geoprimsField-grade geospatial math

Contour lines from a grid of elevations

Lines of equal elevation at a chosen interval from a grid of elevations, as vector lines with their lengths, by marching squares.

3

3 contour lines at 3 levels, 182.599 m in all.

Each level
ElevationLinesClosed loopsLength
105.000 m11105.558 m
110.000 m1156.030 m
115.000 m1121.011 m
Levels
Total length
Lowest elevation
Highest elevation
Provenance
Computed by
raster.terrain.contours 1.0.0, core 0.1.0
Model
Marching squares (the two-dimensional case of Lorensen and Cline 1987): in each grid square, a level crosses an edge whose corners lie on either side of it (a corner exactly at the level counts as below, as in contourpy), at the linearly interpolated point; the crossings are paired, and a square crossed on all four edges joins its high corners through the middle when the mean of its corners is above the level. Levels are the base plus whole intervals within the grid's range. Squares with a no-data corner are skipped. A geographic grid uses WGS 84 east-west and north-south degree lengths at its center latitude, and its vertices retain the grid's affine latitude-longitude placement
Accuracy
Contour placement is exact for the surface made of linear pieces along the grid lines. Geographic lengths use the WGS 84 degree lengths at the grid's center latitude; a real hillside between grid points is only as well known as the grid
Notes
None
Cites
Lorensen, W. E., and Cline, H. E., ACM SIGGRAPH Computer Graphics, Marching cubes: A high resolution 3D surface construction algorithm

Something look off?

Your values

Showing an example. Change anything.
More options 4
Run many at once from a CSV

Loading…

How we got thisFormula, worked example, sources, and proof

Model: Marching squares (the two-dimensional case of Lorensen and Cline 1987): in each grid square, a level crosses an edge whose corners lie on either side of it (a corner exactly at the level counts as below, as in contourpy), at the linearly interpolated point; the crossings are paired, and a square crossed on all four edges joins its high corners through the middle when the mean of its corners is above the level. Levels are the base plus whole intervals within the grid's range. Squares with a no-data corner are skipped. A geographic grid uses WGS 84 east-west and north-south degree lengths at its center latitude, and its vertices retain the grid's affine latitude-longitude placement

Accuracy: Contour placement is exact for the surface made of linear pieces along the grid lines. Geographic lengths use the WGS 84 degree lengths at the grid's center latitude; a real hillside between grid points is only as well known as the grid

When to use this: Use this to turn a small grid of elevations into contour lines: a survey of spot heights on a regular grid, a DEM clip exported as numbers, or a site model, when you want lines to draw, measure, or hand to CAD rather than a picture. It gives each line's points in order with higher ground on the left, the lines and closed loops per elevation, and their lengths, so a closed loop can be read as a hill or a hollow at a glance.

Limitations: The grid must be regular, north-up, and unrotated, with elevations in meters. A meter grid is square-celled and stays in local east-south coordinates; a geographic grid uses equal latitude and longitude steps, whose ground widths differ by latitude, and also returns map coordinates. Between grid points the ground is assumed to change linearly along each grid line, so a contour cannot show a feature smaller than the grid spacing, and where a square is crossed on all four sides the choice of which way to join is a convention (the mean of the corners, as contourpy and Matplotlib use) rather than something the grid knows. Squares that touch a no-data value are left out, so lines stop at gaps. At most 300 by 300 points, 500 levels, and 100,000 contour vertices.

Worked example: 5 m contours on a geographic hill. Source: contourpy 1.3.0 gives the same three closed loops at 105, 110, and 115 m; the geographic placement follows the grid coordinates and the WGS 84 degree lengths.

You enter

Cell size in degrees
0.0001
Elevation grid
100, 102, 104, 102, 100 102, 106, 110, 106, 102 104, 110, 118, 110, 104 102, 106, 110, 106, 102 100, 102, 104, 102, 100
Contour interval
5 m
North-west latitude
40 deg
North-west longitude
-105 deg

You get

Lines
3
Levels
3
Total length
182.599 m
Lowest elevation
100 m
Highest elevation
118 m

Review: Not yet independently reviewed by a GIS professional.

Last verified: 2026-09-24, when a maintainer last confirmed this tool's sources at the issuer. See the sources ledger.

Status: version 1.0.0, core 0.1.0. See this tool in the verification report.

Changes

Checked against: 25 golden test vectors (download the test vectors, each with its source and tolerance). See how results are checked and every source.

Constants this tool assumes

Values you do not enter, and where each comes from.

WGS 84 semi-major axis, for geographic grid spacing
6378137 m — Department of Defense World Geodetic System 1984
WGS 84 inverse flattening, for geographic grid spacing
298.257223563 — Department of Defense World Geodetic System 1984

Sources