Introducing a new way to review your PRs.

Read the announcement

maplibre / maplibre-tile-spec

Next generation map vector tiles format

View on GitHub

20 documents · Updated Nov 23, 2026

docs / cpp-codec-pipeline

C++ codec pipeline

The C++ codec pipeline turns tile layers into compact MLT byte streams and turns those streams back into usable layers, features, properties, and geometry. It keeps the tile model separate from the wire model while the decoder validates tile-controlled sizes and limits memory growth. See the C++ tile library for the wider library view and C++ tools and synthetics for data generation and tool flows.

Tile data model

Layer and feature model

The tile model groups features into named layers. Each layer carries a tile extent, feature order, feature IDs, geometry, and feature properties. The decoded tile exposes layers for lookup by name, while each feature keeps its property row index so column data can be read in feature order (cpp/…/tile.hpp• L10-27, cpp/…/layer.hpp• L15-44, cpp/…/feature.hpp• L15-43).

This model supports both ordinary application access and compact storage. Geometry can remain in a shared layer-level vector until a feature needs a concrete geometry object. Property values remain grouped by column, while presence information maps a logical feature row to its compact value row (cpp/…/layer.hpp• L35-44, cpp/…/properties.hpp• L37-67, cpp/…/properties.hpp• L87-114, cpp/…/properties.cpp• L59-104).

References: cpp/…/tile.hpp• L10-27 cpp/…/layer.hpp• L15-44 cpp/…/feature.hpp• L15-43 cpp/…/properties.hpp• L37-67 cpp/…/properties.cpp• L74-104

Geometry forms

The geometry model represents points, lines, polygons, and their multi-part forms with integer-like tile coordinates exposed as floating-point coordinates. Polygon rings are represented as grouped paths, and decoded polygon geometry can also carry triangle indexes for render use (cpp/…/geometry.hpp• L13-31, cpp/…/geometry.hpp• L33-118).

The wire model separates geometry shape from geometry data. Topology describes how vertices form geometries, parts, and rings. Vertex data then supplies the coordinates. This separation lets the same geometry data serve CPU geometry objects or GPU-oriented buffers (cpp/…/geometry_vector.hpp• L25-93, cpp/…/geometry_vector.hpp• L95-172).

References: cpp/…/coordinate.hpp• L8-24 cpp/…/geometry.hpp• L13-118 cpp/…/geometry_vector.hpp• L25-93

Property columns

Properties use a column model rather than one map per feature on the wire. A column has a scalar type or a structured type. A presence bitset maps the compact value list back to feature rows, and strings can use views into shared storage (cpp/…/properties.hpp• L37-67, cpp/…/properties.hpp• L87-114).

The schema records layer name, extent, column names, nullability, and column roles before column data. This metadata gives the decoder the contract it needs to interpret each following stream. The decoder charges column metadata before it creates the column tree and limits nested columns so tile data cannot cause unbounded schema growth (cpp/…/tileset.hpp• L17-49, cpp/…/tileset.hpp• L85-135, cpp/…/tileset.cpp• L21-92, cpp/…/tileset_decode.hpp• L7-11).

References: cpp/…/properties.hpp• L37-67 cpp/…/properties.hpp• L87-114 cpp/…/tileset.hpp• L17-49 cpp/…/tileset.hpp• L108-135 cpp/…/tileset.cpp• L21-92 cpp/…/tileset_decode.hpp• L7-11

Encode and decode lifecycle

The codec moves between a feature-oriented input model and a stream-oriented tile format. The layer schema and stream metadata describe the meaning of each byte range, while the body carries IDs, geometry streams, and property streams.

The main lifecycle connects the public encoder and decoder through the same layer and column contracts.

flowchart LR
    A[Layers, features, geometry, properties] --> B[Encoder]
    B --> C[Layer schema and stream metadata]
    B --> D[Encoded column streams]
    C --> E[MLT tile bytes]
    D --> E
    E --> F[Decoder]
    F --> G[Geometry vectors and property columns]
    G --> H[Layers, features, geometry, properties]

Encoding flow

