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.
| Shape | Output | Use when |
|---|---|---|
mesh | Mesh | You want the legacy mesh object. |
arrow | Mesh Arrow table | You want columnar mesh attributes. |
| TerrainLoader | |
|---|---|
| File Extension | .png, .pngraw |
| File Type | Binary |
| File Format | Encoded height map |
| Data Format | Mesh Arrow table, Mesh |
| Supported APIs | load, parse |
| Decoder Type | Asynchronous |
| 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
| Option | Type | Default | Description |
|---|---|---|---|
terrain.meshMaxError | number | 10 | Mesh error in meters. The output mesh is in higher resolution (more vertices) if the error is smaller. |
terrain.meshMaxSampleTests | number | max(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.bounds | array<number> | null | Bounds 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.elevationDecoder | object | See below | See below |
terrain.tesselator | string | auto | See below |
terrain.shape | string | arrow | Output shape: 'arrow' or 'mesh'. |
terrain.skirtHeight | number | null | If 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).