Introducing a new way to review your PRs.

Read the announcement

maplibre / martin

Blazing fast and lightweight PostGIS, MBtiles and PMtiles tile server, tile generation, and mbtiles tooling.

View on GitHub

22 documents · Updated Nov 23, 2026

docs / raster-terrain-and-transcoding

Raster terrain and transcoding

Raster terrain processing turns elevation or normal-map tiles into hillshade images or contour vector tiles. The tile-serving path can then return those results in supported raster codecs or convert vector tiles between MVT and MLT when the client requests a different format. See tile serving for the wider request flow and file and remote tile sources or Postgres tile sources for source setup.

Processing model

Terrain processing works from the source tile that covers the request and, when edge samples are needed, the surrounding tiles. The derived result is made before the response is cached or sent, while vector format conversion applies to vector results and ordinary vector source tiles.

flowchart LR
    A[Source tile request] --> B{Terrain process enabled?}
    B -->|Hillshade| C[Read 3x3 normal-map tiles]
    C --> D[Create lossless raster image]
    B -->|Contour| E[Read 3x3 Terrarium tiles]
    E --> F[Create MVT contour tile]
    B -->|No| G[Use source tile]
    F --> H{Client asks for MLT?}
    G --> I{MVT or MLT conversion needed?}
    H -->|Yes| J[Encode MLT]
    H -->|No| K[Return MVT]
    I -->|Yes| L[Convert vector format]
    I -->|No| M[Return source format]

The two terrain paths accept different source data. Hillshade uses Mapzen normal maps because they carry surface direction. Contours use Terrarium elevation tiles because they carry height values.

Raster codecs

Raster format support

martin-tile-utils/…/lib.rs defines the tile formats and their media types. Raster format detection recognizes PNG, GIF, JPEG, WebP, and JPEG XL from tile bytes. Terrain assembly decodes image tiles into a common RGBA field, and the JPEG XL decoder is registered when that path is used (martin-core/…/raster_codecs.rs).

Hillshade output is a grayscale image encoded without loss. It supports PNG, lossless WebP, and JPEG XL. The choice changes the response format and size, but not the shade values (martin-core/…/encode.rs).

References: martin-tile-utils/…/lib.rs• L40-102 martin-tile-utils/…/lib.rs• L269-294 martin-core/…/raster_codecs.rs• L1-9 martin-core/…/encode.rs• L8-59

Neighborhood assembly

Tile neighborhood

martin/…/neighbourhood.rs gathers a 3x3 set centered on the requested tile. The x axis wraps on cylindrical grids, including across the antimeridian. The y axis stops at the grid edge. Missing or failed neighbors are left empty so the center edge can be extended across the gap.

The core assembler decodes the available images and joins them into one field at the source tile resolution. A missing neighbor is filled by clamping the nearest center edge. A fetched center tile that cannot decode is an error. An absent center is kept distinct from a corrupt center so callers can return no content instead of a misleading blank result (martin-core/…/assemble.rs).

The same neighborhood rules serve both terrain processors. This makes a derived line or shade continue across tile edges when adjacent source data exists, while still allowing coverage gaps at map boundaries.

References: martin/…/neighbourhood.rs• L25-52 martin/…/neighbourhood.rs• L75-118 martin-core/…/assemble.rs• L22-103 martin-core/…/assemble.rs• L195-228

Terrain products

Hillshade generation

Hillshade converts Mapzen normal-map pixels into a grayscale relief image. The light direction and shade controls determine how strongly slopes appear, and the result can include an apron for clients that sample beyond tile edges. The default output is a 512-by-512 PNG unless the source settings select another supported lossless format (martin/…/hillshade.rs).

The server gathers the neighborhood first and performs the CPU-heavy bake separately from the request task. It derives the response identity from the source inputs and the shade settings when all inputs provide tags. If the center source tile is absent, the processor returns an empty tile that becomes a no-content response rather than a flat-looking terrain tile (martin/…/hillshade.rs).

Hillshade is enabled per source with convert_to_hillshade. auto uses defaults, a settings map changes selected values, and disabled leaves the normal-map source unchanged. Request overrides are off by default and should remain off for stable edge caching.

References: martin/…/hillshade.rs• L56-68 martin/…/hillshade.rs• L84-142 martin/…/hillshade.rs• L32-56 martin/…/hillshade.rs• L68-141

Contour generation

Contour processing converts Terrarium elevation pixels into isolines. It traces equal-height lines across the assembled field, transforms them into the requested tile space, and writes one MVT layer. The output is vector data, so clients can style line weight, labels, and visibility after receiving the tile (martin-core/…/mod.rs).

