forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlas.go
339 lines (285 loc) · 8.16 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Package atlas provides an abstraction for a collection of Maps.
package atlas
import (
"context"
"os"
"strconv"
"strings"
"sync"
"github.com/go-spatial/geom/slippy"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/cache"
"github.com/go-spatial/tegola/internal/log"
"github.com/go-spatial/tegola/internal/observer"
"github.com/go-spatial/tegola/observability"
)
var (
simplifyGeometries bool
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.Debugf("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.Errorf("invalid value for SimplifyMaxZoom (%v). using default (%v).", options[idx:eidx], simplificationMaxZoom)
return
}
simplificationMaxZoom = uint(i + 1)
log.Debugf("SimplifyMaxZoom set to (%v)", simplificationMaxZoom)
}
}
// defaultAtlas is instantiated 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 equivalent 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
// holds a reference to the observer backend
observer observability.Interface
// publishBuildInfo indicates if we should publish the build info on change of observer
// this is set by calling PublishBuildInfo, which will publish
// the build info on the observer and insure changes to observer
// also publishes the build info.
publishBuildInfo bool
}
// 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, z, x, y uint) 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, z, x, y)
}
ctx = context.WithValue(ctx, observability.ObserveVarMapName, m.Name)
// confirm we have a cache backend
if a.cacher == nil {
return ErrMissingCache
}
tile := slippy.NewTile(z, x, y)
// encode the tile
b, err := m.Encode(ctx, tile)
if err != nil {
return err
}
// cache key
key := cache.Key{
MapName: m.Name,
Z: z,
X: x,
Y: 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 *tegola.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
}
// let's see if we have an observer set. If so, we need to wrap
// the given cache with the observer.
if a.observer != nil {
c = a.observer.InstrumentedCache(c)
}
a.cacher = c
}
// SetObservability will set the observability backend
func (a *Atlas) SetObservability(o observability.Interface) {
if a == nil {
defaultAtlas.SetObservability(o)
return
}
if a.observer != nil {
a.observer.Shutdown()
}
a.observer = o
if a.publishBuildInfo {
a.observer.Init()
}
if a.cacher != nil {
if w, ok := a.cacher.(observability.Cache); ok && w.IsObserver() {
a.cacher = o.InstrumentedCache(w.Original())
} else {
a.cacher = o.InstrumentedCache(a.cacher)
}
}
for _, aMap := range a.maps {
collectors, err := aMap.Collectors("tegola", o.CollectorConfig)
if err != nil {
log.Errorf("failed to register collector for map: %v ignoring", aMap.Name)
continue
}
o.MustRegister(collectors...)
}
}
func (a *Atlas) Observer() observability.Interface {
if a == nil {
return defaultAtlas.Observer()
}
if a.observer == nil {
return nil
}
if _, ok := a.observer.(observer.Null); ok {
return nil
}
return a.observer
}
func (a *Atlas) StartSubProcesses() {
if a == nil {
defaultAtlas.StartSubProcesses()
return
}
o := a.Observer()
if o == nil {
return
}
a.publishBuildInfo = true
o.Init()
}
// 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, z, x, y uint) error {
return defaultAtlas.SeedMapTile(ctx, m, z, x, y)
}
// PurgeMapTile will purge a map tile from the configured cache backend
// for the defaultAtlas
func PurgeMapTile(m Map, tile *tegola.Tile) error {
return defaultAtlas.PurgeMapTile(m, tile)
}
// SetObservability sets the observability backend for the defaultAtlas
func SetObservability(o observability.Interface) { defaultAtlas.SetObservability(o) }
func StartSubProcesses() { defaultAtlas.StartSubProcesses() }