File and remote tile sources
File and remote tile sources turn tile archives, geospatial data, raster images, or upstream tile URLs into the common source model used by Martin. Each source publishes TileJSON and tile format information, answers z/x/y requests, and declares which zoom levels may use the shared tile cache. See Tile serving for the common request model, PostgreSQL tile sources for database-backed SQL sources, and Raster terrain and transcoding for raster processing after a source returns a tile.
Source model
The source model gives every backend the same place in the server. A source has an ID, map metadata, a tile format, a tile grid, cache zoom limits, and an operation that returns tile bytes or an empty tile.
Source lifecycle
Martin resolves a configured location into a live source before it serves requests. The load step reads enough metadata to describe the source and prepares the state needed for tile reads. Some sources can rebuild themselves when the underlying file or remote object changes.
The lifecycle has three broad stages:
- Discover configured files, objects, or upstream URLs.
- Open or inspect the source and publish its TileJSON in the source catalog.
- Read, generate, or proxy tiles for requests until a change requires a reload.
The source manager replaces changed sources and invalidates their cached tiles. A failed source can produce a warning while other valid sources remain available when the configuration uses warning behavior for invalid sources.
References: martin-core/…/source.rs• L15-26 martin-core/…/source.rs• L55-105 martin/…/content.rs• L615-633
Archive and feature sources
Archive sources serve tiles that already exist, while feature sources create vector tiles from source data. Their loading cost and request work differ, but both use the same cache and HTTP response path.
MBTiles and PMTiles
MBTiles is a local SQLite tile archive. Martin opens it for read access, reads its metadata and tile format at load time, then looks up one stored tile for each request. A missing row becomes an empty tile. Directory watching can rebuild a source when a local .mbtiles file changes. The archive must expose usable tile metadata and a supported format before Martin publishes the source.
PMTiles is a single tile archive that can live on local disk or in an object store such as S3-compatible storage. Martin reads its header and metadata when it opens the archive, then uses the archive directory to find tile byte ranges. A shared PMTiles directory cache avoids repeated directory reads; this cache is separate from the response tile cache. Local reads detect a changed file while reading, while remote prefixes are listed on a polling interval. A remote archive that reports a modification causes the source to reload.
Both archive types can expose stored vector or raster formats supported by the archive reader. Their configured cache zoom range controls whether the shared response cache stores the returned tile.
References: martin-core/…/source.rs• L60-115 martin-core/…/source.rs• L119-157 martin-core/…/backend.rs• L13-18 martin-core/…/backend.rs• L34-52 martin-core/…/source.rs• L60-90 martin-core/…/source.rs• L108-172 martin-core/…/source.rs• L195-232 martin-core/…/cache.rs• L17-40 martin/…/pmtiles.rs• L16-24
GeoJSON sources
GeoJSON sources turn a local GeoJSON file into vector tiles. Martin loads the feature set at startup, changes coordinates to Web Mercator, and builds a spatial index. A tile request searches that index, clips the matching geometries to the tile area, and encodes the result as one MVT layer named for the source.
The source keeps the prepared features in memory, so requests do not parse the file again. The configured tile extent and edge buffer affect the encoded tile and the geometry margin near tile edges. Local file watching rebuilds the in-memory source after a file change. Empty search results become empty tiles and can be handled by the shared tile cache.
References: martin-core/…/source.rs• L24-35 martin-core/…/source.rs• L50-116 martin-core/…/source.rs• L127-169 martin/…/geojson.rs• L12-15 martin/…/geojson.rs• L50-64
DuckDB and GeoParquet sources
DuckDB sources generate MVT tiles by running a prepared SQL query against a DuckDB connection pool. A database file can publish selected tables and table macros, or discover suitable tables when auto-publish is enabled. A GeoParquet entry can point to a local file or a remote URL. Startup inspection finds the geometry column, spatial reference, layer fields, and bounds before Martin publishes TileJSON.
When auto-publish is enabled, Martin scans the DuckDB catalog for non-internal tables with geometry columns and for tile table macros. It skips tables and macros already named by explicit configuration. Table source IDs follow the configured ID pattern, and an optional list of integer columns supplies the feature ID. If no requested column has an integer type, the table still publishes without that feature ID. Schema filters apply without regard to case. These rules let one database expose useful layers without requiring a separate entry for every table.
For a table or GeoParquet source, a request supplies its tile coordinates to the prepared query. The query returns tile bytes, so the request does not need to assemble individual features in the server. A macro source follows the same contract but calls a macro with z, x, and y. The pool controls concurrent database work; the shared tile cache controls reuse of completed responses.
DuckDB sources do not use URL query parameters as a dynamic request input. Remote GeoParquet is read through DuckDB's remote file support, while local database files are opened read-only. Configuration or introspection errors are isolated to the affected source during resolution.
References: martin/…/plan.rs• L13-18 martin/…/plan.rs• L38-55 martin/…/plan.rs• L112-177 martin/…/plan.rs• L180-275 martin/…/database.rs• L17-39 martin/…/database.rs• L90-145 martin/…/resolve.rs• L29-71 martin/…/resolve.rs• L127-157 martin-core/…/source.rs• L46-93
Raster and remote URL sources
Raster sources read image tiles from geospatial files, while passthrough sources fetch tiles from another tile service. Both can use remote storage, but only passthrough makes an HTTP tile request for each cache miss.
COG sources
COG sources serve tiles from Cloud Optimized GeoTIFF data on the WebMercatorQuad grid. A source can read one local or remote COG, or a mosaic that maps a tile pattern to many COG files. Startup reads raster metadata and publishes bounds, tile format, and zoom limits. JPEG and WebP tiles can pass through; other supported image data is returned as PNG unless the source requests a new encoding. A COG with an unsupported tile format does not resolve.
A COG mosaic opens files on demand and keeps a bounded set of opened or known-missing files. File expiry and idle time limit how long those entries remain usable. A request for a tile outside the source zoom range, or for a missing mosaic file, returns an empty tile. Local files use file events. Remote objects and prefixes use polling so replacements and new files can reach the catalog.
COG sources can also build a raster mosaic from a STAC API or a GeoPackage tile index. A STAC source checks the collection and a first matching item during load, then searches for assets that cover each requested tile. It caches search results for their expiry period and paints matching COGs in the configured date order. A tile-index source reads each file footprint and location into memory at load time, uses the footprints to select files for each tile, and opens those files on demand. Both forms can read local or remote assets, reproject them to Web Mercator, select bands, and keep a bounded set of opened files.
References: martin-core/…/errors.rs• L3-27 martin-core/…/source.rs• L20-26 martin-core/…/source.rs• L29-100 martin-core/…/source.rs• L102-165 martin-core/…/source.rs• L279-320 martin-core/…/stac.rs• L11-66 martin-core/…/tile_index.rs• L12-66 martin/…/cog.rs• L34-81 martin/…/cog.rs• L223-325 martin/…/cog.rs• L337-427 martin/…/cog.rs• L508-570 martin/…/cog.rs• L762-1013
Passthrough sources
Passthrough sources proxy an existing tile service without importing its tile data. A source may use one or more {z}/{x}/{y} URL templates, or a URL that returns a TileJSON document. TileJSON discovery runs when the source loads and supplies the upstream tile templates, metadata, zoom range, and bounds.
On a cache miss, Martin selects an upstream template, substitutes the requested coordinates, sends configured headers with the request, and applies the per-request timeout. It detects the tile format from configuration, response headers, or the response body. A 404 or 204 means an empty tile. Other non-success responses are errors. A usable upstream strong ETag is kept for the response; otherwise Martin hashes the tile bytes. Reloading a passthrough source fetches TileJSON again when the upstream is a TileJSON URL.
Passthrough sources have no local file discovery step. Their source-level cache settings still control use of the shared tile cache, so repeated requests can avoid the upstream call when the request key and zoom range allow caching.
References: martin/…/passthrough.rs• L54-95 martin/…/passthrough.rs• L108-140 martin/…/passthrough.rs• L209-259 martin-core/…/source.rs• L114-153 martin-core/…/source.rs• L159-221 martin-core/…/source.rs• L224-257 martin-core/…/source.rs• L286-304
Loading and change detection
Loading is backend-specific because a source must prepare a different kind of read state. Local file sources are found by extension and can be watched. Remote object sources need object-store access and use polling because the store does not provide local file events.
| Source | Initial load | Change signal |
|---|---|---|
| MBTiles | Open archive and read metadata and format | Local file events |
| PMTiles | Open local file or remote object and read archive metadata | Local file events or remote prefix polling |
| GeoJSON | Read and index all features | Local file events |
| DuckDB | Open pools and inspect tables, macros, or GeoParquet | Source resolution; remote GeoParquet reads occur during tile queries |
| COG | Read image metadata and tile layout | Local file events or remote object and prefix polling |
| Passthrough | Resolve templates or fetch TileJSON | Explicit source reload or request retry after a source change signal |
COG configuration resolves three raster collection forms in addition to individual files: filename-pattern mosaics, STAC-backed sources, and GeoPackage tile indexes. Remote COG settings use shared object-store clients for asset locations, while local index files are loaded once and retain their file-to-footprint map until the source reloads.
The reload system publishes a replacement source instead of mutating a live reader in place. This keeps requests on a valid source while the new source loads and lets the source manager clear stale tile entries.
References: martin/…/mbtiles.rs• L13-24 martin/…/geojson.rs• L12-15 martin/…/pmtiles.rs• L16-24 martin/…/cog.rs• L15-24 martin/…/cog.rs• L996-1013 martin-core/…/stac.rs• L33-66 martin-core/…/tile_index.rs• L37-66 martin-core/…/source.rs• L94-105 martin/…/tile_source_manager.rs• L168-242
Cache and request behavior
The shared tile cache sits around source reads and stores completed responses for configured zoom levels. It is not the same as a backend's read state: PMTiles caches archive directories, COG mosaics cache opened files, STAC sources cache search results, tile indexes keep their footprint maps in memory, and GeoJSON keeps prepared features in memory.
When a request arrives, Martin validates the tile grid, builds a cache key from the source set, tile coordinates, request query, accepted format, and response encoding, and checks the shared cache. On a miss it reads the source, applies configured pre-cache format processing, and stores the result when every source allows caching at that zoom. A composite request is cached only when its sources and processing path support a stable cached response.
The source can provide a native ETag or Martin can hash the returned bytes. Empty tile data becomes an HTTP 204 No Content; non-empty data includes its format, optional content encoding, and ETag. A configured per-source Cache-Control value is sent when all sources in a request agree on the same value.
References: martin/…/content.rs• L435-485 martin/…/content.rs• L508-565 martin/…/content.rs• L648-683 martin/…/cache.rs• L24-67 martin-core/…/source.rs• L69-85 martin-core/…/stac.rs• L11-31 martin-core/…/tile_index.rs• L12-35
Change checklist
When these files or sections are changed, remember to consider these:
- Keep each backend's declared tile format and TileJSON metadata aligned with the bytes it returns. Format negotiation and response headers use both values.
- Keep source cache zoom rules aligned with the reload path. Replacing a source must invalidate old entries before new data is served.
- When changing remote discovery, preserve the split between object replacement checks and prefix listing. A zero polling interval disables both remote refresh paths.
- When changing passthrough status handling, preserve the distinction between an empty upstream tile and an upstream failure. The shared cache treats them differently.
- When changing PMTiles directory caching, keep cache identity separate for each archive while allowing the configured memory budget to be shared.
- When changing STAC search or tile-index selection, keep the selected asset footprints and band order consistent with the raster mosaic that paints the tile.
- When changing COG source configuration, keep
minzoom,maxzoom, cache ranges, and re-encoding metadata aligned across direct files, mosaics, STAC sources, and tile indexes. - When changing DuckDB auto-publish rules, keep catalog discovery, explicit-source merging, source ID patterns, and feature ID selection consistent. Case-insensitive schema and object matching must remain consistent with DuckDB.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Archive metadata or tile format is invalid | The archive source does not resolve | Validate the archive during initial load and report a source warning |
| A PMTiles object changes while a tile is being read | The request returns a reload signal instead of stale directory data | Rebuild the source and invalidate its cached tiles |
A passthrough upstream returns 404 or 204 | Martin serves an empty tile and the HTTP layer returns 204 | Treat only these statuses as empty; keep other non-success statuses as errors |
| A COG has an unsupported tile format or tile encoding fails | The COG source cannot serve the affected tile or does not resolve | Validate the COG format at load and report encoding errors |
| A remote COG or GeoParquet object cannot be reached | Source load or tile generation fails | Use remote polling and surface the source-specific error |
| A STAC search has no usable asset, exceeds its item limit, or links outside the API origin | The source may fail at load or a tile may miss some or all matching files | Check the first item during load, limit each search, and reject foreign next links |
| A tile index has no valid feature layer, footprint, location, or file | The indexed COG source does not resolve | Require an EPSG:4326 feature layer with usable envelopes and resolvable locations |
| A DuckDB table has no usable geometry or tile result | The affected table source is omitted or returns empty tiles | Introspect source metadata before publication and isolate warnings per source |
| DuckDB auto-publish finds only explicitly configured tables or macros | No duplicate auto-published source is created | Merge explicit sources by schema, object name, and geometry column before planning discovery |
| A source is cached outside its valid zoom range | Requests can reuse a response where the source does not promise stable coverage | Apply cache_zoom to every source in the request before using the shared cache |
Key files
| File | Why to read it |
|---|---|
| martin-core/…/source.rs | Defines the common source contract, metadata, cache range, tile reads, and reload hook. |
| martin/…/content.rs | Shows the shared cache lookup, source read, format processing, ETag, and HTTP response flow. |
| martin-core/…/source.rs | Shows how a local MBTiles archive loads metadata and reads stored tiles. |
| martin-core/…/source.rs | Shows local and object-store PMTiles loading, directory lookup, and modification handling. |
| martin-core/…/source.rs | Shows the in-memory feature preparation and vector tile generation model. |
| martin-core/…/source.rs | Shows the per-request SQL tile query contract for DuckDB and GeoParquet. |
| martin/…/plan.rs | Shows how DuckDB catalog entries become auto-published table and macro sources without duplicating explicit sources. |
| martin-core/…/source.rs | Shows direct COG file and object-store reads, format handling, and common raster metadata. |
| martin-core/…/stac.rs | Shows STAC collection checks, per-tile searches, asset ordering, search caching, and remote asset selection. |
| martin-core/…/tile_index.rs | Shows GeoPackage footprint loading and tile-to-file selection for indexed COG sources. |
| martin-core/…/source.rs | Shows TileJSON discovery, upstream fetch status rules, format detection, and ETag handling. |
| martin/…/cog.rs | Shows how direct COGs, mosaics, STAC sources, and tile indexes are configured and resolved. |
| martin/…/pmtiles.rs | Shows local event and remote polling behavior for PMTiles discovery. |
| martin/…/cog.rs | Shows local, configured remote, and remote-prefix COG refresh paths. |