add proj.wgsToMerc/mercToWgs functions

master
Oliver Tonnhofer 2013-05-06 16:17:35 +02:00
parent d159ccac0e
commit 1897a0d30d
2 changed files with 48 additions and 0 deletions

19
proj/proj.go Normal file
View File

@ -0,0 +1,19 @@
package proj
import (
"math"
)
const pole = 6378137 * math.Pi // 20037508.342789244
func wgsToMerc(long, lat float64) (x, y float64) {
x = long * pole / 180.0
y = math.Log(math.Tan((90.0+lat)*math.Pi/360.0)) / math.Pi * pole
return x, y
}
func mercToWgs(x, y float64) (long, lat float64) {
long = 180.0 * x / pole
lat = 180.0 / math.Pi * (2*math.Atan(math.Exp((y/pole)*math.Pi)) - math.Pi/2)
return long, lat
}

29
proj/proj_test.go Normal file
View File

@ -0,0 +1,29 @@
package proj
import (
"math"
"testing"
)
func TestWgsToMerc(t *testing.T) {
x, y := wgsToMerc(0, 0)
if x != 0 || y != 0 {
t.Fatalf("%v %v", x, y)
}
x, y = wgsToMerc(8, 53)
if math.Abs(x-890555.9263461898) > 1e-6 || math.Abs(y-6982997.920389788) > 1e-6 {
t.Fatalf("%v %v", x, y)
}
}
func TestMercToWgs(t *testing.T) {
long, lat := mercToWgs(0, 0)
if long != 0 || lat != 0 {
t.Fatalf("%v %v", long, lat)
}
long, lat = mercToWgs(890555.9263461898, 6982997.920389788)
if math.Abs(long-8) > 1e-6 || math.Abs(lat-53) > 1e-6 {
t.Fatalf("%v %v", long, lat)
}
}