The encoder accepts layers made of feature IDs, geometry, and typed property values. It builds layer metadata, chooses a physical stream method, and emits each layer as a length-delimited byte block. Empty layers do not produce a layer block (cpp/…/encoder.hpp• L19-57, cpp/…/encoder.hpp• L63-106, cpp/…/encoder.cpp• L503-520, cpp/…/encoder.cpp• L865-890).

Geometry becomes a set of type, topology, and vertex streams. Properties become typed data streams with optional presence streams. The encoder can sort suitable point and line features by spatial order, precompute polygon triangles, and choose a smaller plain, dictionary, or spatially indexed representation when the input allows it (cpp/…/encoder.cpp• L287-359, cpp/…/encoder.cpp• L361-404, cpp/…/encoder.cpp• L406-465, cpp/…/encoder.cpp• L563-655).

References: cpp/…/encoder.hpp• L19-57 cpp/…/encoder.hpp• L63-106 cpp/…/encoder.cpp• L503-520 cpp/…/encoder.cpp• L563-655 cpp/…/encoder.cpp• L865-890

Decoding flow

The decoder reads the tile as a sequence of length-delimited layer blocks. For each supported layer block it reads the schema through a budget-aware feature-table decoder, then uses each column description to decode IDs, geometry, or properties. It joins the decoded IDs and geometries into features and returns a MapLibreTile; one memory budget covers tile-driven decoded allocations (cpp/…/decoder.hpp• L12-30, cpp/…/decoder.cpp• L55-122, cpp/…/decoder.cpp• L158-181, cpp/…/tileset_decode.hpp• L7-11, cpp/…/memory_budget.hpp• L9-45).

Decoded geometry normally stays in a compact geometry vector until feature geometry is requested. The vector expands topology and vertex data into concrete geometry objects and attaches triangle indexes when the tile contains them. A caller can provide a geometry factory to control the created geometry objects (cpp/…/decoder.hpp• L12-24, cpp/…/geometry.hpp• L120-146, cpp/…/geometry_vector.cpp• L121-146).

References: cpp/…/decoder.hpp• L12-30 cpp/…/decoder.cpp• L55-122 cpp/…/decoder.cpp• L158-181 cpp/…/tileset_decode.hpp• L7-11 cpp/…/geometry_vector.cpp• L121-146 cpp/…/memory_budget.hpp• L9-45

Compression and indexing primitives

Integer and presence streams

Integer streams carry IDs, lengths, offsets, geometry types, and coordinates. The codec applies logical transforms such as delta, component-wise delta, Morton coding, or run-length encoding before a physical representation such as fixed-width values, varints, or FastPFOR. Stream metadata records this chain so the decoder can reverse it (cpp/…/stream.hpp• L35-69, cpp/…/stream.hpp• L94-147, cpp/…/int_template.hpp• L17-43, cpp/…/int_template.hpp• L71-161).

Presence streams carry the row mask for nullable columns. The decoder expands compact non-null values through that mask and checks that the mask agrees with the decoded value count, which keeps storage dense while preserving the feature-level view (cpp/…/property.hpp• L29-45, cpp/…/property.hpp• L121-137, cpp/…/property.hpp• L63-117, cpp/…/properties.cpp• L74-104).

FastPFOR handles complete groups of integer values and uses a variable-byte tail for the remainder. Its decoder checks page boundaries, packed data, exception lists, bit widths, and the exact output count because the compressed stream is tile input (cpp/…/fastpfor.hpp• L8-18, cpp/…/fastpfor.cpp• L90-168, cpp/…/fastpfor.cpp• L171-224).

References: cpp/…/stream.hpp• L35-69 cpp/…/stream.hpp• L94-147 cpp/…/int.hpp• L20-35 cpp/…/int.hpp• L21-95 cpp/…/int_template.hpp• L46-69 cpp/…/int_template.hpp• L236-294 cpp/…/fastpfor.hpp• L8-18 cpp/…/fastpfor.cpp• L90-224 cpp/…/property.hpp• L29-45 cpp/…/property.hpp• L63-117 cpp/…/rle.cpp• L13-35 cpp/…/rle.hpp• L115-153

String dictionaries

