Skip to content

Commit

Permalink
Fix bug in mercator y calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Feb 28, 2022
1 parent ea78ee3 commit daaed9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func semicirclesToRadians(s int32) float64 {

func mercatorMeters(lat, lon float64) (float64, float64) {
x := 6_378_137 * lon
y := 6_378_137 * math.Log(math.Tan(lat+(math.Pi/4))) / (2 * math.Pi)
y := 6_378_137 * math.Log(math.Tan((2*lat+math.Pi)/4))
return x, y
}

Expand Down
29 changes: 29 additions & 0 deletions geo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"math"
"testing"
)

func TestMercatorMeters(t *testing.T) {
testCases := []struct {
lat, lon float64
}{
{0, 0},
{0, -180},
{0, 180},
{45, 0},
{-45, 0},
{80, 0},
{-80, 0},
}
for i, testCase := range testCases {
x, y := mercatorMeters(degreesToRadians(testCase.lat), degreesToRadians(testCase.lon))
if math.IsNaN(x) {
t.Fatal("test case", i, "failed: expected x number")
}
if math.IsNaN(y) {
t.Fatal("test case", i, "failed: expected y number")
}
}
}

0 comments on commit daaed9d

Please sign in to comment.