Skip to content

Valhalla Polyline Decoder

Decodes routing responses from the Valhalla (OpenStreetMap FOSSGIS) routing engine. Valhalla encodes polylines at 6-decimal-place precision (1e6), which is 10× finer than Google's standard 5-decimal format.


Where It Is Used

apps/passengers-app/src/app/core/services/osrm.service.ts

Valhalla is called for pedestrian walking legs (origin → boarding stop, alighting stop → destination). Its higher precision matters here because walking paths follow narrow sidewalks and pedestrian crossings where 1.1 m resolution would introduce visible snapping artifacts.


Difference from Google Encoded Polyline

Property Google (syncc routes) Valhalla (walking legs)
Precision factor 1e5 1e6
Accuracy ~1.1 m ~0.11 m
Bit logic identical identical

Only the precision constant changes. The bit-manipulation loop is the same as Google Encoded Polyline.


Implementation

function decodeValhalla(encoded: string): [number, number][] {
  const PRECISION = 1e6;   // ← only difference from Google decoder
  const coords: [number, number][] = [];
  let index = 0, lat = 0, lng = 0;

  while (index < encoded.length) {
    for (let i = 0; i < 2; i++) {
      let b, shift = 0, result = 0;
      do {
        b = encoded.charCodeAt(index++) - 63;
        result |= (b & 0x1F) << shift;
        shift += 5;
      } while (b >= 0x20);
      const delta = (result & 1) ? ~(result >> 1) : (result >> 1);
      if (i === 0) lat += delta;
      else lng += delta;
    }
    coords.push([lat / PRECISION, lng / PRECISION]);
  }
  return coords;
}