String columns can store raw values, a dictionary with value offsets, or an FSST-compressed dictionary. The encoder compares candidate layouts and selects a smaller one. Structured string properties can share one dictionary so related child columns reuse the same values (cpp/…/string.hpp• L31-59, cpp/…/string.hpp• L61-132).

The decoder rebuilds string views over retained byte storage. It checks string lengths, symbol references, and dictionary indexes. The owning property column must remain alive while its string views are used (cpp/…/properties.hpp• L16-35, cpp/…/string.hpp• L35-91, cpp/…/string.hpp• L95-199, cpp/…/string.hpp• L202-322).

References: cpp/…/string.hpp• L31-59 cpp/…/string.hpp• L61-132 cpp/…/fsst.hpp• L14-75 cpp/…/string.hpp• L35-91 cpp/…/string.hpp• L95-199 cpp/…/string.hpp• L202-322 cpp/…/raw.hpp• L14-43

Geometry topology and vertex indexes

Geometry topology uses length streams to describe nested geometry, part, and ring boundaries. Optional offset streams refer to shared vertices or triangle indexes. The decoder expands these streams into feature geometry or a GPU-oriented vector based on the available topology and index data, while checking offset order, buffer ranges, and allocation size (cpp/…/geometry.hpp• L57-171, cpp/…/geometry.hpp• L65-290, cpp/…/geometry.hpp• L299-477, cpp/…/geometry_vector.cpp• L29-55).

Polygon data can include triangle indexes for fast rendering. When outlines are retained, the decoder keeps both topology and triangles. When only indexed polygon data is present, it can return a flat GPU vector without rebuilding outline topology. The geometry vector checks that triangle indexes and all consumed vertex data stay within their declared buffers (cpp/…/geometry.hpp• L225-289, cpp/…/geometry_vector.cpp• L121-144, cpp/…/geometry_vector.cpp• L526-548).

References: cpp/…/geometry_vector.hpp• L25-93 cpp/…/geometry.hpp• L57-171 cpp/…/geometry.hpp• L65-290 cpp/…/geometry.hpp• L299-477 cpp/…/geometry_vector.cpp• L121-144 cpp/…/geometry_vector.cpp• L526-548

Spatial ordering and vertex reuse

The pipeline uses space-filling curves as compact integer indexes for geometry values. Morton order interleaves coordinate bits, while Hilbert order preserves local spatial proximity. The encoder uses these indexes to test vertex dictionaries and to order eligible point and line features; it keeps the representation that gives the best result (cpp/…/geometry.hpp• L75-120, cpp/…/encoder.cpp• L361-404).

Both curves shift negative coordinates into a non-negative range and limit the supported coordinate span to 16 bits. If a range does not fit the curve or the indexed form is not smaller, the codec uses the plain vertex stream instead (cpp/…/space_filling_curve.hpp• L11-63, cpp/…/morton_curve.hpp• L7-45, cpp/…/hilbert_curve.hpp• L12-27).

References: cpp/…/space_filling_curve.hpp• L11-63 cpp/…/morton_curve.hpp• L7-45 cpp/…/hilbert_curve.hpp• L12-27 cpp/…/geometry.hpp• L75-120 cpp/…/encoder.cpp• L361-404 cpp/…/int_template.hpp• L323-345 cpp/…/geometry_vector.cpp• L99-117

Decode memory budget

The decode path treats counts and lengths from a tile as untrusted allocation requests. MemoryBudget charges new decoded storage before allocation and rejects a tile when the configured limit would be exceeded; this includes feature-table columns, and the default limit is 64 MiB and resets for each tile (cpp/…/memory_budget.hpp• L9-45, cpp/…/decoder.cpp• L158-165, cpp/…/tileset.cpp• L39-49, cpp/…/tileset_decode.hpp• L7-11).

The budget covers intermediate integer buffers, expanded geometry topology, IDs, and other structures whose size can exceed the encoded input. Raw byte copies still check source availability and declared byte lengths before reading (cpp/…/int.hpp• L28-30, cpp/…/int.hpp• L130-145, cpp/…/geometry.hpp• L330-341, cpp/…/raw.hpp• L14-43).

