-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercator_test.go
82 lines (67 loc) · 2.41 KB
/
mercator_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
package geod_test
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
geod "github.com/starboard-nz/go-geodesy"
)
func TestMercator(t *testing.T) {
const δ = 0.000001
testData := map[geod.LatLon]geod.MercatorPoint{
geod.LatLon{Latitude: 0, Longitude: 0}: geod.MercatorPoint{Y: 0.5, X: 0.5},
geod.LatLon{Latitude: 0, Longitude: -180}: geod.MercatorPoint{Y: 0.5, X: 0},
geod.LatLon{Latitude: 0, Longitude: 180}: geod.MercatorPoint{Y: 0.5, X: 1},
geod.LatLon{Latitude: geod.MercatorMaxLat, Longitude: 180}: geod.MercatorPoint{Y: 1, X: 1},
geod.LatLon{Latitude: -geod.MercatorMaxLat, Longitude: 180}: geod.MercatorPoint{Y: 0, X: 1},
geod.LatLon{Latitude: -45, Longitude: 0}: geod.MercatorPoint{Y: 0.359725, X: 0.5},
geod.LatLon{Latitude: 45, Longitude: 0}: geod.MercatorPoint{Y: 0.640275, X: 0.5},
}
for ll, exp := range testData {
mp := ll.MercatorPoint()
assert.InDeltaf(t, exp.X, mp.X, δ, "X coordinate of LatLon{%f, %f} - expected %f got %f",
ll.Latitude, ll.Longitude, exp.X, mp.X)
assert.InDeltaf(t, exp.Y, mp.Y, δ, "Y coordinate of LatLon{%f, %f} - expected %f got %f",
ll.Latitude, ll.Longitude, exp.Y, mp.Y)
}
}
func TestMercatorConversionAndBack(t *testing.T) {
const δ = 0.000001
for i := 0; i < 10; i++ {
ll := geod.LatLon{
Longitude: geod.Degrees(rand.Float64()*360 - 180), // nolint:gosec
Latitude: geod.Degrees(rand.Float64()*160 - 80), // nolint:gosec
}
mp := ll.MercatorPoint()
ll1 := mp.LatLon()
assert.InDelta(t, float64(ll.Longitude), float64(ll1.Longitude), δ)
assert.InDelta(t, float64(ll.Latitude), float64(ll1.Latitude), δ)
}
}
func BenchmarkMercator(b *testing.B) {
const N = 100000
testPoints := make([]geod.LatLon, N)
for i := 0; i < N; i++ {
testPoints[i] = geod.LatLon{
Longitude: geod.Degrees(rand.Float64()*360 - 180), // nolint:gosec
Latitude: geod.Degrees(rand.Float64()*160 - 80), // nolint:gosec
}
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = testPoints[n%N].MercatorPoint()
}
}
func BenchmarkInverseMercator(b *testing.B) {
const N = 100000
testPoints := make([]geod.MercatorPoint, N)
for i := 0; i < N; i++ {
testPoints[i] = geod.MercatorPoint{
X: rand.Float64(), // nolint:gosec
Y: rand.Float64(), // nolint:gosec
}
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = testPoints[n%N].LatLon()
}
}