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-tile-library

C++ tile library

The C++ tile library encodes application tile data into the MapLibre Tile (MLT) format and decodes MLT bytes into layers, features, properties, and geometry. It provides a static library for product code and optional JSON, test, command-line, and fuzzing targets for validation and inspection.

Library role

Tile codec

The library is the native C++ path for reading and writing MLT vector tiles. It keeps the public API focused on tile data and hides the format work needed to describe columns, compress streams, and rebuild geometry.

References: cpp/README.md• L1-7 cpp/…/encoder.hpp• L59-111 cpp/…/decoder.hpp• L12-30

Public library surface

Encoder contract

The mlt::Encoder accepts layers made of feature IDs, geometry, and typed properties. It returns one byte buffer containing the encoded layers. EncoderConfig lets callers choose key format features such as IDs, feature order, polygon outlines, string packing, and integer stream methods.

The input geometry uses tile coordinates. Polygon rings must be supplied without a repeated closing vertex because the decoder restores closure when it builds geometry objects. The encoder also sets polygon winding to the MVT convention and can create triangle indexes for polygon use.

References: cpp/…/encoder.hpp• L19-57 cpp/…/encoder.hpp• L63-106 cpp/README.md• L55-61

Decoder contract

The mlt::Decoder accepts a view of raw tile bytes or a BufferStream and returns an mlt::MapLibreTile. The decoder can use the built-in geometry types or a caller-supplied GeometryFactory, which lets a host create its own geometry objects. Malformed input and input that exceeds the memory limit raise std::runtime_error.

The decoder applies a 64 MiB per-tile limit by default. It shares that budget with feature-table schema decoding and stream work, then resets it for each tile. A caller can change the limit with setMemoryLimit; FastPFOR support is built into the decoder and needs no per-call switch.

References: cpp/…/decoder.hpp• L13-31 cpp/…/decoder.cpp• L55-185 cpp/…/tileset_decode.hpp• L7-11 cpp/…/memory_budget.hpp• L9-45

Decoded tile model

MapLibreTile exposes layers by name or as a list. Each Layer provides its name, tile extent, features, and property columns. Each Feature provides an optional ID, one geometry, and access to a property by name.

Decoded string properties are views into tile-owned data. Keep the tile and its layer alive while those views are in use. The layer also keeps its compact geometry data alive because feature geometry objects may refer to that data.

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

Codec pipeline

The codec turns each layer into a length-framed record. The record starts with a feature-table description, followed by stream data for geometry, IDs, and properties.

flowchart LR
    A[Input layers] --> B[Build layer schema]
    B --> C[Encode geometry and properties]
    C --> D[Add stream metadata]
    D --> E[Length-frame layers]
    E --> F[MLT tile bytes]
    F --> G[Read layer frames]
    G --> H[Read schema and streams]
    H --> I[Build tile model]

For the byte-level codec flow and stream order, see C++ codec pipeline.

Tile framing

The encoder writes each non-empty layer as a varint length followed by a layer tag and the layer payload. The decoder reads one frame at a time, handles the supported basic layer tag, and skips unknown layer tags so future layer forms can coexist with this reader.

The decoder uses bounded substreams for each layer. This keeps a bad layer length from consuming bytes from the next layer and makes trailing data inside a layer an error.

References: cpp/…/encoder.cpp• L865-891 cpp/…/decoder.cpp• L161-180 cpp/…/buffer_stream.hpp• L20-31

Metadata and stream model

Feature-table metadata describes the layer name, extent, and columns. A column can hold a scalar value, geometry, or a nested string structure. The decoder charges column storage to the same tile budget used by the rest of the decode. It also rejects a missing layer name, unknown column type, invalid child declaration, or column tree that is too deep.

This split lets the same reader understand different choices for integer, string, and geometry packing. It also gives the decoder the counts and byte lengths needed to keep each stream within its layer frame. Stream metadata can describe plain values, varints, FastPFOR, delta steps, RLE, Morton coordinates, dictionaries, and FSST data.

References: cpp/…/tileset.hpp• L12-140 cpp/…/tileset.cpp• L21-92 cpp/…/tileset_decode.hpp• L7-11 cpp/…/stream.hpp• L14-147 cpp/…/stream.cpp• L51-113

Encoding stages

The encoder builds one layer in a fixed order:

  1. It derives the column schema from IDs and feature properties.
  2. It collects geometry types, vertex data, and shape counts.
  3. It encodes geometry topology, coordinates, IDs, and property columns.
  4. It writes the schema and stream chunks into one layer payload.

The result is then prefixed with its layer length. This keeps layer boundaries clear and allows the tile to carry more than one layer.

References: cpp/…/encoder.cpp• L89-110 cpp/…/encoder.cpp• L180-284 cpp/…/encoder.cpp• L287-359 cpp/…/encoder.cpp• L857-891

Decoding stages

The decoder follows the reverse order. It reads the feature table first, then uses each column description to decode IDs, geometry, or properties. It expands the decoded geometry vectors through the configured factory and checks that the layer stream is fully consumed before it returns the layer. Feature-table allocations use the tile budget before the decoder creates the column tree.

References: cpp/…/decoder.cpp• L55-145 cpp/…/tileset.cpp• L39-92 cpp/…/tileset_decode.hpp• L7-11

Data and algorithm model

Geometry model

The library represents point, line, polygon, and multi-part geometry as tile-coordinate objects. During decode, compact vertex buffers and topology offsets are expanded into geometry objects. Topology means the counts and offsets that show where geometries, parts, and rings begin and end.

The decoder can retain geometry as a compact CPU or GPU-friendly vector until a caller asks for individual geometries. This supports both feature access and rendering-oriented use without changing the tile format.

References: cpp/…/geometry.hpp• L13-146 cpp/…/geometry_vector.hpp• L25-93 cpp/…/geometry_vector.cpp• L120-158

Geometry reconstruction

The geometry decoder turns type values, topology lengths, vertex data, and optional indexes into geometry objects. Topology lengths rebuild the boundaries between geometries, parts, and polygon rings. Vertex data can be plain, shared through offsets, or represented by Morton codes.

The resulting GeometryVector keeps compact data until the decoder asks the factory to create feature geometries. Polygon rings are closed at this boundary, and optional triangle indexes are checked against the vertex count before they are attached to the geometry.

References: cpp/…/geometry.hpp• L65-289 cpp/…/geometry_vector.cpp• L61-117 cpp/…/geometry_vector.cpp• L146-186

Property model

Properties are stored by column rather than as a separate map for every feature. A presence bitmap marks which features have a value. Feature lookup maps a feature index to the right value in each column and returns a typed property.

The encoder supports numeric, Boolean, string, and simple struct values. Struct values become child columns, so nested string fields keep the same column and stream model as other properties.

References: cpp/…/properties.hpp• L37-114 cpp/…/encoder.cpp• L201-281 cpp/…/decoder.cpp• L101-109

String and nullable values

String columns can use plain bytes, a dictionary, a shared dictionary for struct fields, or FSST symbols. The decoder returns string views into tile-owned storage, so the decoded tile must stay alive while callers use those values.

Nullable scalar columns use a presence bitset to map dense stored values back to feature positions. This lets the stored value array omit nulls while the public property lookup still uses the feature index.

References: cpp/…/string.hpp• L28-91 cpp/…/string.hpp• L94-199 cpp/…/property.hpp• L63-104 cpp/…/properties.cpp• L74-104

Compression choices

The codec chooses packing methods to reduce tile size while keeping decode work predictable. Integer streams can use varints, delta coding, run-length encoding (RLE), or FastPFOR. Strings can use dictionaries and FSST. Geometry coordinates can use plain values or a coordinate dictionary based on Morton or Hilbert order when that is smaller.

These choices are recorded in stream metadata. The decoder does not guess the method from the data; it reads the declared method and applies the matching reverse steps.

References: cpp/…/stream.hpp• L14-61 cpp/…/encoder.hpp• L27-46 cpp/…/geometry.hpp• L57-121 cpp/…/int.hpp• L29-87

Integer stream decoding

Integer streams are decoded in two stages. The physical stage reads fixed-width values, varints, or FastPFOR words. The logical stage restores delta, componentwise delta, RLE, or Morton transforms in the order declared by the stream metadata.

FastPFOR is handled by a checked decoder rather than by trusting the low-level compression library. It reads compressed blocks and their exception data, then reads the variable-byte tail. It rejects inconsistent counts, lengths, offsets, bit widths, and unused data.

References: cpp/…/int_template.hpp• L164-294 cpp/…/fastpfor.hpp• L8-18 cpp/…/fastpfor.cpp• L90-168

Polygon handling

Polygon rings are stored with the winding expected by MVT. When requested, the encoder tessellates polygons into triangle indexes and can keep outline topology as well. The decoded geometry can therefore serve both map feature access and render paths.

References: cpp/…/encoder.cpp• L287-359 cpp/…/encoder.cpp• L412-419 cpp/…/geometry.hpp• L123-171 cpp/…/geometry_vector.cpp• L99-117

Decoder safety

MLT bytes are treated as untrusted input. Buffer reads, varints, layer frames, stream sizes, topology offsets, string lengths, dictionary indexes, and triangle indexes are checked before use. Feature-table column counts are checked against available data and charged to the per-tile memory budget before allocation. Nested columns have a fixed depth limit, and unsupported or inconsistent column type declarations fail during schema decoding.

This safety boundary has two outcomes: valid tiles produce the normal public model, while malformed tiles fail with std::runtime_error instead of reading outside a buffer or allocating from an unchecked size.

References: cpp/…/buffer_stream.hpp• L20-31 cpp/…/varint.hpp• L29-77 cpp/…/tileset.cpp• L21-92 cpp/…/tileset_decode.hpp• L7-11 cpp/…/memory_budget.hpp• L9-45 cpp/…/geometry_vector.cpp• L29-55

Build integration

CMake targets

The CMake build uses C++20 and produces two static library targets: mlt-cpp for decoding and mlt-cpp-encoder for encoding. Both targets include the shared feature-table metadata codec, while the decoder target adds the shared-budget decode entry used for tile reads. JSON support, tests, and command-line tools are optional build areas and are enabled by default. Fuzzers are optional and require Clang. FastPFOR, FSST, and Earcut are linked as format support dependencies.

References: cpp/CMakeLists.txt• L1-37 cpp/CMakeLists.txt• L108-190 cpp/CMakeLists.txt• L195-222 cpp/…/CMakeLists.txt• L1-22

Bazel target

The Bazel setup exposes the public decoder library as //cpp:mlt_cpp. It builds with C++20 and disables JSON support. The current target excludes encoder headers and encoder sources, so Bazel consumers do not receive the C++ encoder through this target.

References: cpp/BUILD.bazel• L1-51

Test and tool boundary

The C++ test target checks primitive encodings, metadata, full encode/decode behavior, geometry, expected tile data, and malformed-input behavior. The robustness suite also verifies that invalid schema counts, nesting, lengths, and stream data fail as std::runtime_error without unsafe memory use. The FastPFOR tests cover both cross-language streams and corrupted stream fields. The fuzz targets exercise whole-tile decoding and the FastPFOR stream decoder.

The mlt-cpp-json tool uses the decoder and converts a tile to JSON or GeoJSON for inspection. For tool use and synthetic data flows, see C++ tools and synthetics. For byte-level stream order and codec stages, see C++ codec pipeline.

References: cpp/…/CMakeLists.txt• L1-67 cpp/…/test_fastpfor_decode.cpp• L1-11 cpp/…/test_robustness.cpp• L1-7 cpp/…/test_robustness.cpp• L104-179 cpp/…/fuzz_tile.cpp• L1-27 cpp/…/fuzz_fastpfor.cpp• L1-49 cpp/…/mlt-json.cpp• L33-69

Change checklist

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

  • Keep the column type rules in metadata/tileset.hpp, encoder schema creation, and decoder column dispatch aligned.
  • Keep the public feature-table decode entry and the decoder's shared-budget entry aligned when schema validation or allocation rules change.
  • Keep stream ordering and stream counts aligned between geometry or property encoders and their decoder readers.
  • If a new packing method is added, update stream metadata, the matching decoder capability, and the relevant CMake source and dependency lists.
  • Preserve tile and layer ownership rules when changing decoded string views or compact geometry storage.
  • Keep CMake and Bazel target contents aligned when adding a public library feature.
  • Keep FastPFOR stream validation aligned with the stream format and with the fuzz and robustness checks.
  • Keep the memory budget charged before any allocation whose size comes from tile data.

Important failure modes

TriggerConsequenceGuard
A FastPFOR count, page field, exception, or tail does not match the streamInteger decoding fails or could write outside the output without validationUse the checked FastPFOR decoder and reject malformed fields
A layer frame or stream leaves unread bytesThe decoder rejects the layer or reads the next field at the wrong offsetPreserve length prefixes, stream counts, and byte lengths
A nullable column has a wrong presence streamFeature values shift to the wrong rowsKeep column nullability and presence bits derived from the same feature order
A decoded tile is destroyed before string properties are usedString views refer to invalid storageKeep the owning tile and layer alive
Encoder and decoder disagree on polygon ring closurePolygon shape gains or loses a vertexSupply open rings to the encoder and let decode restore closure
A tile declares a large expansion from RLE, topology, or another countDecoding can consume excessive memoryCharge allocations to MemoryBudget before resizing buffers
A feature table declares too many columns, uses an unknown type, or nests them too deeplySchema decoding can exceed the tile budget or accept an invalid column treeCheck counts against the remaining data, reject unsupported or inconsistent type codes, charge column storage, and enforce the nesting limit

Key files

FileWhy to read it
cpp/…/encoder.hppPublic input model and encoder options
cpp/…/decoder.hppPublic decode entry points and extension hooks
cpp/…/tile.hppTop-level decoded tile access
cpp/…/layer.hppLayer ownership and feature access
cpp/…/geometry.hppPublic geometry types and factory
cpp/…/tileset.hppFeature-table and column schema
cpp/…/tileset.cppFeature-table schema encoding, decoding, and validation
cpp/…/tileset_decode.hppShared-budget feature-table decode entry used by the tile decoder
cpp/…/stream.hppStream meanings and packing declarations
cpp/…/encoder.cppLayer assembly and encode policy
cpp/…/decoder.cppLayer framing and decode orchestration
cpp/…/int_template.hppPhysical and logical integer stream restoration
cpp/…/fastpfor.cppChecked FastPFOR and variable-byte stream decoding
cpp/…/geometry.hppGeometry stream and topology reconstruction
cpp/…/string.hppPlain, dictionary, shared-dictionary, and FSST string decoding
cpp/…/memory_budget.hppPer-tile allocation limit
cpp/CMakeLists.txtLibrary, dependency, test, and tool integration
cpp/…/CMakeLists.txtOptional libFuzzer target integration
cpp/BUILD.bazelPublic Bazel library boundary