Tilegen inputs and output sinks
Tilegen turns an ordered stream of source features into a complete vector tile archive. A source supplies geometry and typed properties, while a plan selects how those features appear in layers; the generation run then writes encoded tiles and final TileJSON metadata to either MBTiles or PMTiles. See Tile generation for the wider system view and Tilegen pipeline and plans for the render and plan stages.
Source data
The source side is a stream contract. A FeatureSource reports a set of partitions and reads each partition into feature batches, so tilegen can read source data in parallel without changing the source order used for drawing. The source does not provide tiles. It provides the feature data from which tiles are made.
Feature streams
Each source feature has an optional numeric id, one geometry, and a set of properties. A FeatureBatch also identifies the source table, the source row range, the coordinate system, and the partition that supplied the features; these values let the plan map source records to output layers and let errors point back to source data. References: martin-tilegen/…/source.rs• L174-212
Partitions may be read by several readers. The required contract is that partition order and row order still reproduce the source order, even when readers finish at different times. This keeps output deterministic and preserves source order where a layer does not define its own feature order. References: martin-tilegen/…/source.rs• L195-227, martin-tilegen/…/generate.rs• L124-180
Geometry inputs
The source accepts points, lines, polygons, their multi-part forms, and well-known binary (WKB). A batch marks its geometry as WGS84 or Web Mercator. Tilegen converts WGS84 data during rendering, so a source can provide geographic coordinates while the output remains in the tile system expected by vector tiles. References: martin-tilegen/…/source.rs• L35-55, martin-tilegen/…/source.rs• L57-89
Empty geometries have no drawable content and are skipped. A non-empty geometry collection is not a valid feature geometry for a tile, and invalid WKB stops the generation run with a source-related error. References: martin-tilegen/…/source.rs• L92-108, martin-tilegen/…/source.rs• L127-170
Property model
Properties carry the source attributes that layer rules, filters, sort expressions, and tile encoders use. Tilegen keeps values typed and gives each property key a stable id during one generation run, then uses the resolved names when it builds tile fields and metadata.
Typed values
The supported source value kinds are Boolean, signed integer, 32-bit float, 64-bit float, and string. A property can be absent from a feature; expressions and layer attribute rules decide whether a value is kept, changed, or omitted from a given layer and zoom. References: martin-tilegen/…/props.rs• L45-78, martin-tilegen/…/mod.rs• L210-239
Property keys
The plan declares known source columns and can also allow keys from dynamic properties such as a JSON column. Declared keys receive the first ids in declaration order, which preserves their column order in output tiles. Keys found later are added as dynamic keys and are resolved before tile encoding starts, so parallel readers use the same names and ids. References: martin-tilegen/…/props.rs• L86-172, martin-tilegen/…/mod.rs• L54-88, martin-tilegen/…/mod.rs• L26-56
Output sink contract
An output sink receives finished EncodedTile values, not source features. Each value contains a tile coordinate, encoded and compressed bytes, and an optional deduplication key. The shared TileSink contract lets the generation engine use different archive formats while keeping tile assembly, compression, duplicate detection, and metadata completion in the common flow.
The main flow moves data from the source to the selected archive sink:
flowchart LR
S[Feature source] --> R[Render and group features]
R --> E[Encode and compress tiles]
E --> M[MBTiles sink]
E --> P[PMTiles sink]
M --> MM[TileJSON metadata]
P --> MM
Sink lifecycle
The sink declares the tile order that it needs, and the generation engine sorts encoded batches to that order before it writes them. It then passes batches to write_all; an error batch means that upstream generation failed and the sink must stop without treating the partial stream as a result. After the last tile, the caller passes complete TileJSON metadata to finish. References: martin-tilegen/…/mod.rs• L17-36, martin-tilegen/…/generate.rs• L64-107
If source reading, rendering, sorting, or encoding fails, the sink receives an error instead of a complete tile stream and must stop. The run therefore does not treat a partially written archive as a successful output. References: martin-tilegen/…/mod.rs• L27-36, martin-tilegen/…/error.rs• L29-80
Tile order and duplicate content
The sink chooses the order that best matches its archive layout. MBTiles uses TMS order, while PMTiles uses Hilbert order. Tilegen sorts for that order before writing, and it can mark equal tile bytes with a shared deduplication key so a sink can store repeated content once. References: martin-tilegen/…/tile.rs• L30-49, martin-tilegen/…/encode.rs• L51-75, martin-tilegen/…/mod.rs• L17-25
MBTiles output
The MBTiles sink creates a new MBTiles database and writes encoded tiles through the selected MBTiles schema. Its options also control archive hashing and metadata edits, so the final database can carry both generator metadata and caller-supplied changes.
MBTiles archive
MBTiles output is written in TMS order because that follows the archive's tile table key. The default output uses a deduplicated schema and computes agg_tiles_hash; callers can select another schema, disable that hash, or set and delete metadata keys. A destination that is not empty is rejected, which protects an existing tileset from accidental replacement. References: martin-tilegen/…/mbtiles.rs• L16-53, martin-tilegen/…/mbtiles.rs• L67-108
The sink writes tile batches as one bulk operation and hashes the same tile stream when the archive option requests it. If a tile batch reports an upstream error, the write does not become a completed result. After all tiles succeed, finish inserts generated TileJSON metadata, completes or updates the aggregate tile hash when enabled, and applies the requested metadata edits. References: martin-tilegen/…/mbtiles.rs• L111-169
PMTiles output
The PMTiles sink writes a new PMTiles v3 archive as a single file. It maps the requested tile format and compression to the PMTiles header, writes tiles in clustered Hilbert order, and uses the generation run's deduplication keys for shared tile content.
PMTiles archive
PMTiles supports MLT or MVT tiles and the uncompressed, gzip, Brotli, or Zstandard encodings supported by the sink. Zlib is not accepted for this output. The destination must not already exist, so each run creates a new archive rather than adding to an old one. References: martin-tilegen/…/pmtiles.rs• L17-41, martin-tilegen/…/pmtiles.rs• L77-97
On completion, the sink stores the serialized TileJSON, zoom range, bounds, and a center point in the PMTiles archive before finalization. Finalization is the step that turns the stream of written tiles into a complete PMTiles file. References: martin-tilegen/…/pmtiles.rs• L45-74
Archive metadata
Tilegen completes metadata from the tiles it actually produces. It records the output format, the generated zoom range, and the vector layers with their observed fields and field types, then passes that result to the selected sink.
TileJSON metadata
Layer statistics come from encoded tile content. This means metadata describes the generated result rather than only the source schema: a layer that produces no tiles is left out, and fields are reported with the types observed in its output. References: martin-tilegen/…/encode.rs• L79-123, martin-tilegen/…/mod.rs• L71-116
The same completed metadata serves both archive formats. MBTiles stores it as archive metadata and then applies its configured metadata edits, while PMTiles stores it in the archive header data together with bounds, center, and zoom values. References: martin-tilegen/…/mbtiles.rs• L156-169, martin-tilegen/…/pmtiles.rs• L60-74
Change checklist
When these files or sections are changed, remember to consider these:
- When source columns or dynamic keys change, check the table plan, layer attributes and expressions, field names, and the generated
vector_layersmetadata together. - When a tile format or compression option changes, check both the common TileJSON
formatvalue and the encoding accepted by each sink. - When tile order changes, check the sorter order and the archive layout assumptions in both sink implementations.
- When duplicate-content handling changes, check that both MBTiles and PMTiles still receive keys only for byte-identical tiles.
- When MBTiles schema, hash, or metadata edit options change, check their ordering against generated metadata and aggregate hash finalization.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A destination already contains data | The run refuses to create the output | Use a new empty MBTiles path or a new PMTiles path |
| A source emits invalid WKB or an unsupported geometry collection | Generation stops before a complete archive is finished | Validate source geometry before the run |
| A source or render stage fails after tile writing starts | The sink stops and the current output is not a successful tileset | Treat the run result and sink finalization as one transaction |
| PMTiles is requested with zlib encoding | Sink creation fails because PMTiles does not support that encoding here | Select an accepted PMTiles encoding |
| Source rows or partitions exceed tilegen's ordering limits | Generation fails because source order cannot fit the sort key | Keep source partition and row ranges within the generator limits |
| MBTiles tiles are not in TMS order | The archive write violates the sink's ordering contract | Preserve the engine's requested sink order |
| Metadata edits set or delete a hash key | The generated aggregate hash is not written for that key | Treat explicit hash metadata edits as authoritative |
Key files
| File | Why to read it |
|---|---|
| martin-tilegen/…/source.rs | Defines the feature, geometry, batch, partition, and source-reading contract. |
| martin-tilegen/…/props.rs | Defines typed property values and the run-wide property-key model. |
| martin-tilegen/…/mod.rs | Shows how source columns and properties connect to output layers. |
| martin-tilegen/…/generate.rs | Connects source reading, tile production, sink writing, and metadata completion. |
| martin-tilegen/…/mod.rs | Defines the common output-sink lifecycle and encoded tile contract. |
| martin-tilegen/…/mbtiles.rs | Describes MBTiles creation, ordering, deduplication, hashing, and final metadata edits. |
| martin-tilegen/…/pmtiles.rs | Describes PMTiles creation, supported output settings, and final archive metadata. |
| martin-tilegen/…/mod.rs | Explains how generated tile statistics become TileJSON layer metadata. |