forked from ryankurte/go-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mercator_test.go
90 lines (70 loc) · 2.62 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
83
84
85
86
87
88
89
90
/**
* go-mapbox Maps Module Tests
* Wraps the mapbox Maps API for server side use
* See https://www.mapbox.com/api-documentation/#maps for API information
*
* https://github.com/alex-ring/go-mapbox
* Copyright 2017 Ryan Kurte
*/
package maps
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alex-ring/go-mapbox/lib/base"
)
const delta = 1e-6
func TestMercator(t *testing.T) {
zoom := uint64(4)
size := uint64(256)
fsize := float64(size)
loc := base.Location{-45.942805, 166.568500}
t.Run("Performs mercator projections to global pixels", func(t *testing.T) {
x, y := MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom, size)
assert.EqualValues(t, 15.0, math.Floor(x/fsize))
assert.EqualValues(t, 10.0, math.Floor(y/fsize))
// Increase zoom scale x2 multiplies location by 4
x, y = MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom+2, size)
assert.EqualValues(t, 15.0*4+1, math.Floor(x/fsize))
assert.EqualValues(t, 10.0*4+1, math.Floor(y/fsize))
// Doubling tile size doubles pixel location
x, y = MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom, size*2)
assert.EqualValues(t, 15.0*2, math.Floor(x/fsize))
assert.EqualValues(t, 10.0*2, math.Floor(y/fsize))
})
t.Run("Performs mercator projections to tile IDs", func(t *testing.T) {
x, y := MercatorLocationToTileID(loc.Latitude, loc.Longitude, zoom, size)
assert.EqualValues(t, 15, x)
assert.EqualValues(t, 10, y)
// Increasing zoom level by 2 multiplies tile IDs by 4
x, y = MercatorLocationToTileID(loc.Latitude, loc.Longitude, zoom+2, size)
assert.EqualValues(t, 15*4+1, x)
assert.EqualValues(t, 10*4+1, y)
// Doubling tile size does not change tile ID
x, y = MercatorLocationToTileID(loc.Latitude, loc.Longitude, zoom, size*2)
assert.EqualValues(t, 15, x)
assert.EqualValues(t, 10, y)
})
t.Run("Reverses mercator projections", func(t *testing.T) {
x, y := MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom, size)
lat2, lng2 := MercatorPixelToLocation(x, y, zoom, size)
assert.InDelta(t, loc.Latitude, lat2, delta)
assert.InDelta(t, loc.Longitude, lng2, delta)
})
}
func BenchmarkMercator(b *testing.B) {
zoom := uint64(4)
loc := base.Location{Latitude: -45.942805, Longitude: 166.568500}
size := uint64(256)
x, y := MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom, size)
b.Run("Forward projection", func(b *testing.B) {
for i := 0; i < b.N; i++ {
MercatorLocationToPixel(loc.Latitude, loc.Longitude, zoom, size)
}
})
b.Run("Reverse projection", func(b *testing.B) {
for i := 0; i < b.N; i++ {
MercatorPixelToLocation(x, y, zoom, size)
}
})
}