182 lines
7.8 KiB
Plaintext
182 lines
7.8 KiB
Plaintext
= Offline routing — design
|
||
:toc:
|
||
:toclevels: 1
|
||
|
||
Status: *implemented* (the container and builds; the route panel, vehicle profile and saving a route; elevation profile and reach).
|
||
|
||
== Goal
|
||
|
||
Car/camper routing that works with no internet, from the same OSM
|
||
extracts the offline map is built from, with the vehicle's dimensions
|
||
(height, width, length, weight) respected, so the route doesn't go
|
||
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"]
|
||
|===
|
||
| Valhalla
|
||
| `truck` costing takes height/width/length/weight/axle load per
|
||
*request*, so one graph serves any vehicle. Has `/isochrone`,
|
||
`/height` and `/optimized_route` built in. Tiles are a memory-mapped
|
||
tar, so RAM stays modest even for many countries. Chosen.
|
||
|
||
| GraphHopper
|
||
| Java is already in the image (Planetiler), and custom models can do
|
||
dimensions too, but the import for ~16 GB of PBF wants a heap close
|
||
to the server's 30 GB RAM.
|
||
|
||
| OSRM
|
||
| Fastest queries, but restrictions are baked in at build time: one
|
||
graph per vehicle profile, no dimension parameters per request.
|
||
|===
|
||
|
||
== Data
|
||
|
||
The PBF files are already there: `data/geofabrik_cache/<CC>.osm.pbf`,
|
||
one per country selected for the offline map (`data/offline_regions.json`,
|
||
currently 14 countries, ~16 GB). Routing uses exactly that set, so
|
||
there's no second region picker. The files are hard-linked into
|
||
`data/routing/pbf/` (same file system, no copies, and Geofabrik refreshes
|
||
don't change a build that's in progress).
|
||
|
||
Measured on the server: 16 cores, 30 GB RAM, 780 GB free. Rough
|
||
expectation for this set: tiles 15–25 GB, a build of a few hours. The
|
||
build runs at a low priority and the old tiles keep serving while it
|
||
runs.
|
||
|
||
== Containers
|
||
|
||
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.
|
||
|
||
The two sides talk through files in `data/routing/`:
|
||
|
||
[cols="1,1,2"]
|
||
|===
|
||
| File | Written by | Meaning
|
||
|
||
| `build-request.json`
|
||
| maptool
|
||
| `{"regions": [...], "pbf": "routing/merged.osm.pbf"}`: build these countries. `pbf` is set for more than one country.
|
||
|
||
| `merged.osm.pbf`
|
||
| maptool
|
||
| The countries' extracts merged by osmium. Valhalla fails on several overlapping extracts ("Failed tile ... _M_range_check", valhalla#3925), and its image has no osmium. Removed after a successful build.
|
||
|
||
| `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
|
||
|
||
* "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 the downloads only, then runs alongside Planetiler (`Routing.extracts_ready`).
|
||
The row says whether the service answers.
|
||
* `POST /api/route` (`{"locations": [[lon, lat], ...]}`) proxies to
|
||
Valhalla `/route` and returns the route as a GeoJSON line with
|
||
distance, time and turn instructions. It's open to everyone, like the
|
||
map; only saving needs a login. The costing depends on the weight:
|
||
up to 3.5 t it's `auto` with the vehicle's height and width, because a
|
||
camper shouldn't be kept off roads closed to lorries (`hgv=no`). Above
|
||
3.5 t it's `truck` with every dimension.
|
||
* Vehicle profile: height, width, length, weight in `data/vehicle.json`
|
||
(`GET`/`PUT /api/vehicle`), one for now, edited in the route panel.
|
||
The default is a camper: 3.0 m, 2.3 m, 7 m, 3.5 t.
|
||
* Route panel (`public/js/route.js`): while it's open, a map click adds a
|
||
point; markers drag; Reverse, Clear. The route is drawn with distance,
|
||
time and directions. "Save to library" stores it as a `route` item in
|
||
the "Routes" folder through the notes endpoint, with its points (`via`)
|
||
and vehicle in `properties`. That way it goes to OsmAnd/Locus through
|
||
the same export as everything else.
|
||
* Points also come from "+" on a search result, "Add to route" in a
|
||
library point's popup, or "+ My position" (the GNSS fix, else the
|
||
browser's position). These go through events in `panels.js`, the same
|
||
way the library refresh does. ↑/↓ reorder points.
|
||
* "Edit route" in a saved route's menu in the tree loads its points
|
||
back. Save then updates that item instead of adding a copy. A route
|
||
imported from GPX has no `via`, so it can't be edited this way.
|
||
|
||
== Phases
|
||
|
||
. Container + build script + "Build routing" + status. Done when
|
||
`curl valhalla:8002/route` answers on the server.
|
||
. `/api/route`, the vehicle profile, the route panel, save to library.
|
||
. Elevation profile of the route, then isochrones. Done:
|
||
* The browser samples the route every ≥50 m (at most 400 points) from
|
||
the same terrain tiles the hillshade uses (`dem.js`; from
|
||
`terrain.pmtiles` when it's there, else online), instead of
|
||
Valhalla's own SRTM download. The summary adds climb, descent and the
|
||
steepest grade sustained over 500 m. "Elevation profile" opens the
|
||
usual profile panel, and a saved route keeps the profile, so the
|
||
library shows it like a track's.
|
||
* `POST /api/isochrone` (`{"center": [lon, lat], "minutes": [...]}`,
|
||
1–4 times up to 240 min) returns Valhalla's polygons for the stored
|
||
vehicle. "Reach in" in the route panel draws them from the start
|
||
point, or from the map center when there are no points yet.
|
||
|
||
== Open questions
|
||
|
||
* 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.
|