fix: wait for a precise fix on start and route GPX from the rider

- Start no longer fails when the first fix is coarser than 25 m: both
  searched and imported routes wait up to 60 s for guidance accuracy.
- Starting an imported route more than 150 m from its first point
  prepends a road leg from the current position; traces stay traces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-26 16:43:41 +01:00
1 parent b7bf2c2b43
commit 646a4a2a2a
7 files changed
+128 -9

No files matched your search

@@ -275,6 +275,10 @@ private class RouteLine(private val route: RouteResult) {
private fun validPoint(point: GeoPoint) = point.latitude.isFinite() && point.longitude.isFinite() &&
point.latitude in -90.0..90.0 && point.longitude in -180.0..180.0
private fun wrappedLongitude(value: Double): Double = ((value + 180) % 360 + 360) % 360 - 180
/** Farther than this from an imported route's first point, a road leg to it is prepended on start. */
const val IMPORTED_APPROACH_MIN_METERS = 150.0
fun needsApproach(position: GeoPoint, route: RouteResult): Boolean =
route.geometry.isNotEmpty() && earthDistance(position, route.geometry.first()) > IMPORTED_APPROACH_MIN_METERS
private fun earthDistance(a: GeoPoint, b: GeoPoint): Double {
val lat1 = Math.toRadians(a.latitude); val lat2 = Math.toRadians(b.latitude)
val dlat = lat2 - lat1; val dlon = Math.toRadians(wrappedLongitude(b.longitude - a.longitude))
@@ -192,6 +192,15 @@ fun mergeRouteLegs(parts: List<RouteResult>): RouteResult {
guidanceLanguage = parts.first().guidanceLanguage)
}
/**
* Prefix [route] with a road leg from the rider's position to its first point. A trace stays a
* trace: no turn instructions or ETA, the leg only extends the line to follow.
*/
fun prependApproach(approach: RouteResult, route: RouteResult): RouteResult {
val merged = mergeRouteLegs(listOf(approach, route))
return if (route.imported && route.maneuvers.isEmpty()) merged.copy(maneuvers = emptyList(), durationSeconds = 0) else merged
}
private fun validatePoint(point: GeoPoint) {
if (!point.latitude.isFinite() || !point.longitude.isFinite() ||
point.latitude !in -90.0..90.0 || point.longitude !in -180.0..180.0) invalid("Invalid routing coordinate")
@@ -189,4 +189,23 @@ class ValhallaRouteTest {
// Independently snapped endpoints that do not coincide keep both points.
assertEquals(6, mergeRouteLegs(listOf(part(a, b, c, true), part(GeoPoint(43.0201, -5.0), GeoPoint(43.025, -5.0), d, false))).geometry.size)
}
@Test fun `imported routes get a road leg from a distant rider position`() {
val start = GeoPoint(43.0, -5.0)
val trace = RouteResult(listOf(start, GeoPoint(43.01, -5.0)), emptyList(), 1_100, 0, GeoPoint(43.01, -5.0), imported = true)
assertFalse(needsApproach(GeoPoint(43.0005, -5.0), trace)) // About 55 m away.
assertTrue(needsApproach(GeoPoint(42.99, -5.0), trace))
val approach = RouteResult(listOf(GeoPoint(42.99, -5.0), start), listOf(
Maneuver("start", 1_100, type = 1, beginShapeIndex = 0, endShapeIndex = 1),
Maneuver("arrive", 0, type = 4, beginShapeIndex = 1, endShapeIndex = 1),
), 1_100, 90, start)
val merged = prependApproach(approach, trace)
assertEquals(3, merged.geometry.size)
assertTrue(merged.maneuvers.isEmpty()) // Still a trace to follow.
assertEquals(2_200, merged.distanceMeters)
assertTrue(merged.imported)
assertEquals(trace.destination, merged.destination)
assertEquals(0, merged.durationSeconds) // A trace never shows an ETA.
}
}