-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj_test.go
75 lines (64 loc) · 2.25 KB
/
proj_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package geo
import (
"fmt"
"testing"
vec2d "github.com/flywave/go3d/float64/vec2"
"github.com/flywave/go-geo/gcj02"
"github.com/stretchr/testify/assert"
)
var tests = []struct {
wgsLat, wgsLng float64
gcjLat, gcjLng float64
}{
{31.1774276, 121.5272106, 31.17530398364597, 121.531541859215}, // shanghai
{22.543847, 113.912316, 22.540796131694766, 113.9171764808363}, // shenzhen
{39.911954, 116.377817, 39.91334545536069, 116.38404722455657}, // beijing
}
func toString(lat, lng float64) string {
return fmt.Sprintf("%.5f,%.5f", lat, lng)
}
func TestGCJ02(t *testing.T) {
for i, test := range tests {
gcjLat, gcjLng := gcj02.WGS84toGCJ02(test.wgsLat, test.wgsLng)
got := toString(gcjLat, gcjLng)
target := toString(test.gcjLat, test.gcjLng)
assert.Equal(t, got, target, "test %d", i)
}
for i, test := range tests {
wgsLat, wgsLng := gcj02.GCJ02toWGS84(test.gcjLat, test.gcjLng)
d := gcj02.Distance(wgsLat, wgsLng, test.wgsLat, test.wgsLng)
assert.Equal(t, d < 5, true, "test %d, distance: %f", i, d)
}
for i, test := range tests {
wgsLat, wgsLng := gcj02.GCJ02toWGS84Exact(test.gcjLat, test.gcjLng)
d := gcj02.Distance(wgsLat, wgsLng, test.wgsLat, test.wgsLng)
assert.Equal(t, d < 0.5, true, "test %d, distance: %f", i, d)
}
}
func TestGCJ02Proj(t *testing.T) {
srs4326 := newSRSProj4("EPSG:4326")
pgcj02 := newGCJ02Proj(true)
for i, test := range tests {
pts := srs4326.TransformTo(pgcj02, []vec2d.T{{test.wgsLng, test.wgsLat}})
got := toString(pts[0][1], pts[0][0])
target := toString(test.gcjLat, test.gcjLng)
assert.Equal(t, got, target, "test %d", i)
}
}
func TestGCJ02To900913Proj(t *testing.T) {
srs900913 := newSRSProj4("EPSG:900913")
srs4326 := NewProj(4326)
pgcj02 := newGCJ02Proj(true)
for i, test := range tests {
p900913 := srs4326.TransformTo(srs900913, []vec2d.T{{test.wgsLng, test.wgsLat}})
pts := srs900913.TransformTo(pgcj02, p900913)
got := toString(pts[0][1], pts[0][0])
target := toString(test.gcjLat, test.gcjLng)
assert.Equal(t, got, target, "test %d", i)
}
for i, test := range tests {
pts := pgcj02.TransformTo(srs4326, []vec2d.T{{test.gcjLng, test.gcjLat}})
d := gcj02.Distance(pts[0][1], pts[0][0], test.wgsLat, test.wgsLng)
assert.Equal(t, d < 0.5, true, "test %d, distance: %f", i, d)
}
}