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 / tilegen-pipeline-and-plans

Tilegen pipeline and plans

Tilegen turns source features into ordered vector tiles. A plan decides which layers and features are eligible, phase one turns eligible geometry into tile records, and phase two groups those records into encoded tiles for a sink. The pipeline keeps output order stable while it uses parallel readers, renderers, and encoders.

For source and sink contracts, read Tilegen inputs and output sinks. For the wider product view, read Tile generation.

Generation pipeline

The generation flow has a render stage and a write stage. Readers provide feature batches to render workers, the workers create sorted records, and the later stage merges those records before it assembles and encodes each tile. The sink receives tiles in tile order even when worker completion order differs.

In Martin bulk mode, source discovery selects table sources, lowers their layer settings into one plan, and passes that plan to the tile generator. The run creates its output before generation starts and removes the partial output if generation fails or is interrupted. Bulk mode scans tables and views. It does not use function sources, and all selected sources must use the Web Mercator tile grid.

The bulk command resolves a continuous zoom range before it starts. It rejects skipped zoom levels and rejects options that belong to serving or to an existing-output workflow. It selects one database for the requested tables, then creates the output sink and starts generation.

This diagram shows how source batches become encoded tiles and how the write stage completes metadata after all tile work finishes.

flowchart LR
    A[Feature source] --> B[Feature batches]
    B --> C[Phase one: evaluate and render]
    C --> D[Sorted runs]
    D --> E[Merge and group by tile]
    E --> F[Phase two: assemble and encode]
    F --> G[Tile sink]
    F --> H[TileJSON metadata]

The pipeline uses bounded windows between production and writing. An error from a reader or encoder reaches the writer in production order, so the sink does not finish a partial output. Progress changes from feature rendering to tile writing after all records have been produced.

Tilegen also supports rendering a bounded tile block in memory. This path uses the same plan, phase-one renderer, sorter, and phase-two encoder, then returns only tiles that contain features in the block.

References: martin/…/mod.rs• L321-387 martin/…/mod.rs• L547-642 martin/…/mod.rs• L689-702 martin-tilegen/…/generate.rs• L70-129 martin-tilegen/…/generate.rs• L146-228 martin-tilegen/…/block.rs• L12-43

Generation plan

The generation plan is the contract between a source and the tile renderer. It maps source tables to output layers and defines each layer’s zoom range, geometry policy, properties, filters, rules, IDs, and feature order. Plan creation validates layer names, zoom limits, attributes, and expressions before generation starts.

Martin builds the plan by lowering table and layer configuration. Table defaults apply first, then each layer can narrow its zooms or replace geometry, grid, clipping, attributes, rules, per-tile operations, and sort settings. A layer with no zoom in the requested range is left out, and unsupported geometry choices stop that layer from being used in bulk mode.

The plan also narrows the source scan. It records the columns needed by layer output and expressions, then exposes a row filter when the layer conditions can be expressed in source terms. A source must read all rows when no safe source condition exists.

Plan concernResult during generation
Table and layer mappingA source feature can feed one or more layers.
Layer settingsGeometry and properties are prepared for rendering.
Required columnsThe source scans only columns the plan may use.
Row filterThe source can skip rows that no eligible layer can take.
Output keysProperty names are stable for each output layer.

References: martin/…/layers.rs• L44-101 martin/…/layers.rs• L104-197 martin/…/mod.rs• L589-642 martin-tilegen/…/mod.rs• L54-126 martin-tilegen/…/mod.rs• L241-296 martin-tilegen/…/mod.rs• L351-479

Expression compilation

Expressions use CEL over feature properties. The plan compiles each expression once and records the property names it reads, then workers evaluate the compiled form for each feature. Bare property names and feature['key'] access known properties. Dynamic key access can require all feature keys at run time.

Compilation rejects invalid syntax, unknown functions, and unknown properties when the table does not allow dynamic properties. A run-time evaluation or type conversion failure becomes null, is counted in the summary, and is warned about once.

References: martin-tilegen/…/mod.rs• L1-23 martin-tilegen/…/compile.rs• L35-129 martin-tilegen/…/eval.rs• L102-139 martin/…/mod.rs• L390-415

Filters and source pushdown

A layer filter decides whether a feature enters that layer. Tilegen evaluates the filter for correctness, and it also tries to translate the filter into a row condition that the source can apply before feature data reaches the renderer.

Pushdown supports conditions that preserve the expression’s null behavior, such as typed comparisons, null checks, literal membership, and selected string operations. If a filter uses a value or operation that cannot be represented safely by the source, the plan marks it untranslatable and the source reads all rows for that table.

When Martin serves tiles, it builds a source query for each zoom from the plan’s row filter. A feature limit can be used only when the filter has a safe source form. Otherwise serving rejects the limit request instead of applying a limit that could remove eligible features. Bulk mode does not accept max_feature_count.

References: martin/…/serve.rs• L30-85 martin/…/mod.rs• L190-201 martin-tilegen/…/filter.rs• L14-86 martin-tilegen/…/mod.rs• L547-565 martin-tilegen/…/pushdown.rs• L8-21 martin-tilegen/…/pushdown.rs• L89-136

