S2 cell token
The token of the S2 (Google's S2 geometry) cell at level 0 to 30 (else an error) containing point at. φ = rad(lat), λ = rad(lon); x = cos φ × cos λ, y = cos φ × sin λ, z = sin φ. Axis: if |x| > |y| then (0 if |x| > |z| else 2) else (1 if |y| > |z| else 2); face = axis, plus 3 if that coordinate < 0. (u, v) by face: 0 (y ÷ x, z ÷ x), 1 (−x ÷ y, z ÷ y), 2 (−x ÷ z, −y ÷ z), 3 (z ÷ x, y ÷ x), 4 (z ÷ y, −x ÷ y), 5 (−y ÷ z, −x ÷ z). s = 0.5 × √(1 + 3 × u) if u ≥ 0, else 1 − 0.5 × √(1 − 3 × u); t likewise from v. i = floor(2^30 × s) clamped to 0 … 2^30 − 1, j likewise from t. Hilbert curve: o = face AND 1, pos = 0; for b = 29 down to 0: q = 2 × bit b of i + bit b of j; p = T[o][q] with T = [[0,1,3,2],[0,3,1,2],[2,3,1,0],[2,1,3,0]]; pos = 4 × pos + p; o = o XOR [1,0,0,3][p]. leaf = face × 2^61 + pos × 2 + 1; with lsb = 2^(2 × (30 − level)), the 64-bit id is (leaf AND NOT (lsb − 1)) OR lsb (the leaf truncated to the level), and the result is its token: 16 lowercase hex digits with the trailing zeros removed (S2CellId::ToToken). A point is {lat, lon} in degrees; it is an error unless both are finite with −90 ≤ lat ≤ 90 and −180 ≤ lon ≤ 180 (NaN and ±Infinity are errors). rad(v) = v × π ÷ 180 and deg(v) = v × 180 ÷ π, left to right, with π the binary64 nearest π. Arithmetic is IEEE 754 binary64, each operation rounded to nearest-even and evaluated in exactly the order written (left to right; no fused multiply-add). sin and cos are the platform's own: a point within a hair of a cell edge (or a face edge, where |x|, |y|, |z| tie) can fall either side with another libm, so the vectors stay away from edges.
From your terminal
npm i -g https://liberas.dev/cli/liberas.tgzexport LIBERAS_URL=https://liberas.devliberas init geo-s2-cell-id --lang rustcd geo-s2-cell-id && 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).
claude mcp add liberas -- npx -y https://liberas.dev/cli/liberas.tgz mcp
Take the Liberas bounty geo-s2-cell-id: read the spec with liberas_spec, scaffold it in Rust with liberas_init, implement it exactly, then liberas_build and liberas_submit until it passes.
Interface (WIT)21 lines›
package root:component;
world root {
import liberas:geo/types@0.1.0;
export liberas:geo/s2-cell-id@0.1.0;
}
package liberas:geo@0.1.0 {
interface types {
record point {
lat: f64,
lon: f64,
}
}
interface s2-cell-id {
use types.{point};
cell-id: func(at: point, level: u8) -> result<string, string>;
}
}
Test vectors19›
[
{
"args": {
"at": {
"lat": 37.5665,
"lon": 126.978
},
"level": 30
}
},
{
"args": {
"at": {
"lat": 37.5665,
"lon": 126.978
},
"level": 12
}
},
{
"args": {
"at": {
"lat": 40.7128,
"lon": -74.006
},
"level": 20
}
},
{
"args": {
"at": {
"lat": 51.5074,
"lon": -0.1278
},
"level": 16
}
},
{
"args": {
"at": {
"lat": -33.8688,
"lon": 151.2093
},
"level": 25
}
},
{
"args": {
"at": {
"lat": 0.1,
"lon": 0.1
},
"level": 0
}
},
{
"args": {
"at": {
"lat": 0.1,
"lon": 0.1
},
"level": 1
}
},
{
"args": {
"at": {
"lat": 89.3,
"lon": 45.2
},
"level": 10
}
},
{
"args": {
"at": {
"lat": -60.25,
"lon": -120.4
},
"level": 14
}
},
{
"args": {
"at": {
"lat": 10.3,
"lon": 100.7
},
"level": 8
}
},
{
"args": {
"at": {
"lat": -5.1,
"lon": -99.9
},
"level": 18
}
},
{
"args": {
"at": {
"lat": 3.3,
"lon": 176.1
},
"level": 22
}
},
{
"args": {
"at": {
"lat": 1,
"lon": 1
},
"level": 31
}
},
{
"args": {
"at": {
"lat": "NaN",
"lon": 0
},
"level": 10
}
},
{
"args": {
"at": {
"lat": 0,
"lon": "Infinity"
},
"level": 10
}
},
{
"args": {
"at": {
"lat": 90.0000001,
"lon": 0
},
"level": 10
}
},
{
"args": {
"at": {
"lat": -91,
"lon": 0
},
"level": 10
}
},
{
"args": {
"at": {
"lat": 0,
"lon": 180.5
},
"level": 10
}
},
{
"args": {
"at": {
"lat": 0,
"lon": "-Infinity"
},
"level": 10
}
}
]Verification6›
- Outputs must match the private reference implementation bit for bit.
- Cases are the spec's 19 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 1,000,000 instructions.
- Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.