garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/adr/035-self-hosted-map-tiles.md

raw

ADR-035: Self-Hosted Vector Map Tiles


Metadata:


Context

Problem: The client renders the map from raster tiles fetched straight from tile.openstreetmap.org (the osm_tile_url_template default in cartopolis_simulator/src/config.rs:42), consumed by tile_loader, map_stream and the globe. Three things make that untenable for the web target (ADR-033) and increasingly untenable generally:

  1. OSM's tile CDN is not a base layer for a product. Their tile usage policy expects heavy or commercial consumers to run their own infrastructure, and explicitly discourages proxying — so ADR-034's normal answer (an L2 passthrough) is off the table here.
  2. A browser cannot identify itself. User-Agent is a forbidden header in fetch(), so TILE_USER_AGENT — our compliance with the policy's identification requirement — does not apply on the web build.
  3. Tiles are by far our largest byte volume, across both the map tier and the globe tier (ADR-020, ADR-025). This is where caching and locality matter most (P-03).

A fourth, smaller issue: the tile template is currently handed to the client by the sim in ServerHelloAck. ADR-031's anonymous Viewer has no sim, so it needs the template from configuration instead (there is a Default in resources.rs:581, but it should be explicit).

We already self-host the rest of the geo stack — Nominatim, Overpass, 3DBAG (ADR-027) — behind the garage44 Caddy, so this follows an established pattern rather than starting one.

Decision

Serve our own OSM vector tiles (Mapbox Vector Tile / MVT) from our origin, in the Shortbread schema the client already tessellates and lights. The client decodes MVT and renders the map as geometry; styling happens client-side.

