Skip to content
Merged
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
3 changes: 2 additions & 1 deletion internal/mercator/mercator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func ToGeo(x, y float64, level uint32) (lng, lat float64) {
maxtiles := float64(uint64(1 << level))

lng = 360.0 * (x/maxtiles - 0.5)
lat = 2.0*math.Atan(math.Exp(math.Pi-(2*math.Pi)*(y/maxtiles)))*(180.0/math.Pi) - 90.0
// Adding + 0.0 ensures arm64 returns precise floats; see https://github.com/paulmach/orb/issues/156.
lat = 2.0*math.Atan(math.Exp(math.Pi-(2*math.Pi)*(y/maxtiles)))*(180.0/math.Pi) + 0.0 - 90.0

return lng, lat
}
32 changes: 24 additions & 8 deletions internal/mercator/mercator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (

func TestScalarMercator(t *testing.T) {
x, y := ToPlanar(0, 0, 31)
lat, lng := ToGeo(x, y, 31)

if lat != 0.0 {
t.Errorf("Scalar Mercator, latitude should be 0: %f", lat)
}
lng, lat := ToGeo(x, y, 31)

if lng != 0.0 {
t.Errorf("Scalar Mercator, longitude should be 0: %f", lng)
}

if lat != 0.0 {
t.Errorf("Scalar Mercator, latitude should be 0: %f", lat)
}

// specific case
if x, y := ToPlanar(-87.65005229999997, 41.850033, 20); math.Floor(x) != 268988 || math.Floor(y) != 389836 {
t.Errorf("Scalar Mercator, projection incorrect, got %v %v", x, y)
Expand All @@ -39,13 +39,14 @@ func TestScalarMercator(t *testing.T) {
t.Error("Scalar Mercator, lat is NaN")
}

if math.Abs(lng-city[1]) > Epsilon {
t.Errorf("Scalar Mercator, longitude miss match: %f != %f", lng, city[1])
}

if math.Abs(lat-city[0]) > Epsilon {
t.Errorf("Scalar Mercator, latitude miss match: %f != %f", lat, city[0])
}

if math.Abs(lng-city[1]) > Epsilon {
t.Errorf("Scalar Mercator, longitude miss match: %f != %f", lng, city[1])
}
}

// test polar regions
Expand All @@ -57,3 +58,18 @@ func TestScalarMercator(t *testing.T) {
t.Errorf("Scalar Mercator, bottom of the world error, got %v", y)
}
}

func TestToGeoPrecision(t *testing.T) {
for level := float64(1); level < 35; level++ {
n := math.Pow(2, level-1)
// tile with north west coordinate of (0, 0) at each zoom level
lng, lat := ToGeo(n, n, uint32(level))
if lng != 0.0 {
t.Errorf("ToGeo, longitude on level %2.0f should be 0: %f", level, lng)
}

if lat != 0.0 {
t.Errorf("ToGeo, latitude on level %2.0f should be 0: %f", level, lat)
}
}
}
Loading