garage44.eu/jeroen/cartopolis

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

.. /docs/adr/037-vector-tile-routing.md

raw

ADR-037: Routing on the Vector Tiles


Metadata:


Context

Problem: The map now streams Shortbread-schema MVT (ADR-035), which means the client already holds the road network as geometry with attributes rather than as pixels — streets carries kind, oneway, oneway_reverse, bicycle, surface, and street_labels carries name. A map you can search and fly around but not ask "how do I get there?" is missing the one thing every map application is expected to do, and the data for it is already being fetched and cached on disk.

Requirements:

Decision

Route client-side, on the streamed vector tiles, with an A* over a graph stitched from the streets layer, and a mobile-navigator-style planner panel driving it.

Approach:

Five decisions carry the design:

1. Node identity is exact, not fuzzy. Every tile at a zoom shares one integer lattice (global = tile_index · extent + local). A way crossing a tile border is clipped into both tiles with a buffer, so the same OSM vertex quantises to the same global integer in both. Stitching tiles therefore needs no distance tolerance and no way ids — which the tiles do not carry.

2. The graph must be noded. Shared vertices are necessary but not sufficient: Shortbread merges ways by attribute and generalises each merged feature independently, so where a side street tees into a main road the tee is an interior vertex of the main road's line and simplification may drop it. Measured on the fixture that is 14% of all nodes left lying on a segment while being a vertex of neither, and it shatters the network — the largest connected car component covered 58% of usable nodes. RoadGraph::finish splits every segment at the nodes lying on it (within ~0.55 m), taking that to 92% (walking 60% → 96%). This failure is silent: without it, ordinary cross-town routes simply return NoPath.

3. Endpoints snap to the routable core, not the nearest node. The nearest node to an address may sit on a genuine one-way street whose forward-reachable set, for that mode, is a tiny dead-end pocket — a car starting there could arrive but never leave, and the route returned NoPath even though a road ran past the door (the same pair routed fine for bikes, which ignore one-ways). In a road network the drivable through-network is one giant strongly-connected component and the stubs, service loops and one-way pockets are tiny ones, so route computes the largest SCC of the mode-filtered graph (routable_core, iterative Kosaraju) and snaps both endpoints into it. Both in one SCC ⇒ a path exists, so any surviving NoPath is a genuinely disconnected corridor.

4. The tile corridor is a capsule, capped, and refused by distance. Tiles within a half-width of the straight line, where the half-width grows with √direct (not linearly — a linear term made a long route both long and wide, and a cross-province query asked for ~700 tiles). A route whose corridor exceeds MAX_ROUTE_TILES is refused with its straight-line distance (TooFar { km }, against an advertised MAX_ROUTE_KM reach), because the kilometres are the thing the user chose — a raw tile count is not actionable. This is the design range limit, not a bug: routing the long haul is the coarse-zoom router's job (see Consequences). The graph is cached against its tile list, so switching travel mode re-runs only the A*.

5. Modes are access filters over one graph. Cars are barred from footways, cycleways and bus lanes and bound by one-ways; bikes are barred from motorways and trunk roads; walkers use everything but those and rails. One-ways bind cars only — the tiles carry no oneway:bicycle, and honouring them for bikes invents detours no rider makes in a dense city centre.

The turn list only emits an instruction where the network branches (a node with ≥ 3 usable neighbours) or the street name changes, and measures the angle over a 25 m window either side of the junction. Without both, a curving street narrates as "bear right, bear left, bear right…" for 300 m — the graph has a node per vertex, and curvature is not a decision.

Rationale

Primary Reasoning:

  1. The data is already here. Routing on the basemap costs no new fetches for anywhere the user has been, no new service to run, and it inherits the offline behaviour of the tile cache. This is P-03 (locality) applied to a capability, not just to bytes.
  2. It is the payoff for ADR-035. Going vector was justified by size and by lit geometry; being able to route is the third dividend, and it is unavailable to a raster basemap at any price.
  3. A router is latency-sensitive and the network is not. A local A* over a warm cache answers in single-digit milliseconds (measured: 1–4 ms for a 1.5 km city route once the graph is built), which is what lets the mode tabs re-solve on click.
  4. Bounded by construction. One worker thread, a capped corridor, a cached graph, and exactly four mesh entities per route — none of the streaming hazards this codebase has been bitten by.

Alternatives Considered:

AlternativeProsConsRejected Because
Self-host OSRM / Valhalla and call itBest-in-class routing quality; turn restrictions, traffic profiles, alternativesA second service to build, feed and operate; a second copy of OSM data; no offline; a network round-trip per re-solveThe whole point of ADR-035 was to stop depending on someone else's derived-data service. Re-adding one for routing, over data we already ship, inverts that.
Public routing API (OSRM demo, GraphHopper)Zero workUsage policies at least as strict as OSM's tile policy; a browser cannot identify itself (ADR-033); no offlineSame objection ADR-035 raised against the OSM tile CDN, for the same reasons.
Route on the Overpass detail patches (ADR-022)Real OSM ways with ids and full tags — no noding neededOnly covers the small patch around the camera; an Overpass call per route; nothing outside the patch existsCannot answer a route that leaves the current patch, which is most routes.
Ship a prebuilt routing graph alongside the tilesCompact; fast; correct topologyA second artefact to build and version; another 60 GiB-class pipeline; drifts from the tilesNot worth a second pipeline while the tiles already imply the graph — revisit if the noding proves insufficient in other regions.
A bounding box instead of a corridorTrivialA diagonal route fetches the whole square it spans3–4× the tiles for the routes where tiles are most expensive.

Consequences

Positive:

Negative:

Related