Skip to content

Add Cell.ToLatLng for H3 cell to geographic coordinate conversion#16

Merged
iandees merged 2 commits into
ZipRecruiter:mainfrom
jrhy:jeffr.cell-to-latlng
Mar 30, 2026
Merged

Add Cell.ToLatLng for H3 cell to geographic coordinate conversion#16
iandees merged 2 commits into
ZipRecruiter:mainfrom
jrhy:jeffr.cell-to-latlng

Conversation

@jrhy

@jrhy jrhy commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

The Cell.ToLatLng() method (reverse of NewCellFromLatLng) completes the
Cell ↔ LatLng round-trip. Downstream consumers need this to convert H3 cell
indices back to geographic coordinates for distance and centroid computations.

What is changed?

Adds three functions to complete the reverse H3 path:

  1. adjustOverageClassII (overage.go) — handles cells near icosahedral face boundaries
    by translating FaceIJK coordinates to the correct neighboring face. Includes
    faceNeighbors table, maxDimByCIIres, and unitScaleByCIIres.
  2. hex2dToGeo (coordijk.go) — reverse gnomonic projection (hex2d → LatLng)
  3. Cell.ToLatLng() (cell.go) — public API tying it all together via
    cellToFaceIjk with full pentagon handling (leading digit 5 rotation,
    leading digit 4 special overage, secondary overage loop)

All ported line-by-line from the H3 C reference implementation (faceijk.c, h3Index.c).

Testing

  • 12 ground-truth values verified against h3-python (uber/h3 C library)
  • All 12 pentagon base cells at res 0
  • All 122 base cells round-trip at res 0
  • 13 global cities × 8 resolutions (104 round-trip property tests)
  • Invalid cell error handling
  • Face boundary overage detection and translation
  • hex2dToGeogeoToHex2d round-trip across faces
  • Full existing test suite passes with no regressions

jrhy added 2 commits March 29, 2026 12:13
Add adjustOverageClassII, faceNeighbors table, maxDimByCIIres, and
unitScaleByCIIres. Ported line-by-line from the H3 C reference
implementation (faceijk.c). These are prerequisites for Cell.ToLatLng
which needs to handle cells near icosahedral face boundaries.
Add the reverse H3 path: Cell → FaceIJK → hex2d → LatLng.

- hex2dToGeo: reverse gnomonic projection (hex2d + face → LatLng)
- cellToFaceIjk: combines baseCellToFaceIjk + toFaceIjkWithInitializedFijk
  + adjustOverageClassII, with full pentagon handling
- Cell.ToLatLng(): public API returning cell center as LatLng

All functions ported from the H3 C reference implementation.

Test coverage:
- 12 ground-truth values verified against h3-python
- All 12 pentagon base cells at res 0
- All 122 base cells round-trip at res 0
- 13 global cities × 8 resolutions (104 round-trip tests)
- Invalid cell error handling
@jrhy
jrhy marked this pull request as ready for review March 30, 2026 16:21
@iandees
iandees merged commit b98ae93 into ZipRecruiter:main Mar 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants