Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-polyline-decodeOpenRuns on Liberas

Encoded polyline decode

The points of polyline, in Google's Encoded Polyline Algorithm Format with precision decimal digits (1 to 10, else an error), as {lat, lon}. Every character c must be in 63 to 126 ('?' to '~'); c − 63 is a chunk, and a value's chunks run least significant first while a chunk has bit 0x20 set: z = Σ (chunk AND 0x1f) × 32^k. A value of more than 11 chunks, or a string ending inside a value, is an error. d = z ÷ 2 when z is even and −(z + 1) ÷ 2 when z is odd; values alternate latitude, longitude, and each d is added to its coordinate's running sum, starting at 0; a sum whose magnitude exceeds 2^53 is an error, and so is an odd number of values (a latitude without a longitude). Each coordinate is sum ÷ 10^precision: one correctly rounded binary64 division by the exact power of ten (not a multiplication by 10^−precision), so results match bit for bit. Decoded values are not range-checked. polyline is at most 1,048,576 bytes, else an error; the empty string gives no points.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init geo-polyline-decode --lang rust
cd geo-polyline-decode && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)21 lines›
geo-polyline-decode.wit
package root:component;

world root {
  import liberas:geo/types@0.1.0;

  export liberas:geo/polyline-decode@0.1.0;
}
package liberas:geo@0.1.0 {
  interface types {
    record point {
      lat: f64,
      lon: f64,
    }
  }
  interface polyline-decode {
    use types.{point};

    decode: func(polyline: string, precision: u8) -> result<list<point>, string>;
  }
}
Test vectors17›
geo-polyline-decode.cases.json
[
  {
    "args": {
      "polyline": "_p~iF~ps|U_ulLnnqC_mqNvxq`@",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "_p~iF~ps|U_ulLnnqC_mqNvxq`@",
      "precision": 6
    }
  },
  {
    "args": {
      "polyline": "",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "??",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "????????",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "_cidP~fsia@~fsia@~fsia@",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "AA",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "_p~iF~ps|U",
      "precision": 10
    }
  },
  {
    "args": {
      "polyline": "_p~iF~ps|U",
      "precision": 1
    }
  },
  {
    "args": {
      "polyline": "_p~iF",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "_p~i",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "_p~iF ps|U",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "~~~~~~~~~~~?",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "~~~~~~~~~~^?",
      "precision": 5
    }
  },
  {
    "args": {
      "polyline": "??",
      "precision": 0
    }
  },
  {
    "args": {
      "polyline": "??",
      "precision": 11
    }
  },
  {
    "args": {
      "polyline": "한?",
      "precision": 5
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 17 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 350,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.