Route math appendix

This page collects the implementation-level equations used by route-aware search logic. A code-snippet version of this page shows the same logic as it is actually implemented.

Polyline simplification for route=<polyline>;w=<width>

When the route polyline is too dense, the app simplifies it before sending it to Search. It uses the Ramer-Douglas-Peucker algorithm with a binary search on tolerance \(\varepsilon\) to keep at most \(M\) points:

\[\begin{split}\varepsilon_{n+1}= \begin{cases} \frac{\varepsilon_n+\varepsilon_{\text{high}}}{2}, & \text{if } |S(\varepsilon_n)| > M\\ \frac{\varepsilon_{\text{low}}+\varepsilon_n}{2}, & \text{if } |S(\varepsilon_n)| \le M \end{cases}\end{split}\]

where \(S(\varepsilon)\) is the simplified polyline and \(|S(\varepsilon)|\) its point count.

In plain terms, we keep adjusting a “detail” threshold until the route has at most the allowed number of points. This keeps the route shape meaningful while making the payload small enough for Search.

Corridor geometry from route width

For corridor mode, the route centerline is buffered by the requested width in metres in a local tangent-plane projection centred on the route centroid, then transformed back to WGS84 for map/search use.

In plain terms, we temporarily switch to a metre-based projection so a “100 m width” really means 100 metres on the ground, build the corridor there, then convert it back to latitude/longitude.

min from pos future position

Given a number of minutes entered by the user, the app converts it to seconds and adds it to the elapsed trip time at the anchor (pos on route), then maps that new elapsed time back to a location on the route.

The elapsed time at the anchor is the sum of the durations of all route spans before the anchor’s span, plus the fraction of the anchor’s own span already travelled — measured by waypoint index within that span.

To turn the target elapsed time back into a position, the app finds the span that contains it, converts the leftover time into a proportional distance along that span, and walks the span’s waypoints accumulating great-circle (haversine) segment lengths until that distance is reached. The final position is interpolated linearly inside the segment that straddles it, and clamped to the last waypoint when the target lies beyond the end of the route.

Detour reranking

Each result gets an excursion distance: the distance at → result plus result → stop, minus the direct at → stop distance. When a maximum excursion is configured, results above that threshold are dropped.

The remaining results are sorted by ascending travel time — time to reach the result when all_along is off, and time to reach it plus the onward time to the stop when all_along is on.

Durations and excursions shown in result titles are integer-truncated to whole minutes and whole kilometres for readability, and the ranking key itself is expressed in minutes.

In plain terms, each place gets a detour cost compared with driving directly to the destination. We can drop places with too much detour, then rank the rest by the fastest relevant travel time (to the place, or to-place-plus-onward).

References