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.

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:

    1. Fetch the first 16 KiB: header + root directory (cached for the worker's lifetime).
    2. Binary-search the directory for the tile ID; a leaf pointer costs one more request (also cached).
    3. 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 tableEach node once; pieces point to a row instead of repeating a 45-byte key.
    Integers6.1491000 → 61491000 (1e-7°). Lengths are a correction in mm to an estimate from the geometry.
    DeltasEach node is written as the step from the one it connects to; interior points as the error of a prediction.
    awayNodes owned by another tile store the offset to it and their number there.
    EndpointsPiece geometry keeps interior points only; ends come from the node table.
    Strings, tag setsTag keys and values once per tile, and each distinct tag list once.
    Varints, split bitsSmall 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

    • 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

    1 · RequestValidate endpoints, profile, limits (mobile defaults). Straight-line distance checked before any download.
    2 · ProfileMode rules pick the tile view (cache shared by profiles of one mode); costs = surface × tag factors per metre.
    3 · Snap A and BIn parallel: read tiles within the radius, project onto road segments, skip cut-off islands (< 100 reachable nodes).
    4 · SearchDijkstra or A* over states = (node, incoming edge), so turn restrictions work. Tiles load on first touch.
    5 · PrefetchEdges leaving a tile hint the scheduler, which keeps 4 loads in flight, nearest-to-goal first, plus 3 tiles ahead.
    6 · AnswerWalk parents back: node path, edge ids, cost, metres, diagnostics.

    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

    Searchgraph.node(key): synchronous when cached, a Promise only on a real load
    Decoded tilesLRU per mode, budget in records (nodes + edges), ~1M by default
    Range cachecompressed tile bytes, 256 MB: an evicted tile is never downloaded twice
    Directoriesheader, root and leaf directories, for the worker's lifetime
    Meter → networkcounts bytes and requests, enforces route limits
    Toy data is invented for illustration. Map data in the real archives © OpenStreetMap contributors (ODbL).