-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgs84_test.go
201 lines (195 loc) · 4.43 KB
/
wgs84_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package wgs84
import (
"math"
"reflect"
"testing"
)
func TestGCJ02_ToWGS84(t *testing.T) {
// go test -v -timeout 30s -run ^TestPoint_GCJ02ToWGS84$ github.com/zhangge3992513/wgsconvert
tests := []struct {
name string
p GCJ02
wantWgs84 WGS84
}{
// TODO: Add test cases.
{
name: "cgj-02坐标转1换为高德wgs84",
p: GCJ02{
Point{
Longitude: 113.942831, // wgs84: 113.93795567290164 113.93796085386425 113.93796084819188 113.93796084819807 113.93796084819807
Latitude: 22.530282, //wgs84: 22.533298820468712
},
},
wantWgs84: WGS84{
Point{
Longitude: 113.937961,
Latitude: 22.533307,
},
},
},
{
name: "cgj-02新疆乌恰县换为wgs84",
p: GCJ02{
Point{
Longitude: 75.25905448676403, //
Latitude: 39.71932164456469,
},
},
wantWgs84: WGS84{
Point{
Longitude: 75.255949, // √
Latitude: 39.719029, // √
},
},
},
{
name: "cgj-02黑龙江佳木斯抚远镇wgs84",
p: GCJ02{
Point{
Longitude: 134.30364464514864, // 134.29595,48.36794,
Latitude: 48.3703309490954,
},
},
wantWgs84: WGS84{
Point{
Longitude: 134.29595, // √ 结果: 134.29595000000018
Latitude: 48.36794, // √ 结果: 48.36794000000012
},
},
},
}
t.Log(math.Pi)
t.Log(math.Pi + 1.1)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotWgs84Point := tt.p.ToWGS84(); !reflect.DeepEqual(gotWgs84Point, tt.wantWgs84) {
t.Errorf("Point.GCJ02ToWGS84() = %v, want %v", gotWgs84Point, tt.wantWgs84)
}
})
}
}
// 11次 BenchmarkPoint_GCJ02ToWGS84-4 293467 3643 ns/op 0 B/op 0 allocs/op 0 allocs/op
// 4 次 BenchmarkPoint_GCJ02ToWGS84-4 925525 1445 ns/op 0 B/op 0 allocs/op
// go test -benchmem -run=^$ -bench ^(BenchmarkPoint_GCJ02ToWGS84)$ github.com/zhangge3992513/wgsconvert
func BenchmarkGCJ02_ToWGS84(t *testing.B) {
p := GCJ02{
Point{
Longitude: 134.30364464514864, // 134.29595,48.36794,
Latitude: 48.3703309490954,
},
}
t.ResetTimer()
t.StartTimer()
for i := 0; i < t.N; i++ {
p.ToWGS84()
}
t.StopTimer()
}
func TestWGS84_ToGCJ02(t *testing.T) {
tests := []struct {
name string
p WGS84
wantGcjP GCJ02
}{
// TODO: Add test cases.
{
name: "wgs84坐标转1换为高德cgj-02",
p: WGS84{
Point{
Longitude: 116.46706996, // cgj: 116.4732071375548
Latitude: 39.99188446, // cgj: 39.9932029943646
},
},
wantGcjP: GCJ02{
Point{
Longitude: 116.4732071375548,
Latitude: 39.9932029943646,
},
},
}, {
name: "wgs84坐标转2换为高德cgj-02",
p: WGS84{
Point{
Longitude: 113.937961, // cgj: 113.94283115195938 gps: 113.937961
Latitude: 22.533307, // cgj: 22.53028209851657 gps: 22.533307
},
},
wantGcjP: GCJ02{
Point{
Longitude: 113.94283115195938,
Latitude: 22.53028209851657,
},
},
}, {
name: "黑龙江乌恰县",
p: WGS84{
Point{
Longitude: 75.255949, //
Latitude: 39.719029, //
},
},
wantGcjP: GCJ02{
Point{
Longitude: 75.25905680338599,
Latitude: 39.719323459202,
},
},
},
{
name: "黑龙江佳木斯抚远镇", // 结果: 134.30364464514864,48.3703309490954, 偏差600多米!
p: WGS84{
Point{
Longitude: 134.29595,
Latitude: 48.36794,
},
},
wantGcjP: GCJ02{
Point{
Longitude: 134.311357,
Latitude: 48.372734,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotGcjP := tt.p.ToGCJ02(); !reflect.DeepEqual(gotGcjP, tt.wantGcjP) {
t.Errorf("WGS84.ToGCJ02() = %v, want %v", gotGcjP, tt.wantGcjP)
}
})
}
}
func TestPoint_DistanceFrom(t *testing.T) {
type args struct {
desp Point
}
tests := []struct {
name string
p Point
args args
wantDistance float64
}{
// TODO: Add test cases.
{
name: "点1与点2之间的距离",
p: Point{
Longitude: 113.00458767361198,
Latitude: 9.998094075521,
},
args: args{
desp: Point{
Longitude: 114.00443684895902,
Latitude: 11.998577473959,
},
},
wantDistance: 248048.606567,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotDistance := tt.p.DistanceFrom(tt.args.desp); gotDistance != tt.wantDistance {
t.Errorf("Point.DistanceFrom() = %v, want %v", gotDistance, tt.wantDistance)
}
})
}
}