forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
413 lines (346 loc) · 12 KB
/
map.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package atlas
import (
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/go-spatial/tegola/observability"
"github.com/golang/protobuf/proto"
"github.com/go-spatial/geom"
"github.com/go-spatial/geom/encoding/mvt"
"github.com/go-spatial/geom/slippy"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/basic"
"github.com/go-spatial/tegola/dict"
"github.com/go-spatial/tegola/internal/convert"
"github.com/go-spatial/tegola/internal/log"
"github.com/go-spatial/tegola/maths/simplify"
"github.com/go-spatial/tegola/maths/validate"
"github.com/go-spatial/tegola/provider"
"github.com/go-spatial/tegola/provider/debug"
)
// NewWebMercatorMap creates a new map with the necessary default values
func NewWebMercatorMap(name string) Map {
return Map{
Name: name,
// default bounds
Bounds: tegola.WGS84Bounds,
Layers: []Layer{},
SRID: tegola.WebMercator,
TileExtent: 4096,
TileBuffer: uint64(tegola.DefaultTileBuffer),
}
}
// Map defines a Web Mercator map
type Map struct {
Name string
// Contains an attribution to be displayed when the map is shown to a user.
// This string is sanitized so it can't be abused as a vector for XSS or beacon tracking.
Attribution string
// The maximum extent of available map tiles in WGS:84
// latitude and longitude values, in the order left, bottom, right, top.
// Default: [-180, -85, 180, 85]
Bounds *geom.Extent
// The first value is the longitude, the second is latitude (both in
// WGS:84 values), the third value is the zoom level.
Center [3]float64
Layers []Layer
// Params holds configured query parameters
Params []provider.QueryParameter
SRID uint64
// MVT output values
TileExtent uint64
TileBuffer uint64
mvtProviderName string
mvtProvider provider.MVTTiler
observer observability.Interface
}
// HasMVTProvider indicates if map is a mvt provider based map
func (m Map) HasMVTProvider() bool { return m.mvtProvider != nil }
// MVTProvider returns the mvt provider if this map is a mvt provider based map, otherwise nil
func (m Map) MVTProvider() provider.MVTTiler { return m.mvtProvider }
// MVTProviderName returns the mvt provider name if this map is a mvt provider based map, otherwise ""
func (m Map) MVTProviderName() string { return m.mvtProviderName }
// SetMVTProvider sets the map to be based on the passed in mvt provider, and returning the provider
func (m *Map) SetMVTProvider(name string, p provider.MVTTiler) provider.MVTTiler {
m.mvtProviderName = name
m.mvtProvider = p
return p
}
func (m Map) Collectors(prefix string, config func(configKey string) map[string]interface{}) ([]observability.Collector, error) {
if m.mvtProviderName != "" {
collect, ok := m.mvtProvider.(observability.Observer)
if !ok {
return nil, nil
}
return collect.Collectors(prefix, config)
}
// not an mvtProvider, so need to ask each layer instead
var collection []observability.Collector
for i := range m.Layers {
aCollection, err := m.Layers[i].Collectors(prefix, config)
if err != nil {
return nil, err
}
if len(aCollection) != 0 {
collection = append(collection, aCollection...)
}
}
return collection, nil
}
// AddDebugLayers returns a copy of a Map with the debug layers appended to the layer list
func (m Map) AddDebugLayers() Map {
// can not modify the layers of an mvt provider based map
if m.mvtProvider != nil {
return m
}
// make an explicit copy of the layers
layers := make([]Layer, len(m.Layers))
copy(layers, m.Layers)
m.Layers = layers
// setup a debug provider
debugProvider, _ := debug.NewTileProvider(dict.Dict{}, nil)
m.Layers = append(layers, []Layer{
{
Name: debug.LayerDebugTileOutline,
ProviderLayerName: debug.LayerDebugTileOutline,
Provider: debugProvider,
GeomType: geom.LineString{},
MinZoom: 0,
MaxZoom: MaxZoom,
},
{
Name: debug.LayerDebugTileCenter,
ProviderLayerName: debug.LayerDebugTileCenter,
Provider: debugProvider,
GeomType: geom.Point{},
MinZoom: 0,
MaxZoom: MaxZoom,
},
}...)
return m
}
// FilterLayersByZoom returns a copy of a Map with a subset of layers that match the given zoom
func (m Map) FilterLayersByZoom(zoom uint) Map {
var layers []Layer
for i := range m.Layers {
if m.Layers[i].MinZoom <= zoom && m.Layers[i].MaxZoom >= zoom {
layers = append(layers, m.Layers[i])
continue
}
}
// overwrite the Map's layers with our subset
m.Layers = layers
return m
}
// FilterLayersByName returns a copy of a Map with a subset of layers that match the supplied list of layer names
func (m Map) FilterLayersByName(names ...string) Map {
var layers []Layer
nameStr := strings.Join(names, ",")
for i := range m.Layers {
// if we have a name set, use it for the lookup
if m.Layers[i].Name != "" && strings.Contains(nameStr, m.Layers[i].Name) {
layers = append(layers, m.Layers[i])
continue
} else if m.Layers[i].ProviderLayerName != "" && strings.Contains(nameStr, m.Layers[i].ProviderLayerName) { // default to using the ProviderLayerName for the lookup
layers = append(layers, m.Layers[i])
continue
}
}
// overwrite the Map's layers with our subset
m.Layers = layers
return m
}
func (m Map) encodeMVTProviderTile(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
// get the list of our layers
ptile := provider.NewTile(tile.Z, tile.X, tile.Y, uint(m.TileBuffer), uint(m.SRID))
layers := make([]provider.Layer, len(m.Layers))
for i := range m.Layers {
layers[i] = provider.Layer{
Name: m.Layers[i].ProviderLayerName,
MVTName: m.Layers[i].MVTName(),
}
}
return m.mvtProvider.MVTForLayers(ctx, ptile, params, layers)
}
// encodeMVTTile will encode the given tile into mvt format
// TODO (arolek): support for max zoom
func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
// tile container
var mvtTile mvt.Tile
// wait group for concurrent layer fetching
var wg sync.WaitGroup
// layer stack
mvtLayers := make([]*mvt.Layer, len(m.Layers))
// set our waitgroup count
wg.Add(len(m.Layers))
// iterate our layers
for i, layer := range m.Layers {
// go routine for fetching the layer concurrently
go func(i int, l Layer) {
mvtLayer := mvt.Layer{
Name: l.MVTName(),
}
// on completion let the wait group know
defer wg.Done()
ptile := provider.NewTile(tile.Z, tile.X, tile.Y,
uint(m.TileBuffer), uint(m.SRID))
// fetch layer from data provider
err := l.Provider.TileFeatures(ctx, l.ProviderLayerName, ptile, params, func(f *provider.Feature) error {
// skip row if geometry collection empty.
g, ok := f.Geometry.(geom.Collection)
if ok && len(g.Geometries()) == 0 {
return nil
}
geo := f.Geometry
// check if the feature SRID and map SRID are different. If they are then reporject
if f.SRID != m.SRID {
// TODO(arolek): support for additional projections
g, err := basic.ToWebMercator(f.SRID, geo)
if err != nil {
return fmt.Errorf("unable to transform geometry to webmercator from SRID (%v) for feature %v due to error: %w", f.SRID, f.ID, err)
}
geo = g
}
// TODO: remove this geom conversion step once the simplify function uses geom types
tegolaGeo, err := convert.ToTegola(geo)
if err != nil {
return err
}
// add default tags, but don't overwrite a tag that already exists
for k, v := range l.DefaultTags {
if _, ok := f.Tags[k]; !ok {
f.Tags[k] = v
}
}
// TODO (arolek): change out the tile type for VTile. tegola.Tile will be deprecated
tegolaTile := tegola.NewTile(tile.ZXY())
sg := tegolaGeo
// multiple ways to turn off simplification. check the atlas init() function
// for how the second two conditions are set
if !l.DontSimplify && simplifyGeometries && tile.Z < simplificationMaxZoom {
sg = simplify.SimplifyGeometry(tegolaGeo, tegolaTile.ZEpislon())
}
// check if we need to clip and if we do build the clip region (tile extent)
var clipRegion *geom.Extent
if !l.DontClip {
// CleanGeometry is expecting to operate in pixel coordinates so the clipRegion
// will need to be in this same coordinate system. this will change when the new
// make valid routing is implemented
pbb, err := tegolaTile.PixelBufferedBounds()
if err != nil {
return fmt.Errorf("err calculating tile pixel buffer bounds: %w", err)
}
clipRegion = geom.NewExtent([2]float64{pbb[0], pbb[1]}, [2]float64{pbb[2], pbb[3]})
}
// TODO: remove this geom conversion step once the simplify function uses geom types
geo, err = convert.ToGeom(sg)
if err != nil {
return err
}
// TODO(arolek): currently the validate.CleanGeometry method does not operate
// well on geometries that are not scaled to tile coordinate space. this will change
// with the adoption of the new make valid routine. once implemented, the clipRegion
// calculation will need to be in the same coordinate space as the geometry the
// make valid function will be operating on.
geo = mvt.PrepareGeo(geo, tile.Extent3857(), float64(mvt.DefaultExtent))
// TODO: remove this geom conversion step once the validate function uses geom types
sg, err = convert.ToTegola(geo)
if err != nil {
return err
}
if !l.DontClean {
tegolaGeo, err = validate.CleanGeometry(ctx, sg, clipRegion)
if err != nil {
return fmt.Errorf("err making geometry valid: %w", err)
}
} else {
tegolaGeo = sg
}
geo, err = convert.ToGeom(tegolaGeo)
if err != nil {
return nil
}
mvtLayer.AddFeatures(mvt.Feature{
ID: &f.ID,
Tags: f.Tags,
Geometry: geo,
})
return nil
})
if err != nil {
switch {
case errors.Is(err, context.Canceled):
// Do nothing if we were cancelled.
// the underlying net.Dial function is not properly reporting
// context.Canceled errors. Because of this, a string check on the error is performed.
// there's an open issue for this and it appears it will be fixed eventually
// but for now we have this check to avoid unnecessary logs
// https://github.com/golang/go/issues/36208
case strings.Contains(err.Error(), "operation was canceled"):
// Do nothing, context was canceled
default:
z, x, y := tile.ZXY()
// TODO (arolek): should we return an error to the response or just log the error?
// we can't just write to the response as the waitgroup is going to write to the response as well
log.Errorf("err fetching tile (z: %v, x: %v, y: %v) features: %v", z, x, y, err)
}
return
}
// add the layer to the slice position
mvtLayers[i] = &mvtLayer
}(i, layer)
}
// wait for the waitgroup to finish
wg.Wait()
// stop processing if the context has an error. this check is necessary
// otherwise the server continues processing even if the request was canceled
// as the waitgroup was not notified of the cancel
if ctx.Err() != nil {
return nil, ctx.Err()
}
// add layers to our tile
err := mvtTile.AddLayers(mvtLayers...)
if err != nil {
return nil, err
}
// generate the MVT tile
vtile, err := mvtTile.VTile(ctx)
if err != nil {
return nil, err
}
// encode our mvt tile
return proto.Marshal(vtile)
}
// Encode will encode the given tile into mvt format
func (m Map) Encode(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
var (
tileBytes []byte
err error
)
if m.HasMVTProvider() {
tileBytes, err = m.encodeMVTProviderTile(ctx, tile, params)
} else {
tileBytes, err = m.encodeMVTTile(ctx, tile, params)
}
if err != nil {
return nil, err
}
// buffer to store our compressed bytes
var gzipBuf bytes.Buffer
// compress the encoded bytes
w := gzip.NewWriter(&gzipBuf)
_, err = w.Write(tileBytes)
if err != nil {
return nil, err
}
// flush and close the writer
if err = w.Close(); err != nil {
return nil, err
}
// return encoded, gzipped tile
return gzipBuf.Bytes(), nil
}