Add Cell.ToLatLng for H3 cell to geographic coordinate conversion#16
Merged
Conversation
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
iandees
approved these changes
Mar 30, 2026
jrhy
marked this pull request as ready for review
March 30, 2026 16:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The
Cell.ToLatLng()method (reverse ofNewCellFromLatLng) completes theCell ↔ 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:
adjustOverageClassII(overage.go) — handles cells near icosahedral face boundariesby translating FaceIJK coordinates to the correct neighboring face. Includes
faceNeighborstable,maxDimByCIIres, andunitScaleByCIIres.hex2dToGeo(coordijk.go) — reverse gnomonic projection (hex2d → LatLng)Cell.ToLatLng()(cell.go) — public API tying it all together viacellToFaceIjkwith 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
hex2dToGeo↔geoToHex2dround-trip across faces