Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x/h3go/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func (b CellBoundary) areaRads2() float64 {
// arc from ll to other (lat/lng in degrees), following the d3-geo spherical-area
// formulation.
func (ll LatLng) cagnoli(other LatLng) float64 {
lat := ll.Lat*degsToRads/2 + math.Pi/4
otherLat := other.Lat*degsToRads/2 + math.Pi/4
lat := ll.Lat*DegsToRads/2 + math.Pi/4
otherLat := other.Lat*DegsToRads/2 + math.Pi/4

sa := math.Sin(lat) * math.Sin(otherLat)
ca := math.Cos(lat) * math.Cos(otherLat)

delta := (other.Lng - ll.Lng) * degsToRads
delta := (other.Lng - ll.Lng) * DegsToRads
sinDelta := math.Sin(delta)
cosDelta := math.Cos(delta)

Expand Down
35 changes: 34 additions & 1 deletion x/h3go/area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,45 @@ func TestCellAreaRes0SumsToSphere(t *testing.T) {
assertRelClose(t, sum, 4*math.Pi, "res0 area sum")
}

// TestCellAreaNullIslandTable ports the testH3CellArea.c specific_cell_area
// regression: the exact area in km² of the cell containing (0, 0) at each
// resolution, to a tight absolute tolerance.
func TestCellAreaNullIslandTable(t *testing.T) {
t.Parallel()

areasKm2 := []float64{
2.562182162955496e+06, 4.476842017201860e+05, 6.596162242711056e+04,
9.228872919002590e+03, 1.318694490797110e+03, 1.879593512281298e+02,
2.687164354763186e+01, 3.840848847060638e+00, 5.486939641329893e-01,
7.838600808637444e-02, 1.119834221989390e-02, 1.599777169186614e-03,
2.285390931423380e-04, 3.264850232091780e-05, 4.664070326136774e-06,
6.662957615868888e-07,
}

// The C fixture checks res 0..MAX_H3_RES-1.
for res := 0; res < MaxResolution; res++ {
cell, err := LatLngToCell(LatLng{Lat: 0, Lng: 0}, res)
if err != nil {
t.Fatalf("LatLngToCell(res %d): %v", res, err)
}

area, err := CellAreaKm2(cell)
if err != nil {
t.Fatalf("CellAreaKm2(res %d): %v", res, err)
}

if math.Abs(area-areasKm2[res]) >= 1e-8 {
t.Fatalf("CellAreaKm2(res %d): got %.15e, want %.15e", res, area, areasKm2[res])
}
}
}

// TestCellAreaInvalidBaseCell covers the error branch of CellAreaRads2/Km2/M2,
// reached when the boundary cannot be computed for an out-of-range base cell.
func TestCellAreaInvalidBaseCell(t *testing.T) {
t.Parallel()

bad := Cell(h3Init) | Cell(cellMode)<<modeOffset | Cell(numBaseCells)<<baseCellOffset
bad := Cell(h3Init) | Cell(cellMode)<<modeOffset | Cell(NumBaseCells)<<baseCellOffset

if _, err := CellAreaRads2(bad); err == nil {
t.Fatal("CellAreaRads2: got nil error, want failure")
Expand Down
Loading
Loading