Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-h3-lat-lng-to-cellOpenRuns on Liberas

H3 cell of a point

The H3 cell (Uber's hexagonal grid, version 4) at resolution 0 to 15 (else an error) containing point at, as H3's latLngToCell computes it: the point in radians (degrees × (π ÷ 180)) is projected gnomonically onto the nearest face of H3's icosahedron, located on that face's hexagonal grid at the resolution (aperture 7, Class III grids rotated at odd resolutions) and re-expressed from its base cell. A cell is written as its 64-bit index in lowercase hexadecimal, 15 digits (every cell index lies in 2^59 … 2^60 − 1). A cell argument is 1 to 16 hex digits in either case and must be a valid H3 cell index (H3's isValidCell: high bit 0, mode 1, resolution 0 … 15, a valid base cell, digits 0 … 6 up to the resolution and 7 after it, no deleted subsequence of a pentagon), else an error. 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). The reference is the h3o crate 0.11.0, which agrees with the H3 C library 4.x. Its trigonometry (sin, cos, atan2, asin) is not correctly rounded, so a point within about 1e-9 degrees of a cell edge may land in the neighboring cell with another libm; the vectors stay away from edges, and elsewhere every correct H3 v4 implementation gives the same cell.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init geo-h3-lat-lng-to-cell --lang rust
cd geo-h3-lat-lng-to-cell && 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-h3-lat-lng-to-cell.wit
package root:component;

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

  export liberas:geo/h3-lat-lng-to-cell@0.1.0;
}
package liberas:geo@0.1.0 {
  interface types {
    record point {
      lat: f64,
      lon: f64,
    }
  }
  interface h3-lat-lng-to-cell {
    use types.{point};

    lat-lng-to-cell: func(at: point, resolution: u8) -> result<string, string>;
  }
}
Test vectors19›
geo-h3-lat-lng-to-cell.cases.json
[
  {
    "args": {
      "at": {
        "lat": 37.775938728915946,
        "lon": -122.41795063018799
      },
      "resolution": 9
    }
  },
  {
    "args": {
      "at": {
        "lat": 37.5665,
        "lon": 126.978
      },
      "resolution": 7
    }
  },
  {
    "args": {
      "at": {
        "lat": 51.5074,
        "lon": -0.1278
      },
      "resolution": 12
    }
  },
  {
    "args": {
      "at": {
        "lat": 0,
        "lon": 0
      },
      "resolution": 0
    }
  },
  {
    "args": {
      "at": {
        "lat": -33.8688,
        "lon": 151.2093
      },
      "resolution": 15
    }
  },
  {
    "args": {
      "at": {
        "lat": 64.7,
        "lon": -147.3
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": -89.9,
        "lon": 10
      },
      "resolution": 3
    }
  },
  {
    "args": {
      "at": {
        "lat": 89.9,
        "lon": -170
      },
      "resolution": 4
    }
  },
  {
    "args": {
      "at": {
        "lat": 40.689167,
        "lon": -74.044444
      },
      "resolution": 10
    }
  },
  {
    "args": {
      "at": {
        "lat": 35.6762,
        "lon": 139.6503
      },
      "resolution": 8
    }
  },
  {
    "args": {
      "at": {
        "lat": 1.3521,
        "lon": 103.8198
      },
      "resolution": 11
    }
  },
  {
    "args": {
      "at": {
        "lat": -22.9068,
        "lon": -43.1729
      },
      "resolution": 6
    }
  },
  {
    "args": {
      "at": {
        "lat": 0,
        "lon": 0
      },
      "resolution": 16
    }
  },
  {
    "args": {
      "at": {
        "lat": "NaN",
        "lon": 0
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": 0,
        "lon": "Infinity"
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": 90.0000001,
        "lon": 0
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": -91,
        "lon": 0
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": 0,
        "lon": 180.5
      },
      "resolution": 5
    }
  },
  {
    "args": {
      "at": {
        "lat": 0,
        "lon": "-Infinity"
      },
      "resolution": 5
    }
  }
]
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 3,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.