Settings control the contour interval by zoom, elevation units, major-line marking, line simplification, minimum feature length, and the source-pixel fetch margin. The margin provides extra input around the tile edge so a line can continue into the neighboring tile before the result is clipped back to the center tile. The default sea-level filter avoids edge-shaped lines over flat sea tiles (martin/…/contour.rs).

The server enables contouring per source with convert_to_contour. It returns no content when the center has no elevation data. A corrupt center is an error, while a missing neighbor only affects the nearby seam. A traced MVT tile can then pass through the normal MVT-to-MLT response conversion.

References: martin-core/…/mod.rs• L1-41 martin/…/contour.rs• L62-137 martin/…/contour.rs• L41-63 martin/…/contour.rs• L189-241

Vector tile transcodes

MVT and MLT negotiation

MVT is the protobuf vector tile format. MLT is a column-based vector tile format designed for compact transfer. The transcode path changes between them only when the request's accepted format and the source process settings allow that direction (martin/…/mod.rs).

The response contract is:

Source dataClient requestResult
MVTMLT acceptedMVT is encoded as MLT.
MLTMVT acceptedMLT is decoded and encoded as MVT.
Raster or no matching requestAnyThe source format is kept.

MLT requires the build feature that provides MLT support. A client must request MLT with Accept: application/vnd.maplibre-tile; a source declaration alone does not change the response format. MVT uses the protobuf media type, including application/x-protobuf for normal tile negotiation (martin-tile-utils/…/lib.rs).

References: martin/…/mod.rs• L11-63 martin-tile-utils/…/lib.rs• L90-128 docs/…/mlt.md• L22-25 docs/…/mlt.md• L44-67

Conversion behavior

MVT-to-MLT conversion first removes any transport compression, reads the vector layers, and encodes each layer with the configured MLT profile. MLT-to-MVT performs the reverse operation and skips MLT layer tags that have no MVT equivalent (martin/…/to_mlt.rs, martin/…/to_mvt.rs).

Conversion returns an uncompressed tile in the target format. It keeps the source cache identity by adding a format suffix to the source ETag: +mlt for MLT and +mvt when converting back. MVT and MLT response variants use separate cache keys, so one format does not replace the other in the tile cache.

The conversion runs on a cache-miss path. As a result, cached entries already represent the requested vector format, while empty derived tiles remain empty and are not passed through a conversion.

References: martin/…/to_mlt.rs• L10-41 martin/…/to_mvt.rs• L11-47 martin/…/mod.rs• L39-62 martin/…/content.rs• L648-683

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the terrain input contract aligned with the processor: normal-map pixels feed hillshade, while Terrarium pixels feed contours.
  • Keep every setting that changes derived bytes in the terrain fingerprint and response identity so caches do not reuse an old result.
  • Preserve the distinction between an absent center tile, a corrupt center tile, and a missing neighbor. They produce different response behavior.
  • Keep accepted media types, format metadata, and MVT/MLT conversion direction aligned across negotiation and cache keys.
  • If a raster codec changes, check both upstream image decoding and hillshade output support. A codec can be valid for one direction and invalid for the other.

Important failure modes

TriggerConsequenceGuard
The center terrain tile is corruptThe derived tile fails instead of returning misleading terrainTreat center decode failure as an error.
The center terrain tile is absentA blank-looking derived tile could be cached as real coverageReturn an empty tile that becomes no content.
A neighbor is missing or unreadableOne tile boundary can show a seamClamp the center edge over the missing slot.
A terrain input has no usable ETagThe server cannot claim that the derived bytes have a stable source identityServe the derived tile without an authoritative ETag.
A client asks for MLT without the required Accept media typeThe client can receive MVT bytes and fail to parse them as MLTSend Accept: application/vnd.maplibre-tile and enable MLT conversion.
A requested hillshade format is not PNG, lossless WebP, or JPEG XLHillshade encoding failsSelect one of the supported lossless formats.

Key files

FileWhy to read it
martin/…/content.rsShows where terrain generation and vector conversion enter the tile response flow.
martin/…/neighbourhood.rsDefines source-grid neighbor lookup, wrapping, clamping, and read behavior.
martin-core/…/assemble.rsExplains how raster tiles become one field for edge-safe processing.
martin/…/hillshade.rsShows the hillshade request lifecycle and response identity.
martin/…/contour.rsShows the contour request lifecycle and MVT result.
martin/…/hillshade.rsDefines hillshade enablement, output formats, and lighting controls.
martin/…/contour.rsDefines contour enablement, intervals, units, and output controls.
martin/…/mod.rsDefines when MVT and MLT conversion is applied and how cache variants are separated.
martin/…/to_mlt.rsShows the MVT-to-MLT response conversion.
martin/…/to_mvt.rsShows the MLT-to-MVT response conversion.
martin-core/…/raster_codecs.rsRegisters JPEG XL decoding for raster processing.