This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
atlas.go
258 lines (213 loc) · 6.12 KB
/
atlas.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Package atlas provides an abstraction for a collection of Maps.
package atlas
import (
"context"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/go-spatial/geom/slippy"
tegola "github.com/go-spatial/tegola-postgis"
"github.com/go-spatial/tegola-postgis/cache"
)
var (
simplifyGeometries bool = true
simplificationMaxZoom uint = 10
)
func init() {
// TODO(arolek): the following env variable processing was pulled form the mvt package when
// geometry processing was pulled out of the encoding package. This functionality could be
// deprecated/removed as it's not well documented and is really a band aid to work around
// some simplification issues. These concepts could just as easily live in the config file.
options := strings.ToLower(os.Getenv("TEGOLA_OPTIONS"))
if strings.Contains(options, "dontsimplifygeo") {
simplifyGeometries = false
log.Println("simplification is disable")
}
if strings.Contains(options, "simplifymaxzoom=") {
idx := strings.Index(options, "simplifymaxzoom=")
idx += 16
eidx := strings.IndexAny(options[idx:], ",.\t \n")
if eidx == -1 {
eidx = len(options)
} else {
eidx += idx
}
i, err := strconv.Atoi(options[idx:eidx])
if err != nil {
log.Printf("invalid value for SimplifyMaxZoom (%v). using default (%v).", options[idx:eidx], simplificationMaxZoom)
return
}
simplificationMaxZoom = uint(i + 1)
log.Printf("SimplifyMaxZoom set to (%v)", simplificationMaxZoom)
}
}
// defaultAtlas is instanitated for convenience
var defaultAtlas = &Atlas{}
const (
// MaxZoom will not render tile beyond this zoom level
MaxZoom = tegola.MaxZ
)
// Atlas holds a collection of maps.
// If the pointer to Atlas is nil, it will make use of the default atlas; as the container for maps.
// This is equaivalent to using the functions in the package.
// An Atlas is safe to use concurrently.
type Atlas struct {
// for managing current access to the map container
sync.RWMutex
// hold maps
maps map[string]Map
// holds a reference to the cache backend
cacher cache.Interface
}
// AllMaps returns a slice of all maps contained in the Atlas so far.
func (a *Atlas) AllMaps() []Map {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.AllMaps()
}
a.RLock()
defer a.RUnlock()
var maps []Map
for i := range a.maps {
m := a.maps[i]
// make an explicit copy of the layers
layers := make([]Layer, len(m.Layers))
copy(layers, m.Layers)
m.Layers = layers
maps = append(maps, m)
}
return maps
}
// SeedMapTile will generate a tile and persist it to the
// configured cache backend
func (a *Atlas) SeedMapTile(ctx context.Context, m Map, tile *slippy.Tile) error {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.SeedMapTile(ctx, m, tile)
}
// confirm we have a cache backend
if a.cacher == nil {
return ErrMissingCache
}
// encode the tile
b, err := m.Encode(ctx, tile)
if err != nil {
return err
}
// cache key
key := cache.Key{
MapName: m.Name,
Z: tile.Z,
X: tile.X,
Y: tile.Y,
}
return a.cacher.Set(&key, b)
}
// PurgeMapTile will purge a map tile from the configured cache backend
func (a *Atlas) PurgeMapTile(m Map, tile *slippy.Tile) error {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.PurgeMapTile(m, tile)
}
if a.cacher == nil {
return ErrMissingCache
}
// cache key
key := cache.Key{
MapName: m.Name,
Z: tile.Z,
X: tile.X,
Y: tile.Y,
}
return a.cacher.Purge(&key)
}
// Map looks up a Map by name and returns a copy of the Map
func (a *Atlas) Map(mapName string) (Map, error) {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.Map(mapName)
}
a.RLock()
defer a.RUnlock()
m, ok := a.maps[mapName]
if !ok {
return Map{}, ErrMapNotFound{
Name: mapName,
}
}
// make an explicit copy of the layers
layers := make([]Layer, len(m.Layers))
copy(layers, m.Layers)
m.Layers = layers
return m, nil
}
// AddMap registers a map by name. if the map already exists it will be overwritten
func (a *Atlas) AddMap(m Map) {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
defaultAtlas.AddMap(m)
return
}
a.Lock()
defer a.Unlock()
if a.maps == nil {
a.maps = map[string]Map{}
}
a.maps[m.Name] = m
}
// GetCache returns the registered cache if one is registered, otherwise nil
func (a *Atlas) GetCache() cache.Interface {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.GetCache()
}
return a.cacher
}
// SetCache sets the cache backend
func (a *Atlas) SetCache(c cache.Interface) {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
defaultAtlas.SetCache(c)
return
}
a.cacher = c
}
// AllMaps returns all registered maps in defaultAtlas
func AllMaps() []Map {
return defaultAtlas.AllMaps()
}
// GetMap returns a copy of the a map by name from defaultAtlas. if the map does not exist it will return an error
func GetMap(mapName string) (Map, error) {
return defaultAtlas.Map(mapName)
}
// AddMap registers a map by name with defaultAtlas. if the map already exists it will be overwritten
func AddMap(m Map) {
defaultAtlas.AddMap(m)
}
// GetCache returns the registered cache for defaultAtlas, if one is registered, otherwise nil
func GetCache() cache.Interface {
return defaultAtlas.GetCache()
}
// SetCache sets the cache backend for defaultAtlas
func SetCache(c cache.Interface) {
defaultAtlas.SetCache(c)
}
// SeedMapTile will generate a tile and persist it to the
// configured cache backend for the defaultAtlas
func SeedMapTile(ctx context.Context, m Map, tile *slippy.Tile) error {
return defaultAtlas.SeedMapTile(ctx, m, tile)
}
// PurgeMapTile will purge a map tile from the configured cache backend
// for the defaultAtlas
func PurgeMapTile(m Map, tile *slippy.Tile) error {
return defaultAtlas.PurgeMapTile(m, tile)
}