forked from ryankurte/go-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile_test.go
159 lines (120 loc) · 4.67 KB
/
tile_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
/**
* go-mapbox Maps Module Tile HelperTests
* See https://www.mapbox.com/api-documentation/#maps for API information
* Run: go test -v -run ^TestTiles$ && open /tmp/mapbox-tile-test-*.jpg
*
* https://github.com/alex-ring/go-mapbox
* Copyright 2017 Ryan Kurte
*/
package maps
import (
"image/color"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alex-ring/go-mapbox/lib/base"
)
func TestTiles(t *testing.T) {
token := os.Getenv("MAPBOX_TOKEN")
if token == "" {
t.Error("Mapbox API token not found")
t.FailNow()
}
b := base.NewBase(token)
maps := NewMaps(b)
cache, err := NewFileCache("/tmp/go-mapbox-cache")
if err != nil {
t.Error(err)
t.FailNow()
}
maps.SetCache(cache)
size := uint64(512)
x, y, z := uint64(15), uint64(9), uint64(4)
loc := base.Location{-36.8485, 174.7633}
img, err := maps.GetTile(MapIDSatellite, x, y, z, MapFormatJpg90, true)
assert.Nil(t, err)
err = SaveImageJPG(img, "/tmp/mapbox-tile-test-1.jpg")
assert.Nil(t, err)
fire, _, err := LoadImage("../../fire64.png")
assert.Nil(t, err)
t.Run("Can draw in local tile space", func(t *testing.T) {
tile := NewTile(x, y, z, size, img)
tile.DrawLocalXY(fire, int(size/2), int(size/2), Center)
err := SaveImageJPG(tile, "/tmp/mapbox-tile-test-2.jpg")
assert.Nil(t, err)
})
t.Run("Can draw in global tile space", func(t *testing.T) {
tile := NewTile(x, y, z, size, img)
err := tile.DrawGlobalXY(fire, int(size*x+size/2), int(size*y+size/2), Center)
assert.Nil(t, err)
err = SaveImageJPG(tile, "/tmp/mapbox-tile-test-3.jpg")
assert.Nil(t, err)
})
t.Run("Can draw in world space", func(t *testing.T) {
tile := NewTile(x, y, z, size, img)
tile.DrawLocation(fire, loc, DrawConfig{Vertical: JustifyBottom, Horizontal: JustifyCenter})
tile.DrawLocation(fire, loc, DrawConfig{Vertical: JustifyCenter, Horizontal: JustifyLeft})
tile.DrawLocation(fire, loc, DrawConfig{Vertical: JustifyCenter, Horizontal: JustifyCenter})
tile.DrawLocation(fire, loc, DrawConfig{Vertical: JustifyCenter, Horizontal: JustifyRight})
tile.DrawLocation(fire, loc, DrawConfig{Vertical: JustifyTop, Horizontal: JustifyCenter})
err := SaveImageJPG(tile, "/tmp/mapbox-tile-test-4.jpg")
assert.Nil(t, err)
})
t.Run("Can render to composite tiles", func(t *testing.T) {
locA := base.Location{-45.942805, 166.568500}
locB := base.Location{-34.2186101, 183.4015517}
x1, y1, _, _ := GetEnclosingTileIDs(locA, locB, 6)
images, err := maps.GetEnclosingTiles(MapIDSatellite, locA, locB, 6, MapFormatJpg90, true)
if err != nil {
t.Error(err)
t.FailNow()
}
img := StitchTiles(images)
tile := NewTile(x1, y1, 6, size, img)
tile.DrawLocation(fire, base.Location{-41.2865, 174.7762}, DrawConfig{Vertical: JustifyBottom, Horizontal: JustifyCenter})
tile.DrawLocation(fire, base.Location{-36.8485, 174.7633}, DrawConfig{Vertical: JustifyBottom, Horizontal: JustifyCenter})
tile.DrawLocation(fire, base.Location{-43.5321, 172.6362}, DrawConfig{Vertical: JustifyBottom, Horizontal: JustifyCenter})
err = SaveImageJPG(tile, "/tmp/mapbox-tile-test-5.jpg")
assert.Nil(t, err)
})
t.Run("Can interpolate lines over complex tiles", func(t *testing.T) {
locA := base.Location{-45.942805, 166.568500}
locB := base.Location{-34.2186101, 183.4015517}
x1, y1, _, _ := GetEnclosingTileIDs(locA, locB, 6)
images, err := maps.GetEnclosingTiles(MapIDSatellite, locA, locB, 6, MapFormatJpg90, true)
if err != nil {
t.Error(err)
t.FailNow()
}
img := StitchTiles(images)
tile := NewTile(x1, y1, 6, size, img)
a, b, c := base.Location{-36.8485, 174.7633}, base.Location{-41.2865, 174.7762}, base.Location{-43.5321, 172.6362}
tile.DrawLine(a, b, color.RGBA{R: 255, G: 0, B: 0, A: 255})
tile.DrawLine(b, c, color.RGBA{R: 255, G: 0, B: 0, A: 255})
tile.DrawLine(c, a, color.RGBA{R: 255, G: 0, B: 0, A: 255})
err = SaveImageJPG(tile, "/tmp/mapbox-tile-test-6.jpg")
assert.Nil(t, err)
})
t.Run("Can fetch terrain data points", func(t *testing.T) {
locA := base.Location{-39.5, 173.5}
locB := base.Location{-39.0, 174.5}
taranaki := base.Location{-39.295182, 174.063668}
level := uint64(6)
images, err := maps.GetEnclosingTiles(MapIDTerrainRGB, locA, locB, level, MapFormatPngRaw, true)
if err != nil {
t.Error(err)
t.FailNow()
}
img := StitchTiles(images)
x1, y1, _, _ := GetEnclosingTileIDs(locA, locB, level)
tile := NewTile(x1, y1, level, size, img)
err = SaveImageJPG(tile, "/tmp/mapbox-tile-test-7.png")
assert.Nil(t, err)
alt, err := tile.GetAltitude(taranaki)
assert.Nil(t, err)
assert.InDelta(t, 2400, alt, 100)
flattened := tile.FlattenAltitudes(3000)
err = SaveImageJPG(flattened, "/tmp/mapbox-tile-test-8.png")
assert.Nil(t, err)
})
}