Branching and rule selection

Branching describes the paths a feature can take through a layer. The layer filter is checked first. Then rules are checked in declaration order, and the first matching rule supplies its zoom, geometry thresholds, and computed attributes. If no rule matches, the layer defaults apply.

Branch analysis also describes source rows and geometry shape bands. These paths help callers understand which rows may reach each outcome, but they do not replace per-feature evaluation. A feature can share source rows with more than one possible path until its values and geometry decide the result.

References: martin-tilegen/…/eval.rs• L141-190 martin-tilegen/…/eval.rs• L277-363 martin-tilegen/…/branch.rs• L23-48 martin-tilegen/…/branch.rs• L106-145

Row cases

A row case is a saved view of one source row and the paths and outputs that the layer plan produces for it. The spec flow renders the row through the same plan and records its decoded tile features, zoom ranges, attributes, and per-tile operation counts so a change in layer behavior can be reviewed as a data change.

The case input contains the columns read by bulk mode and the source geometry. A row can produce separate outputs for separate layers, while an attribute that exists only on part of a feature’s zoom range records that range explicitly.

References: martin/…/file.rs• L13-39 martin/…/file.rs• L75-103 martin/…/render.rs• L51-117 martin/…/render.rs• L145-196

Phase-one geometry work

Phase one converts each accepted feature into records that describe its pieces in output tiles. It projects source geometry into the renderer’s unit space, applies per-feature settings, and renders each feature for the zooms allowed by its layer and rule.

The layer settings that enter phase one include geometry selection, clipping, simplification, minimum size, derived points, and zoom-dependent rules. Per-tile operations are attached to the layer plan for later tile-level work. Geometry choices that Martin bulk mode cannot lower are rejected before rendering begins.

The renderer simplifies and quantizes lines and polygons, clips them when the layer uses clipped mode, and accounts for the layer buffer. Points near a tile edge can reach neighboring tiles. Polygon interiors can become fill ranges so large covered areas do not need one record per tile. Features below the minimum size stop at the first zoom where they are too small. A geometry slice or coordinate error skips that feature at the affected zoom while other work continues.

References: martin/…/layers.rs• L125-196 martin/…/layers.rs• L273-306 martin/…/layers.rs• L374-479 martin-tilegen/…/mod.rs• L24-115 martin-tilegen/…/render.rs• L50-65 martin-tilegen/…/render.rs• L446-536 martin-tilegen/…/render.rs• L593-671 martin-tilegen/…/render.rs• L786-843 martin-tilegen/…/render.rs• L1009-1055

For MVT output, ring start behavior can preserve the source or follow the convention used by the compatible geometry tool. This choice affects equivalent geometry bytes without changing the feature’s shape.

References: martin/…/mod.rs• L112-120 martin/…/mod.rs• L157-159 martin-tilegen/…/generate.rs• L25-34 martin-tilegen/…/generate.rs• L82-92 martin-tilegen/…/render.rs• L438-444 martin-tilegen/…/render.rs• L826-879

Sort strategy

The sort stage is the bridge between geometry work and tile encoding. Each record has a key ordered by tile, layer, and source sequence. Workers keep records in local buffers, spill sorted runs when buffers fill, and merge the runs into one ordered stream.

Bulk mode divides the available memory across worker sort buffers and uses temporary directories for spilled runs. Its bounded work window limits how far rendering can run ahead of tile writing, while the merge fan-in controls how many runs are combined at once. The bulk command keeps each worker buffer within supported bounds and uses a fixed batch size and work window for tile output.

The source sequence is built from partition and row position. It restores source order after parallel reads and keeps equal feature pieces together. The tile grouper expands polygon fill ranges, adds them to every covered tile, and emits tile batches bounded by tile count and record bytes.

References: martin/…/mod.rs• L49-56 martin/…/mod.rs• L514-530 martin/…/mod.rs• L589-637 martin-tilegen/…/key.rs• L23-92 martin-tilegen/…/mod.rs• L24-119 martin-tilegen/…/group.rs• L24-76 martin-tilegen/…/group.rs• L120-188

Phase-two assembly and encoding

Phase two consumes one tile’s grouped records and builds one layer at a time. It joins consecutive records from the same source feature into a multi-part geometry, derives the tile’s property columns, applies feature sort order, and writes the result as MLT or MVT. Tilegen then applies the requested tile compression before the sink receives the bytes.

The encoder keeps per-layer zoom, feature, and field statistics. It uses those statistics to complete TileJSON metadata after all tile work finishes. Equal adjacent tile content can be reused, and fill-only tile bytes can share a deduplication key across the run. MVT can retain declared but unused keys when that output option is enabled.

References: martin-tilegen/…/mod.rs• L26-105 martin-tilegen/…/encode.rs• L41-88 martin-tilegen/…/encode.rs• L128-246 martin/…/mod.rs• L473-493 martin/…/mod.rs• L500-511

Feature sort order

