Tile generation
Tile generation turns source features and a declarative plan into sorted and encoded tile archives. Martin uses the same tile intent for bulk archive creation and direct tile serving, while the tile engine manages feature processing, tile assembly, and output completion.
Generation flow
The generation flow connects source selection to a finished archive or a bounded tile response. Martin prepares the run and selects the output boundary. The tile engine then renders features, orders tile records, encodes tiles, and reports the result.
The main flow moves through these stages:
flowchart LR
A[Source features] --> B[Declarative layer plan]
B --> C[Feature rendering]
C --> D[Sorted tile records]
D --> E[Encoded tile layers]
E --> F[MBTiles or PMTiles archive]
B --> G[Serving query]
G --> H[Bounded tile response]
Bulk mode resolves the requested area and zoom range before it starts generation. A failed or interrupted run removes partial output, so downstream readers do not treat an incomplete archive as a finished result.
References: martin/…/mod.rs• L321-387 martin/…/mod.rs• L547-642 martin/…/mod.rs• L689-702
Source and plan boundary
The source provides feature rows and their geometry, properties, and stable order. The declarative layer plan defines how those features become named output layers. It also sets the zoom range, filters, selected properties, geometry choices, and ordering rules.
This boundary keeps bulk generation and serving aligned. Martin discovers usable table sources and lowers their settings into plans. Layers outside the requested zoom range do not enter the run, and unsupported bulk sources or geometry choices fail before feature processing.
Layer plan lowering
Layer plan lowering converts source and layer settings into the tile engine's shared plan. Table settings provide the base tile grid, bounds, zooms, clipping, and encoding choices. Layer settings then describe feature selection and output behavior.
The lowering step also exposes invalid combinations early. It limits the plan to the active run and warns when an operation cannot affect the selected geometry.
References: martin/…/layers.rs• L44-101 martin/…/layers.rs• L104-197 martin/…/layers.rs• L273-338 martin/…/layers.rs• L374-478
Feature batches
Feature batches connect a source to the plan during processing. A batch identifies its source partition and preserves row order so parallel work can still produce stable output. The engine accepts point, line, and polygon features, including encoded WKB.
Empty geometry produces no tile output. Geometry that cannot be decoded or supported stops the run unless the renderer can report it as a recoverable zoom-specific issue.
References: martin-tilegen/…/source.rs• L35-108 martin-tilegen/…/source.rs• L174-242
Expression planning
Expressions control feature filters, property values, sort keys, and layer choices. The engine checks expressions when it creates the plan, then evaluates them for each feature. Missing properties have a defined null value, while unknown names fail unless the plan allows dynamic properties.
Common expressions can use a faster evaluation path. This does not change the expression contract because both paths produce the same plan results.
References: martin-tilegen/…/compile.rs• L49-143 martin-tilegen/…/fast.rs• L1-4 martin-tilegen/…/fast.rs• L130-165 martin-tilegen/…/view.rs• L13-24
Feature rendering
Feature rendering changes source geometry into tile-local pieces. It projects supported coordinates into the tile grid, applies zoom and bounds, and skips empty features.
Lines and polygons can be simplified before they are quantized. Features that cross tile edges are split into tile pieces. Layer rules and per-tile operations then decide which pieces remain in each tile. Recoverable geometry issues appear in the run summary; source decode errors and other fatal geometry errors stop generation.
References: martin-tilegen/…/mod.rs• L63-114 martin-tilegen/…/render.rs• L446-536 martin-tilegen/…/render.rs• L593-671 martin-tilegen/…/render.rs• L786-842 martin/…/layers.rs• L174-195
Property flow
Feature properties keep their types while they move from source features into encoded layers. Each layer gives property names stable internal identities so workers can pass compact records without repeating names.
The encoder freezes the property name set before it builds layer columns and metadata. Declared keys keep their configured order. Keys discovered from input use a stable name order.
References: martin-tilegen/…/props.rs• L45-77 martin-tilegen/…/props.rs• L86-172 martin-tilegen/…/mod.rs• L38-59 martin-tilegen/…/encode.rs• L108-125
See Tilegen pipeline and plans for pipeline phases, expressions, filters, branching, and sort strategy. See Tilegen inputs and output sinks for source data and archive boundaries.
Sorted tile assembly
Sorted tile assembly combines independently rendered feature pieces into complete tiles. Each record carries enough source and tile order information to keep output stable when workers run in parallel.
The generator bounds memory by turning full sort buffers into temporary sorted runs. It later merges those runs before writing. The sink determines the archive order: TMS order supports MBTiles, while Hilbert order supports PMTiles.
References: martin-tilegen/…/generate.rs• L80-103 martin-tilegen/…/render.rs• L1093-1105 martin-tilegen/…/mod.rs• L41-72 martin-tilegen/…/mod.rs• L17-36 martin/…/mod.rs• L514-530
Tile encoding and metadata
Tile encoding groups sorted records by layer and builds one payload for each layer in each tile. It can produce Mapbox Vector Tile (MVT) or Martin Layer Tile (MLT) data with the selected compression.
The encoder records the layers, zooms, feature counts, and field types that it writes. The completed statistics then form the TileJSON metadata that the sink stores with the archive.
Repeated tile bytes can share a deduplication key when the bytes match. A reused tile still contributes its features to statistics for its own zoom.
Archive finalization
Archive finalization writes completed tiles in the sink's required order and stores the final metadata. Martin selects MBTiles or PMTiles from the output request and rejects options that do not belong to that format.
MBTiles can use its supported storage and metadata options. PMTiles has a separate option set. MLT in PMTiles remains uncompressed so Martin can serve it.
An interrupted or failed run removes the archive and related database files. A downstream reader therefore sees either a completed archive or no archive from that run.
References: martin-tilegen/…/encode.rs• L54-79 martin-tilegen/…/encode.rs• L143-246 martin-tilegen/…/mod.rs• L75-120 martin-tilegen/…/mod.rs• L17-36 martin-tilegen/…/mbtiles.rs• L19-59 martin/…/mod.rs• L90-126 martin/…/mod.rs• L467-511
Runtime coordination
Runtime coordination links source readers, render workers, encoder workers, and the writer with bounded hand-offs. The roles overlap so the system can read new data while it sorts or encodes earlier data.
Bulk progress covers preparation, rendering, sorted-run merging, tile writing, and metadata finalization. The run summary also records skipped features, recoverable geometry errors, expression failures, and per-tile operation counts.
Serving uses the same plan in a bounded path. It creates a source query for each zoom and pushes supported filters into SQL. A feature limit requires a filter that has a matching SQL form.
References: martin-tilegen/…/generate.rs• L61-68 martin-tilegen/…/generate.rs• L146-228 martin-tilegen/…/block.rs• L12-43 martin/…/mod.rs• L390-434 martin/…/mod.rs• L645-702 martin/…/progress.rs• L44-137 martin/…/serve.rs• L21-85
Row-level specifications
Row-level specifications provide a bounded view of what one source row produces. A specification records the layer paths, output features, zoom range, attributes, and per-tile operation counts for that row.
The spec renderer processes the row in memory and decodes the resulting tiles. It stops when the case exceeds its fixed tile limit, which keeps review bounded while full generation remains available for archive work.
References: martin/…/file.rs• L13-39 martin/…/file.rs• L75-103 martin/…/render.rs• L51-117 martin/…/render.rs• L145-223
See OpenStreetMap tile generation for the PBF input and Shortbread-style plan that use this subsystem for OpenStreetMap archives.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep tile order consistent across rendering, sorting, coordinate conversion, and every archive sink.
- Preserve property key order and the point at which the key set becomes fixed.
- Keep expression analysis, direct evaluation, CEL evaluation, and dynamic-key behavior aligned.
- Preserve stable feature order when source partitions or reader concurrency change.
- Keep recoverable render errors aligned with run summaries and produced tiles.
- Keep encoded statistics aligned with TileJSON layers, zooms, field types, and reused tiles.
- Keep bulk plans and serving queries aligned for geometry, zoom, filters, and selected properties.
- Preserve removal of partial archives when output creation, writing, or interrupt handling changes.
- Keep spec paths, zoom ranges, and per-tile counts aligned with generated output.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Source geometry cannot be decoded | Generation stops before final output | Validate geometry encoding and source coordinates before the run |
| Geometry cannot be represented at a zoom | That feature-zoom is skipped and reported | Review slice errors and geometry limits |
| A property key appears after encoding starts | The layer key map cannot complete safely | Freeze property names after rendering |
| Sort order differs from sink order | Tiles can receive the wrong archive position | Use one sink-selected order throughout the run |
| A layer uses unsupported geometry settings | Plan creation fails | Check the layer against supported bulk geometry choices |
| A serving filter has no SQL form with a feature limit | The bounded source query cannot be built | Use a SQL-translatable filter or remove the limit |
| Output MBTiles is not empty | The sink refuses to overwrite it | Use a new path or clear the archive first |
| Format-specific options target another format | Validation fails before generation | Keep MBTiles options on MBTiles output |
| Encoding or sink writing fails | The archive is not finalized | Treat the run as failed and remove partial output |
| A spec case exceeds its tile limit | Row-level review fails instead of returning partial data | Lower the reviewed zoom range or use full generation |
Key files
| File | Why to read it |
|---|---|
| martin/…/mod.rs | Defines bulk options, source selection, output setup, coordination, and cleanup. |
| martin/…/layers.rs | Shows how source and layer settings become the shared tile plan. |
| martin/…/serve.rs | Shows how the plan becomes bounded serving queries. |
| martin-tilegen/…/generate.rs | Defines the generation run and its worker stages. |
| martin-tilegen/…/mod.rs | Connects source batches to rendering and sortable records. |
| martin-tilegen/…/render.rs | Shows the geometry work that creates tile pieces. |
| martin-tilegen/…/mod.rs | Connects sorted batches, encoding, and metadata completion. |
| martin-tilegen/…/encode.rs | Shows layer assembly, MVT or MLT output, statistics, and deduplication. |
| martin-tilegen/…/compile.rs | Defines plan-time expression checks and feature evaluation. |
| martin-tilegen/…/props.rs | Defines typed properties and the layer property-name contract. |
| martin-tilegen/…/source.rs | Defines the source batch contract and accepted geometry forms. |
| martin-tilegen/…/mod.rs | Defines the archive sink contract and encoded-tile hand-off. |
| martin-tilegen/…/mbtiles.rs | Shows MBTiles creation, ordered writes, metadata, and hashing. |
| martin-tilegen/…/block.rs | Shows the bounded tile-window path for serving. |
| martin/…/render.rs | Shows bounded row-level rendering and spec output. |
| docs/tilegen-pipeline-and-plans.md | Provides the detailed view of phases and declarative plans. |
| docs/tilegen-inputs-and-output-sinks.md | Provides the detailed view of inputs and archive sinks. |
| docs/openstreetmap-tile-generation.md | Provides the OpenStreetMap-specific generation flow. |