Skip to main content

Terrain module · loader API

TerrainLoader

Decode RGB or grayscale elevation images into a renderable mesh or Mesh Arrow table, with control over bounds, error, skirts, and elevation decoding.

What it reconstructs

Turn pixels that encode elevation into a surface.

TerrainLoader interprets image channels as elevation samples, places them within optional geographic bounds, and produces mesh data that can be rendered or passed through a columnar pipeline.

Input

RGB, grayscale, or raw terrain images

Decode

Channel scales and elevation offset

Output

Mesh or Mesh Arrow table

Detail

Error tolerance, bounds, and skirts

Detailed guide

TerrainLoader reference

The sections below document supported inputs, output shapes, usage, elevation decoding, and loader options.

TerrainLoader reconstructs mesh surfaces from height map images, e.g. Mapzen Terrain Tiles, which encodes elevation into R,G,B values. It returns a Mesh Arrow table by default. Set terrain.shape: 'mesh' for the legacy Mesh object.

ShapeOutputUse when
meshMeshYou want the legacy mesh object.
arrowMesh Arrow tableYou want columnar mesh attributes.
TerrainLoader
File Extension.png, .pngraw
File TypeBinary
File FormatEncoded height map
Data FormatMesh Arrow table, Mesh
Supported APIsload, parse
Decoder TypeAsynchronous
Worker Thread✅ Yes
Streaming Support❌ No

Usage​

import {TerrainLoader} from '@loaders.gl/raster/terrain';
import {load} from '@loaders.gl/core';

const data = await load(url, TerrainLoader, options);
const table = await load(url, TerrainLoader, {
worker: false,
terrain: {...options.terrain, shape: 'arrow'}
});

TerrainLoader internally decodes heightmap images with ImageLoader and then converts them with getImageData(image).

For Terrarium imagery, including Mapterhorn terrain WebP tiles, use the named preset:

import {TERRARIUM_ELEVATION_DECODER} from '@loaders.gl/raster/terrain/heightmap';

const terrain = await load(url, TerrainLoader, {
terrain: {elevationDecoder: TERRARIUM_ELEVATION_DECODER}
});

If you only need elevation samples, use the lightweight heightmap utilities instead of reconstructing a mesh.

Options​

OptionTypeDefaultDescription
terrain.meshMaxErrornumber10Mesh error in meters. The output mesh is in higher resolution (more vertices) if the error is smaller.
terrain.meshMaxSampleTestsnumbermax(10,000,000, 32 × padded samples)Maximum source-node validation tests per refinement pass. Raise for demanding meshes, or lower to bound work explicitly.
terrain.boundsarray<number>nullBounds of the image to fit x,y coordinates into. In [minX, minY, maxX, maxY]. If not supplied, x and y are in pixels relative to the image.
terrain.elevationDecoderobjectSee belowSee below
terrain.tesselatorstringautoSee below
terrain.shapestringarrowOutput shape: 'arrow' or 'mesh'.
terrain.skirtHeightnumbernullIf set, create the skirt for the tile with particular height in meters

elevationDecoder​

Parameters used to convert a pixel to elevation in meters. An object containing the following fields:

  • rScaler: Multiplier of the red channel.
  • gScaler: Multiplier of the green channel.
  • bScaler: Multiplier of the blue channel.
  • offset: Translation of the sum.

Each color channel (r, g, and b) is a number between [0, 255].

For example, the Mapbox terrain service's elevation is encoded as follows:

height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)

The corresponding elevationDecoder is:

{
"rScaler": 6553.6,
"gScaler": 25.6,
"bScaler": 0.1,
"offset": -10000
}

The default value of elevationDecoder decodes a grayscale image:

{
"rScaler": 1,
"gScaler": 0,
"bScaler": 0,
"offset": 0
}

tesselator​

The choices for tesselator are as follows:

auto:

  • Chooses Martini if possible (if the image is a square where both height and width are powers of 2), otherwise uses Delatin instead, which has no input image limitations.

martini:

  • Uses the Martini algorithm for constructing a mesh.
  • Only works on square 2^n+1 x 2^n+1 grids.
  • Generates a hierarchy of meshes (pick arbitrary detail after a single run)
  • Optimized for meshing speed rather than quality.

delatin:

  • Uses the Delatin algorithm for constructing a mesh.
  • Works on arbitrary raster grids.
  • Generates a single mesh for a particular detail.
  • Optimized for quality (as little triangles as possible for a given error).