References: cpp/…/memory_budget.hpp• L9-45 cpp/…/decoder.cpp• L158-165 cpp/…/tileset.cpp• L39-49 cpp/…/tileset_decode.hpp• L7-11 cpp/…/int.hpp• L28-30 cpp/…/int.hpp• L130-145 cpp/…/geometry.hpp• L330-341 cpp/…/raw.hpp• L14-43

Change checklist

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

  • Keep stream metadata names and stream counts aligned with the order emitted by the encoder and consumed by the decoder.
  • Keep nullable column presence counts aligned with the compact non-null value count.
  • Keep geometry length and offset streams aligned with the geometry type order. A change in one topology level can shift every later vertex or ring.
  • Keep string dictionary ownership valid for all returned std::string_view values.
  • Keep Morton metadata such as bit width and coordinate shift paired with the matching vertex representation.
  • Keep triangle indexes within the vertex scope expected by the geometry vector.
  • Keep every tile-driven decoded allocation, including feature-table columns, charged to the shared memory budget before it grows.
  • Keep FastPFOR page, block, exception, and tail rules aligned with the stream producer.
  • Keep feature-table column type flags, names, child counts, and nesting depth aligned with the schema encoder and decoder.

Important failure modes

TriggerConsequenceGuard
Encoder and decoder disagree on a column's stream countThe next column is read at the wrong byte positionTreat stream count and stream order as one wire contract
Nullable metadata says values are present but the presence stream disagreesProperty rows shift or decoding rejects the columnValidate present-bit count against decoded property count
Geometry topology lengths do not match geometry typesParts, rings, or features receive the wrong verticesPreserve the geometry type and topology stream relationship
A spatial index is decoded without its bit width or coordinate shiftCoordinates decode to the wrong positionsCarry the spatial metadata with the encoded vertex stream
A string property outlives its backing columnReturned string views become invalidKeep the owning PresentProperties and its storage alive
A tile has trailing bytes after a layer is decodedThe layer framing is corrupt or unsupportedReject non-zero bytes remaining in the layer stream
A compressed stream declares more output than its bytes can holdThe decoder can over-read or allocate excessive memoryCheck byte lengths, counts, packed blocks, and output limits before decoding
A tile expands beyond the memory limitDecoding stops with a memory-limit errorSet a suitable limit and charge decoded storage through MemoryBudget
FastPFOR exception metadata is incomplete or inconsistentInteger values decode incorrectly or the stream is rejectedValidate exception counts, bit widths, page data, and exact output fill
A feature table declares unsupported or too-deep column metadataSchema decoding stops before column data is readReject unknown type codes and enforce the nesting limit

Key files

FileWhy to read it
cpp/…/encoder.hppPublic input model and encoding options
cpp/…/decoder.hppPublic decode entry points and geometry factory seam
cpp/…/tile.hppDecoded tile and layer lookup model
cpp/…/geometry.hppConcrete geometry types returned to callers
cpp/…/geometry_vector.hppShared geometry storage and topology contract
cpp/…/properties.hppColumn storage, null presence, and string lifetime model
cpp/…/tileset.hppFeature-table schema, column types, and wire header contract
cpp/…/encoder.cppEnd-to-end layer encoding decisions
cpp/…/decoder.cppEnd-to-end layer framing, column dispatch, feature assembly, and tile memory limit
cpp/…/tileset.cppLayer schema framing, budget checks, and bounded nested column metadata
cpp/…/tileset_decode.hppBudget-aware feature-table decode contract used by the tile decoder
cpp/…/geometry.hppGeometry stream choices and vertex indexing
cpp/…/geometry.hppGeometry stream reconstruction, validation, and vector selection
cpp/…/string.hppPlain, dictionary, shared dictionary, and FSST choices
cpp/…/string.hppString reconstruction and shared dictionary handling
cpp/…/fastpfor.cppChecked FastPFOR and variable-byte integer decoding
cpp/…/stream.hppStream types and transform metadata
cpp/…/memory_budget.hppDecode allocation limit and accounting rules
cpp/…/rle.hppBoolean and integer run-length decoding contracts