sort_by controls the order of features inside one layer and tile. Tilegen evaluates each sort expression during phase one and stores its encoded key with every feature piece. Phase two applies that key after it has merged all pieces of each feature, so a multi-part feature moves as one feature.

Sort keys compare in the declared order. Null and failed values sort before ordinary values, and descending reverses that order. Ties retain source sequence order. When sort_by is present, the layer’s encoder-level spatial and ID sort trials are disabled because the explicit feature order is the contract to preserve.

References: martin/…/layers.rs• L174-186 martin-tilegen/…/mod.rs• L122-126 martin-tilegen/…/mod.rs• L608-642 martin-tilegen/…/eval.rs• L237-243 martin-tilegen/…/assemble.rs• L35-86

Change checklist

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

  • Keep the sort-key fields aligned with tile grouping and feature-piece assembly. A change can silently change layer order or source-order ties.
  • Keep filter translation aligned with expression evaluation and source row filtering. A new expression form must not cause the source to skip rows that runtime evaluation would keep.
  • Keep rule precedence aligned between branch descriptions and per-feature evaluation. The first matching rule must remain the same in both paths.
  • Keep property key collection complete before phase two starts. Encoder metadata and tile columns depend on the final layer key names.
  • Keep explicit sort_by behavior separate from encoder-level sort trials. Otherwise tile output can violate the configured feature order.
  • Keep block rendering aligned with full generation for tile order, geometry options, and summary counts. The in-memory path must not produce a different tile contract.
  • Keep the lowered bulk plan and the served per-zoom plan aligned. A setting accepted in one path must not silently change layer eligibility or source row selection in the other.
  • Keep spec cases tied to decoded output and per-tile counts. A change in plan lowering can affect both the feature result and the recorded path.
  • Keep bulk source rules aligned with command limits. New bulk options must not silently accept function sources, mixed databases, unsupported grids, or discontinuous zooms.
  • Keep output cleanup aligned with sink creation. A new temporary or sidecar file must also be removed on failure and interruption.

Important failure modes

TriggerConsequenceGuard
A filter cannot be translated to a source conditionThe source reads all rows for that table.Treat Untranslatable as a correctness result, not as an empty filter.
An expression fails during feature evaluationThe value becomes null, and the failure is counted.Review the generation summary and the first warning for the affected layer.
Geometry cannot be quantized or sliced at a zoomThat feature is skipped at that zoom while other zooms may continue.Check slice error counts and the first reported source position.
A sort buffer cannot spill or mergeGeneration stops before the sink finishes.Provide valid temporary directories and a workable buffer and fan-in setting.
A feature has several tile piecesWithout grouping, it could become several output features.Keep records for one source sequence consecutive through assembly.
The final key set is not ready before encodingTile columns or generated layer metadata can be incomplete.Start phase two only after all render workers finish.
A layer uses geometry that bulk mode cannot lowerPlan creation stops before the run starts.Use a geometry choice supported by the bulk plan or use the serving path.
max_feature_count is used with a filter that has no safe source formServing cannot cap rows without risking a wrong result.Make the filter source-translatable or omit the feature limit.
Bulk mode receives a function source, mixed databases, an unsupported grid, or no usable layerSource selection stops before generation.Select table or view sources from one Web Mercator database and use a supported zoom range.
A bulk run is interrupted or fails after output creationThe output may be partial.Let the run remove the output and its journal, WAL, and shared-memory files.
Compressed MLT is requested for PMTilesThe output cannot be served by Martin.Use uncompressed MLT for PMTiles.

Key files

FileWhy to read it
martin/…/mod.rsBulk generation lifecycle, source selection, option limits, resource limits, output creation, and failure cleanup.
martin/…/layers.rsTranslation from table and layer settings into the generation plan.
martin/…/serve.rsServed tile plan, per-zoom source queries, and feature-limit rules.
martin/…/file.rsSaved row-case shape used to describe paths and generated outputs.
martin/…/render.rsThe row-case path through the generator and the conversion from tiles to reviewable outputs.
martin-tilegen/…/generate.rsMain generation lifecycle and phase boundary.
martin-tilegen/…/block.rsBounded in-memory generation path for tile blocks.
martin-tilegen/…/mod.rsPlan model, validation, column selection, and filter access.
martin-tilegen/…/eval.rsPer-feature layer selection, rules, attributes, IDs, and sort keys.
martin-tilegen/…/compile.rsExpression validation and property dependency discovery.
martin-tilegen/…/pushdown.rsSafe translation from expressions to source conditions.
martin-tilegen/…/mod.rsPhase-one worker boundary, geometry projection, and render totals.
martin-tilegen/…/render.rsGeometry-to-tile-piece behavior, fill ranges, and MVT ring handling.
martin-tilegen/…/mod.rsSpill, merge, and sort-buffer behavior.
martin-tilegen/…/group.rsTile grouping and fill-range expansion.
martin-tilegen/…/mod.rsPhase-two setup and TileJSON completion.
martin-tilegen/…/assemble.rsFeature-piece assembly and in-tile feature order.
martin-tilegen/…/encode.rsTile format, compression, reuse, deduplication, and statistics.