Overview
Routing without a routing server. A Python build turns OpenStreetMap into one static file of graph tiles. The browser downloads only the tiles a route touches, over HTTP byte ranges, and runs the search itself in a Web Worker.
nodes, ways, relations
graph nodes → pieces → tiles
one gzip schema-6 tile per z12 tile
HTTP range requests
fetches tiles on demand
Rule of the archive: it is a faithful copy of OpenStreetMap. The builder decides only structure (where ways are split, which tiles hold what). Who may use a road, in which direction and at what cost is decided by the router at route time, from the raw tags. Changing a profile never needs a rebuild.
Written docs: docs/how-the-builder-works.md ·
docs/how-the-engine-works.md · contract: docs/archive-format.md
1 · The builder: from OSM ways to graph tiles
A toy extract around a z12 tile corner in Luxembourg City, run through the builder's steps. The tiles, node keys
and lengths below are computed in your browser by a JavaScript re-telling of core/osm-to-pmtiles/graph.py.
2 · The PMTiles container
All tiles live in one file. A tiny index at the front tells the reader which bytes hold which tile, so a browser can
fetch any tile with an HTTP Range request and no tile server.
Tile IDs follow a Hilbert curve
Every z/x/y becomes one number: all tiles of lower zooms first, then the position along a Hilbert
curve. Tiles are stored in that order, so tiles that are neighbours on the map are mostly neighbours in the
file. Hover a cell (zoom 3 shown).
Hover a tile
The toy map's tile 12/2117/1396 has ID .
Reading one tile:
- Fetch the first 16 KiB: header + root directory (cached for the worker's lifetime).
- Binary-search the directory for the tile ID; a leaf pointer costs one more request (also cached).
- Fetch exactly the tile's byte range, gunzip, then read the columns.
3 · Encoding a tile: schema 6
The builder first writes each tile's content as plain JSON: its ways with all their tags, its tagged nodes, its pieces
and its turn restrictions (docs/archive-format.md). core/osm-to-pmtiles/reencode.py then
stores exactly that content as a schema-6 tile: binary columns of small numbers, which gzip compresses well. The bytes
are specified at the top of core/osm-to-pmtiles/tile6.py.
In the content and in the router's API, a node key is identity, owner tile and position in one string
The tile's content (what the builder writes first)
Schema 6, section by section (hex)
What each trick saves
| Node table | Each node once; pieces point to a row instead of repeating a 45-byte key. |
|---|---|
| Integers | 6.1491000 → 61491000 (1e-7°). Lengths are a correction in mm to an estimate from the geometry. |
| Deltas | Each node is written as the step from the one it connects to; interior points as the error of a prediction. |
away | Nodes owned by another tile store the offset to it and their number there. |
| Endpoints | Piece geometry keeps interior points only; ends come from the node table. |
| Strings, tag sets | Tag keys and values once per tile, and each distinct tag list once. |
| Varints, split bits | Small numbers take one byte; low and high bits are stored apart so gzip finds the repeats. |
Germany z12: 2,128 MB as gzip JSON, 617 MB as schema 6. Every tile round-trips exactly.
The node table, decoded
The owned nodes first, by OSM id, then the nodes of other tiles with their owner.
One number per node, the same from every tile
A tile lists the nodes it owns first, sorted by OSM id. A node's row there is its number, and any other tile
that mentions the node stores that number next to the offset to the owner. The router can then name every node by
(owner tile code) × 2²² + number: one number that says which tile to load and which row to read, with no
key to build, hash or parse.
To see a real schema-6 tile, with its nine sections, bytes and every road, open Tiles.
4 · Decoding: bytes to a graph one mode can search
typed arrays
combination
edgeStart[i]…edgeStart[i+1]
per mode
- directed edge the mode may use
- in the archive, unused by this mode
- barrier that stops this mode
- turn restriction for this mode
5 · The engine: one route
Watch the search
A synthetic road grid with a lake, cut into tiles. The grid is encoded as schema-6 tiles near Luxembourg and decoded
for cars, and the animation replays what the engine's own search() (core/src/search/search.js)
did on them, step by step. Click the map to move the start, then the goal.
- state expanded (darker = later)
- tile loaded: the search needed it
- tile wanted: prefetch hint, not needed yet
- shortest path
A* adds the straight-line distance to the goal to each state's priority. Stored lengths are never shorter than that distance, so A* still finds the optimal path, while expanding far fewer states and touching fewer tiles: fewer downloads.
Why states are arrivals, not nodes
"No left turn from Avenue de la Gare onto Rue Verte" depends on how you reached node 3. State ①, (node 3, arrived on the avenue), may not go north; state ②, (node 3, arrived on Rue du Sud), may. If the search kept one state per node, whichever arrival came first would decide for both. It keeps one best state per (node, incoming edge) instead.
Caches between the network and the search
graph.node(key): synchronous when cached, a Promise only on a real load