Offline routing phase 1: valhalla container building from the offline map's extracts, status in the Offline data panel
CI / check (push) Successful in 55s
CI / deploy (push) Successful in 2m48s

This commit is contained in:
randy committed 2026-11-13 21:24:19 +01:00
1 parent 6f5f11cb4d
commit bf912b96e5
13 files changed
+455 -36

No files matched your search

+76 -34
View File
@@ -2,7 +2,7 @@
:toc:
:toclevels: 1
Status: proposal, nothing implemented yet.
Status: *phase 1 implemented* (the container, builds, status in the Offline data panel); phases 2–3 proposed.
== Goal
@@ -12,6 +12,28 @@ extracts the offline map is built from, with the vehicle's dimensions
under a 3 m bridge or over a 3.5 t bridge. Later on the same engine:
an elevation profile of the route and isochrones ("how far in 2 hours").
== How OsmAnd and Locus do it
Neither routes on raw OSM. Someone else builds a compact graph once and
the phone just downloads it:
* *OsmAnd*: every `.obf` map file has a routing section (roads with only
the tags routing needs) built on OsmAnd's servers. Since 4.6 it also
carries precomputed "HH" (highway hierarchy) shortcuts for long
routes. Vehicle height/weight/width are profile parameters checked
against `maxheight`/`maxweight` at query time.
* *Locus*: offline routing goes through BRouter: `.rd5` segment files
(5°×5° tiles, built weekly by brouter.de) and profiles scripted at
query time. Its lookup tables have no `maxheight`/`maxweight`, so it
can't respect vehicle dimensions. It's built for cycling first.
Valhalla is the same idea: the build (hours, once per map refresh)
produces compact tiles with only roads and their restrictions (a
fraction of the OSM extract), and queries are A* over those tiles in
milliseconds. The difference is only that we build the tiles ourselves
from the extracts we already have, instead of downloading someone
else's.
== Engine: Valhalla
[cols="1,3"]
@@ -48,43 +70,64 @@ runs.
== Containers
Valhalla isn't packaged for Debian trixie, so it doesn't go into the
maptool image. Instead there's one more compose service, the same pattern
as `kiwix`:
Valhalla isn't packaged for Debian trixie, so it isn't in the maptool
image but in its own compose service, `valhalla`, from Valhalla's image
`ghcr.io/valhalla/valhalla` (Renovate bumps the tag). It runs
`bin/routing-service.sh`, which maptool copies to
`data/routing/service.sh` at boot, so the script always matches the
deployed app. It mounts the whole `data/`, since a hard link from
`data/geofabrik_cache/` can't cross two bind mounts.
[source,yaml]
----
valhalla:
image: ghcr.io/valhalla/valhalla:<pinned tag> # renovate bumps it
restart: unless-stopped
command: ["/app/bin/routing-service.sh"]
volumes:
- ./data/routing:/routing:z
- ./build/bin/routing-service.sh:/app/bin/routing-service.sh:ro,z
----
The two sides talk through files in `data/routing/`:
It uses the base image (binaries only), not `valhalla-scripted`. The
scripted image only rebuilds when the container restarts, and maptool
can't restart containers. Instead `bin/routing-service.sh` (~40 lines of
shell) runs inside it:
[cols="1,1,2"]
|===
| File | Written by | Meaning
. Serve `valhalla_service` from `tiles.tar`, if one exists.
. Poll for `data/routing/build-request.json`. When it shows up, build
into `staging/` (`valhalla_build_config`, `valhalla_build_admins`,
`valhalla_build_timezones`, `valhalla_build_tiles`,
`valhalla_build_extract`), writing the current step to `status.json`.
. When the build succeeds, swap `staging/tiles.tar` in and restart
`valhalla_service`. When it fails, keep the old tiles and put the log
in `status.json`.
| `build-request.json`
| maptool
| `{"regions": [...]}`: build these countries.
maptool reaches it at `http://valhalla:8002` (`VALHALLA_URL`), server
side only, like `KIWIX_URL`.
| `status.json`
| container
| `state` (running/done/error), `step`, `started_at`, `message`.
| `tiles.tar`, `admins.sqlite`, `valhalla.json`
| container
| The served graph.
| `built.json`
| container
| The request the current tiles were built from.
| `build.log`
| container
| The last build's output.
|===
The script serves `tiles.tar` with `valhalla_service`, polls for a
request every 10 s, and builds into `staging/` while the old tiles keep
serving. It runs `valhalla_build_admins`, then `valhalla_build_tiles`,
then `valhalla_build_extract`, at `nice 10` with half the cores. When
the build succeeds, it swaps the tiles in and restarts the service. A
failure keeps the old tiles and puts the end of the log in
`status.json`. Every redeploy restarts the whole compose project; a
build cut off that way starts over on its own (its request is still in
`staging/`).
No timezone database (`valhalla_build_timezones` downloads one): only
time-dependent routing needs it.
Tested locally with Liechtenstein: the build takes 2 s, a rebuild while
serving swaps the tiles in, and a truck route with dimensions comes back.
== maptool side
* "Build routing" next to the existing offline/terrain builds, login
required. It writes `build-request.json` and links the PBF files.
Status and history appear in the same build panel (`build_history`).
* "Offline routing" row in the Offline data panel, next to the offline
map and terrain (`/api/routing-tiles`, same shape). Apply builds all
three. A routing request made while the offline map is still
downloading extracts waits for it (`Routing.offline_build_finished`).
The row says whether the service answers.
* `POST /api/route` proxies to Valhalla `/route` with `costing: truck`
and the vehicle profile filled in. It's open to everyone, like the
map. Login is required only to save the result.
@@ -107,7 +150,6 @@ side only, like `KIWIX_URL`.
== Open questions
* Pin a Valhalla version: take the latest `ghcr.io/valhalla/valhalla`
release tag at implementation time and add it to Renovate (a docker
image in compose is tracked by default).
* Trailer / several vehicle profiles: one profile until it's needed.
* Build time and size for all 14 countries: measure on the first build
on the server.