Skip to main content

Terrain surfaces

Turn elevation samples into a renderable surface.

The terrain module decodes height-map imagery and quantized mesh tiles into mesh data that can be rendered, analyzed, or carried as a mesh Arrow table.

Two terrain inputs

Decode samples, then let the application place the mesh.

Height maps provide elevation samples for reconstruction. Quantized mesh provides an already tessellated terrain tile. Both can end in the same mesh-oriented application path.

Height map

RGB-encoded elevation samples

Quantized mesh

Compressed terrain vertices and indices

Output

Mesh or mesh Arrow table

Writer

Encode compatible terrain as quantized mesh

The @loaders.gl/raster/terrain module reconstructs mesh surfaces from either height map images--e.g. Mapzen Terrain Tiles--which encode elevation into R,G,B values or the quantized mesh format.

Detailed guide

Terrain loaders and writers

The sections below cover installation, supported loader and writer entry points, and the third-party components used for mesh reconstruction.

Installation​

npm install @loaders.gl/raster
npm install @loaders.gl/core

Loaders and Writers​

Loader or WriterDescription
TerrainLoaderLoads height-map terrain as a Mesh or Mesh Arrow table.
QuantizedMeshLoaderLoads quantized mesh terrain as a Mesh or Mesh Arrow table.
QuantizedMeshWriterWrites triangle-list Mesh or Mesh Arrow table terrain data as quantized mesh.

Height grids without mesh reconstruction​

The optional @loaders.gl/raster/terrain/heightmap entry decodes already unpacked RGBA pixels into an unpadded Float32Array, with a named Terrarium preset and support for custom RGB coefficients. It does not load images or import tessellation or Arrow code.

Attribution​

The QuantizedMeshLoader is a fork of quantized-mesh-decoder from HERE under the MIT license to decode quantized mesh.

The TerrainLoader uses MARTINI or Delatin for mesh reconstruction which are both under the ISC License.

Terrain and quantized mesh are now provided by @loaders.gl/raster. Neither requires the optional geotiff or lerc decoder. See the raster package.

Shared terrain algorithms​

TerrainLoader delegates Martini and Delatin reconstruction to @math.gl/geospatial/terrain. loaders.gl owns image decoding, padded borders, geographic bounds, UVs, skirts, and Mesh/Arrow packaging. math.gl owns adaptive triangulation and independent source-node error validation; meshes may refine beyond the old backend output to meet meshMaxError. The shared backend admits up to 1,100,000 padded samples.

For analysis without rebuilding a display mesh, pass decoded heights to math.gl HeightField. Its terrain entry point offers gradients, slope/aspect, contours, profiles, viewsheds, tiled fields, mesh seams, and terrain surface queries. Supply an explicit metric frame and retain the height reference; these APIs do not convert vertical datums.

import {HeightField, createTerrainGradientGrid} from '@math.gl/geospatial/terrain';

// grid is a decoded heightmap with source-provided bounds, registration, validity and datum.
const [west, south, east, north] = grid.bounds;
const cell = grid.registration === 'pixel-center';
const field = new HeightField({
width: grid.width,
height: grid.height,
heights: grid.heights,
validity: grid.valid,
registration: cell ? 'cell' : 'vertex',
origin: [west, north],
columnStep: [(east - west) / (cell ? grid.width : grid.width - 1), 0],
rowStep: [0, -(north - south) / (cell ? grid.height : grid.height - 1)],
heightReference: grid.verticalDatum
});
const gradients = createTerrainGradientGrid(field);