Implementation (2026-07-24) — how the server was actually built. The mechanism below (planetiler → PMTiles → a cartopolis_relay handler) was the plan; the shipped server takes a simpler path that reaches the same end. The client's whole vector path — vector_tiles.rs, routing.rs, the styling — is written against the Shortbread schema, which is what tiles.versatiles.org serves and what this ADR's default already pointed at. Rather than run planetiler (whose default profile is not Shortbread, and whose planet build needs far more RAM than the 22 GB VPS has), we mirror VersaTiles' prebuilt planet archive and serve it ourselves:

  • Data: https://download.versatiles.org/osm.versatiles — one 62 GB .versatiles container, z0–14, Shortbread. Downloaded once to /srv/versatiles/osm.versatiles on the garage44.eu VPS (on /, not /data — the 62 GB doesn't fit the smaller volume). Refresh = re-download.
  • Serving: the Rust versatiles server (ghcr.io/versatiles-org/versatiles) as a tiles service in the existing geo Docker Compose stack, exposing /tiles/osm/{z}/{x}/{y}. It already emits Access-Control-Allow-Origin: * and a 28-day Cache-Control, so no relay logic is needed — this replaces the "small pmtiles handler in cartopolis_relay" design.
  • Origin: Caddy fronts it at tiles.cartopolis.org (its own subdomain, the overpass / nominatim / 3dbag pattern), not the same-origin /tiles/* path ADR-034 sketched. The single-origin relay of ADR-033/034 (for the wasm build) can still front this upstream later.
  • Client: a pure origin swap. DEFAULT_TILE_URL_TEMPLATE moved from tiles.versatiles.org to tiles.cartopolis.org — same schema, same URL shape, no decode/render change. CARTO_TILE_URL still overrides.

The rest of this ADR (why vector, coverage, client work, downstream consolidation) stands as written; only the generation-and-serving mechanism changed. Planetiler → PMTiles remains the path to a custom cartopolis-styled or non-Shortbread tileset if we ever outgrow the mirror.

The original plan — kept for the record and as the fallback for a custom tileset — was to serve at /tiles/{z}/{x}/{y}.mvt, generated offline with planetiler and stored as PMTiles:

Why vector rather than raster

Raster was the conservative choice because it preserved the existing PNG → texture path. Vector wins on the axes that matter most for this project:

Coverage

TierZoomsExtent
Globe / space (ADR-025)z0–~z6whole planet
Map / cityto ~z14, then client overzoomregional extracts first (Netherlands, matching 3DBAG), expanding per region

Rough order of magnitude: a country extract is single-digit GB; the full planet to z14 is tens of GB — feasible later, but regional extracts plus global low zoom is the sensible start. (Estimates to be confirmed by the implementation workstream.)

Serving

A PMTiles archive (single file, HTTP range reads) rather than millions of small files. A small Rust handler (pmtiles crate) reads z/x/y out of the archive; this lives in cartopolis_relay (ADR-034) as a first-class route rather than an UPSTREAMS adapter — it has no upstream, it reads local data. Caddy fronts it at /tiles/* with long-lived Cache-Control; the native client keeps an on-disk cache.

Client work (the substantial part)

This is a real client change, not a URL swap, and is being built in a separate workstream:

The globe tier may keep a rasterised or simplified representation at very low zoom, where per-feature geometry buys nothing — decided during implementation.

Client configuration

The tile URL template becomes configuration (ADR-014 layering) rather than something only the sim supplies, so the anonymous Viewer (ADR-031) has a base map with no session. ServerHelloAck may still override it, letting a deployment point clients at its own tile source.

The default is now the vector source: cartopolis_core::world::DEFAULT_TILE_URL_TEMPLATE (https://tiles.versatiles.org/tiles/osm/{z}/{x}/{y}), shared by the sim's advertised default (cartopolis_simulator/src/config.rs) and the client's offline/--shot fallback (tile_loader) so the two cannot drift. This replaces the original raster tile.openstreetmap.org/{z}/{x}/{y}.png default: with the tessellated-and-lit client path real, leaving raster as the out-of-the-box source meant solo mode silently rendered the old pre-baked tiles (labels, dashed carto, static lighting) instead of the vector map this ADR exists to deliver. A raster template still works — tile_source_kind classifies by the tail after {y}, and CARTO_TILE_URL / CARTO_TILE_FORMAT override per run — it is just no longer the default.

Downstream consolidation (noted, not decided)

MVT tiles carry buildings, roads, landuse and POIs — overlapping what we currently fetch from Overpass for building footprints (ADR-022) and data layers (ADR-023). There is a real opportunity to retire some Overpass traffic later. Not decided here: Overpass gives live, queryable, arbitrarily fresh data, while tiles are a periodic snapshot at fixed zoom, and the data layers depend on tag filtering that MVT schemas flatten away. Revisit once the vector client path is real.

Rationale

Primary Reasoning:

  1. Policy leaves no alternative to self-hosting. OSM discourages proxying and expects heavy users to self-host; the browser cannot send our identifying User-Agent.
  2. Vector suits a lit, 3D, time-varying world. Our basemap needs to respond to the day/night and weather systems; a raster image structurally cannot.
  3. Overzoom collapses the cost. The dominant expense in the raster plan was the high-zoom regional pyramid, which vector tiles do not need.
  4. It follows the existing pattern — one more self-hosted geo service behind the same Caddy (ADR-027 precedent).
  5. Pure-Rust decode keeps the wasm target clean, unlike the C-dependency trap ADR-033 hit.
  6. We control caching and locality for our highest-volume traffic (P-03).

Alternatives Considered:

AlternativeProsConsRejected Because
Keep hitting tile.openstreetmap.orgZero workAgainst their policy at our scale; no UA from a browserNot viable for a public web build
Proxy/cache OSM tiles through our relayLittle infrastructureOSM explicitly discourages proxying; still their bandwidthPolicy, not cost
Self-hosted raster tiles (the earlier plan)No client change; preserves PNG→texture pathStatic appearance under a live day/night cycle; expensive high-zoom pyramid; larger payloads; needs a rasterisation stepVector is a better fit for a lit 3D world and cheaper to store and serve
Third-party keyed provider (MapTiler, Stadia, …)No ops at allPer-tile cost; key custody in a browser; vendor dependencyRecurring cost and dependency for our highest-volume asset
Directory of tile filesDead simpleMillions of inodes; slow sync/backupPMTiles is one file with the same access pattern
Full planet at full depth nowUniformTens of GB and long build before it's neededRegional extracts + global low zoom is the sensible start

Consequences

Positive:

Negative:

Related