baldav/frontend/js/router.js
4aiman a52e998fb6 BRouter: analysis + embed the offline routing engine (Stage 1)
Analysis in docs/brouter-findings.md: how BRouter knows roads (.rd5
segments), routes and reaches low elevation gain (profile uphill/downhill
cost + kinematics), the three call surfaces, and an evaluation of two ideas
— fill GPS gaps along roads, and offline waypoint route planning. License is
MIT; the engine has no desktop-only deps, so it embeds on Android ART.

Stage 1 (embed + bridge, compiles into the apk; on-device route not yet run):
- vendored brouter-1.6.3-all.jar into the Android module (dexes cleanly) and
  bundled the trekking/shortest profiles + lookups.dat as assets, copied to
  files/brouter/profiles2 on launch
- MainActivity: a brouter_command.txt handler on the existing FileObserver
  runs btools.router.RoutingEngine off the UI thread (recipe taken via javap
  from ServerHandler) and returns the track as GPX
- Rust brouter_route command: same file bridge as GPS (request + command,
  poll for the result), registered in the invoke handler
- frontend/js/router.js: BRouter.route(points, profile) -> GPX (parseRouteGPX)
- LICENSE-brouter (MIT notice) kept alongside the vendored engine

Segments (.rd5, ~13-23 MB per 5x5 tile) are delivered later (Stage 2, wired
download inside BalDav). IMPLEMENTATION_PLAN.md tracks the 5 stages.
2026-09-09 00:56:13 +03:00

20 lines
962 B
JavaScript

// Offline routing via the embedded BRouter engine.
// JS → Rust command `brouter_route` → Kotlin (btools.router) → GPX back.
// BRouter.route(points, profile):
// points — array of [lon, lat] pairs (BRouter order; NOT [lat, lon])
// profile — "trekking" (planning) | "shortest" (gap fill); default "trekking"
// Returns a GPX string (parse with parseRouteGPX) or throws with the engine's error.
// Needs the region .rd5 segments present on device (files/brouter/segments4).
window.BRouter = {
async route(points, profile) {
let lonlats = JSON.stringify((points || []).map(p => [p[0], p[1]]));
let raw = await window.__TAURI__.core.invoke("brouter_route", {
lonlats,
profile: profile || "trekking",
});
let res;
try { res = JSON.parse(raw); } catch (e) { throw new Error("brouter: bad result: " + raw); }
if (!res.ok) throw new Error("brouter: " + (res.error || "unknown error"));
return res.gpx;
},
};