-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilemap.go
711 lines (615 loc) · 18.6 KB
/
tilemap.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"html"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
"github.com/go-spatial/geom"
"github.com/go-spatial/geom/slippy"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/atlas"
"github.com/go-spatial/tegola/basic"
"github.com/go-spatial/tegola/config"
aprd "github.com/go-spatial/tegola/provider"
"github.com/go-spatial/tegola/provider/debug"
"github.com/jinzhu/gorm"
"github.com/go-spatial/geom/encoding/mvt"
proto "github.com/golang/protobuf/proto"
// "github.com/jackc/pgx"
// "github.com/jackc/pgx/pgtype"
_ "github.com/mattn/go-sqlite3" // import sqlite3 driver
log "github.com/sirupsen/logrus"
)
const (
bboxToken = "!BBOX!"
zoomToken = "!ZOOM!"
scaleDenominatorToken = "!SCALE_DENOMINATOR!"
pixelWidthToken = "!PIXEL_WIDTH!"
pixelHeightToken = "!PIXEL_HEIGHT!"
)
//TileBuffer 瓦片缓冲区大小
var TileBuffer = tegola.DefaultTileBuffer
var isSelectQuery = regexp.MustCompile(`(?i)^((\s*)(--.*\n)?)*select`)
//TileMap 瓦片数据集
type TileMap struct {
ID string `json:"id"`
Name string `json:"name"`
// Contains an attribution to be displayed when the map is shown to a user.
// This string is sanatized so it can't be abused as a vector for XSS or beacon tracking.
Attribution string `json:"attribution"`
// 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 `json:"center"`
Layers []TileLayer `json:"layers"`
SRID uint64
// MVT output values
TileExtent uint64
TileBuffer uint64
Format TileFormat
}
//TileLayer tile layer
type TileLayer struct {
ID string `json:"id" toml:"name"`
Name string `json:"name" toml:"name" binding:"required"`
MinZoom uint `json:"min_zoom"`
MaxZoom uint `json:"max_zoom"`
Bounds *geom.Extent `gorm:"-"`
Provider aprd.TilerUnion `gorm:"-"`
ProviderLayerID string `json:"provider_layer" toml:"provider_layer" binding:"required"`
GeomType geom.Geometry `gorm:"-"`
DefaultTags map[string]interface{} `json:"default_tags" gorm:"-"`
DontSimplify bool `json:"dont_simplify" toml:"dont_simplify" `
DontClip bool `json:"dont_clip" toml:"dont_clip" `
SRID uint64
}
//UpInsert 创建更新瓦片集服务
//create or update upload data file info into database
func (tl *TileLayer) UpInsert() error {
if tl == nil {
return fmt.Errorf("datafile may not be nil")
}
tmp := &TileLayer{}
err := db.Where("id = ?", tl.ID).First(tmp).Error
if err != nil {
if gorm.IsRecordNotFoundError(err) {
err = db.Create(tl).Error
if err != nil {
return err
}
}
return err
}
err = db.Model(&TileLayer{}).Update(tl).Error
if err != nil {
return err
}
return nil
}
// MVTName will return the value that will be encoded in the Name field when the layer is encoded as MVT
func (tl *TileLayer) MVTName() string {
if tl.Name != "" {
return tl.Name
}
return tl.ProviderLayerID
}
// FilterByZoom 过滤
func (tl *TileLayer) FilterByZoom(zoom uint) bool {
if (tl.MinZoom <= zoom || tl.MinZoom == 0) && (tl.MaxZoom >= zoom || tl.MaxZoom == 0) {
return false
}
return true
}
//MVTEncode encode for mvt_postgis
func (tl *TileLayer) MVTEncode(ctx context.Context, tile *slippy.Tile) ([]byte, error) {
if tl.Provider.Mvt == nil {
return nil, fmt.Errorf(".Mvt is null")
}
ptile := aprd.NewTile(tile.Z, tile.X, tile.Y,
uint(TileBuffer), uint(tl.SRID))
lry := aprd.Layer{
ID: tl.ID,
MVTName: tl.MVTName(),
}
layers := []aprd.Layer{lry}
data, err := tl.Provider.Mvt.MVTForLayers(ctx, ptile, layers)
if err != nil {
switch err {
case context.Canceled:
return nil, err
// TODO (arolek): add debug logs
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.Printf("err fetching tile (z: %v, x: %v, y: %v) features: %v", z, x, y, err)
if err.Error() != "too much features" {
return nil, err
}
}
}
// 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()
}
// buffer to store our compressed bytes
var gzipBuf bytes.Buffer
// compress the encoded bytes
w := gzip.NewWriter(&gzipBuf)
_, err = w.Write(data)
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
}
//Encode TODO (arolek): support for max zoom
func (tl *TileLayer) Encode(ctx context.Context, tile *slippy.Tile) ([]byte, error) {
if tl.Provider.Std == nil {
return nil, fmt.Errorf(".Std is null")
}
// tile container
var mvtTile mvt.Tile
// wait group for concurrent layer fetching
mvtLayer := mvt.Layer{
Name: tl.MVTName(),
}
ptile := aprd.NewTile(tile.Z, tile.X, tile.Y,
uint(TileBuffer), uint(tl.SRID))
// fetch layer from data provider
err := tl.Provider.Std.TileFeatures(ctx, tl.ProviderLayerID, ptile, func(f *aprd.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
if f.SRID != tl.SRID {
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
}
geo = mvt.PrepareGeo(geo, tile.Extent3857(), float64(mvt.DefaultExtent))
mvtLayer.AddFeatures(mvt.Feature{
ID: &f.ID,
Tags: f.Tags,
Geometry: geo,
})
return nil
})
if err != nil {
switch err {
case context.Canceled:
return nil, err
// TODO (arolek): add debug logs
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.Printf("err fetching tile (z: %v, x: %v, y: %v) features: %v", z, x, y, err)
if err.Error() != "too much features" {
return nil, err
}
}
}
// 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
mvtTile.AddLayers(&mvtLayer)
// generate our tile
vtile, err := mvtTile.VTile(ctx)
if err != nil {
return nil, err
}
// encode our mvt tile
tileBytes, err := proto.Marshal(vtile)
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
}
// TileFormat returns the TileFormat of the DB.
func (tm TileMap) TileFormat() TileFormat {
return tm.Format
}
//Tile 获取瓦片
func (tm TileMap) Tile(ctx context.Context, z uint8, x uint, y uint) ([]byte, error) {
tile := slippy.NewTile(uint(z), x, y)
// check for the debug query string
if true {
tm = tm.AddDebugLayers()
}
pbyte, err := tm.Encode(ctx, tile)
if err != nil {
switch err {
case context.Canceled:
// TODO: add debug logs
return nil, err
default:
errMsg := fmt.Sprintf("error marshalling tile: %v", err)
log.Error(errMsg)
return nil, err
}
}
return pbyte, nil
}
// AddDebugLayers returns a copy of a Map with the debug layers appended to the layer list
func (tm TileMap) AddDebugLayers() TileMap {
// make an explicit copy of the layers
layers := make([]TileLayer, len(tm.Layers))
copy(layers, tm.Layers)
tm.Layers = layers
// setup a debug provider
// debugProvider, _ := debug.NewTileProvider(dict.Dict{})
tm.Layers = append(layers, []TileLayer{
{
Name: debug.LayerDebugTileOutline,
ProviderLayerID: debug.LayerDebugTileOutline,
// Provider: debugProvider,
GeomType: geom.LineString{},
MinZoom: 0,
MaxZoom: 22,
},
{
Name: debug.LayerDebugTileCenter,
ProviderLayerID: debug.LayerDebugTileCenter,
// Provider: debugProvider,
GeomType: geom.Point{},
MinZoom: 0,
MaxZoom: 22,
},
}...)
return tm
}
// FilterLayersByZoom returns a copy of a Map with a subset of layers that match the given zoom
func (tm TileMap) FilterLayersByZoom(zoom uint) TileMap {
var layers []TileLayer
for i := range tm.Layers {
if (tm.Layers[i].MinZoom <= zoom || tm.Layers[i].MinZoom == 0) && (tm.Layers[i].MaxZoom >= zoom || tm.Layers[i].MaxZoom == 0) {
layers = append(layers, tm.Layers[i])
continue
}
}
// overwrite the Map's layers with our subset
tm.Layers = layers
return tm
}
// FilterLayersByName returns a copy of a Map with a subset of layers that match the supplied list of layer names
func (tm TileMap) FilterLayersByName(names ...string) TileMap {
var layers []TileLayer
nameStr := strings.Join(names, ",")
for i := range tm.Layers {
// if we have a name set, use it for the lookup
if tm.Layers[i].Name != "" && strings.Contains(nameStr, tm.Layers[i].Name) {
layers = append(layers, tm.Layers[i])
continue
} else if tm.Layers[i].ProviderLayerID != "" && strings.Contains(nameStr, tm.Layers[i].ProviderLayerID) { // default to using the ProviderLayerName for the lookup
layers = append(layers, tm.Layers[i])
continue
}
}
// overwrite the Map's layers with our subset
tm.Layers = layers
return tm
}
//Encode TODO (arolek): support for max zoom
func (tm TileMap) Encode(ctx context.Context, tile *slippy.Tile) ([]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(tm.Layers))
// set our waitgroup count
wg.Add(len(tm.Layers))
// iterate our layers
for i, layer := range tm.Layers {
// go routine for fetching the layer concurrently
go func(i int, l TileLayer) {
mvtLayer := mvt.Layer{
Name: l.MVTName(),
}
// on completion let the wait group know
defer wg.Done()
if l.Provider.Std == nil {
return
}
ptile := aprd.NewTile(tile.Z, tile.X, tile.Y,
uint(TileBuffer), uint(tm.SRID))
// fetch layer from data provider
err := l.Provider.Std.TileFeatures(ctx, l.ProviderLayerID, ptile, func(f *aprd.Feature) error {
// TODO: remove this geom conversion step once the mvt package has adopted the new geom package
geo, err := ToTegola(f.Geometry)
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
}
}
mvtLayer.AddFeatures(mvt.Feature{
ID: &f.ID,
Tags: f.Tags,
Geometry: geo,
})
return nil
})
if err != nil {
switch err {
case context.Canceled:
// TODO (arolek): add debug logs
return
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.Printf("err fetching tile (z: %v, x: %v, y: %v) features: %v", z, x, y, err)
if err.Error() != "too much features" {
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
mvtTile.AddLayers(mvtLayers...)
// generate our tile
vtile, err := mvtTile.VTile(ctx)
if err != nil {
return nil, err
}
// encode our mvt tile
tileBytes, err := proto.Marshal(vtile)
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
}
// LoadTileMap creates a new MBTiles instance.
// Co123456789nnection is closed by runtime on application termination or by calling
// its Close() method.
func LoadTileMap(pathfile string) (*TileMap, error) {
//Saves last modified mbtiles time for setting Last-Modified header
// fStat, err := os.Stat(pathfile)
// if err != nil {
// return nil, fmt.Errorf("could not read file stats for mbtiles file: %s", pathfile)
// }
// check the conf file exists
if _, err := os.Stat(pathfile); os.IsNotExist(err) {
return nil, fmt.Errorf("config file %v not found", pathfile)
}
buf, err := ioutil.ReadFile(pathfile)
if err != nil {
return nil, err
}
out := &TileMap{}
json.Unmarshal(buf, &out)
for i := range out.Layers {
out.Layers[i].Provider = providers["pg"]
}
return out, nil
}
//SaveTileMap 保存配置文件
func SaveTileMap(pathfile string) error {
var layers []TileLayer
layer := TileLayer{
Name: "places_a",
ProviderLayerID: "osm_places_a",
MinZoom: 5,
MaxZoom: 20,
}
layers = append(layers, layer)
out := &TileMap{
Name: pathfile,
Layers: layers,
}
buf, err := json.Marshal(out)
err = ioutil.WriteFile(pathfile, buf, os.ModePerm)
if err != nil {
return err
}
return nil
}
// TileFormat returns the TileFormat of the DB.
func (tm TileMap) toAtlasMap() atlas.Map {
newMap := atlas.NewWebMercatorMap(string(tm.Name))
newMap.Attribution = html.EscapeString(string(tm.Attribution))
// convert from env package
for i, v := range tm.Center {
newMap.Center[i] = float64(v)
}
if len(tm.Bounds) == 4 {
newMap.Bounds = geom.NewExtent(
[2]float64{float64(tm.Bounds[0]), float64(tm.Bounds[1])},
[2]float64{float64(tm.Bounds[2]), float64(tm.Bounds[3])},
)
}
if tm.TileBuffer != 0 {
newMap.TileBuffer = uint64(tm.TileBuffer)
}
return newMap
}
//*******************************************************************
// registerMaps registers maps with with atlas
func registerMaps(a *atlas.Atlas, maps []config.Map, providers map[string]aprd.TilerUnion) error {
var (
layerer aprd.Layerer
)
// iterate our maps
for _, m := range maps {
newMap := webMercatorMapFromConfigMap(m)
// iterate our layers
for _, l := range m.Layers {
prdID, plyrID, err := l.ProviderLayerID()
if err != nil {
return fmt.Errorf("ErrProviderLayerInvalid,ProviderLayer:%s,Map:%s", string(l.ProviderLayer), string(m.Name))
}
// find our layer provider
layerer, err = selectProvider(prdID, string(m.Name), &newMap, providers)
if err != nil {
return err
}
layer, err := atlasLayerFromConfigLayer(&l, plyrID, layerer)
if err != nil {
return err
}
newMap.Layers = append(newMap.Layers, layer)
}
a.AddMap(newMap)
}
return nil
}
func webMercatorMapFromConfigMap(cfg config.Map) (newMap atlas.Map) {
newMap = atlas.NewWebMercatorMap(string(cfg.Name))
newMap.Attribution = html.EscapeString(string(cfg.Attribution))
// convert from env package
for i, v := range cfg.Center {
newMap.Center[i] = float64(v)
}
if len(cfg.Bounds) == 4 {
newMap.Bounds = geom.NewExtent(
[2]float64{float64(cfg.Bounds[0]), float64(cfg.Bounds[1])},
[2]float64{float64(cfg.Bounds[2]), float64(cfg.Bounds[3])},
)
}
if cfg.TileBuffer != nil {
newMap.TileBuffer = uint64(*cfg.TileBuffer)
}
return newMap
}
func layerInfosFindByID(infos []aprd.LayerInfo, lyrID string) aprd.LayerInfo {
if len(infos) == 0 {
return nil
}
for i := range infos {
if infos[i].ID() == lyrID {
return infos[i]
}
}
return nil
}
func atlasLayerFromConfigLayer(cfg *config.MapLayer, mapName string, layerProvider aprd.Layerer) (layer atlas.Layer, err error) {
var (
// providerLayer is primary used for error reporting.
providerLayer = string(cfg.ProviderLayer)
ok bool
)
// read the provider's layer names
// don't care about the error.
prdID, plyrID, _ := cfg.ProviderLayerID()
layerInfos, err := layerProvider.Layers()
if err != nil {
return layer, fmt.Errorf("ErrFetchingLayerInfo,Provider:%s,error:%s",
string(prdID), err.Error())
}
layerInfo := layerInfosFindByID(layerInfos, plyrID)
if layerInfo == nil {
return layer, fmt.Errorf("ErrProviderLayerNotRegistered,Map:%s,Provider:%s,ProviderLayer:%s",
mapName, prdID, providerLayer)
}
layer.GeomType = layerInfo.GeomType()
if cfg.DefaultTags != nil {
if layer.DefaultTags, ok = cfg.DefaultTags.(map[string]interface{}); !ok {
return layer, fmt.Errorf("ErrDefaultTagsInvalid,ProviderLayer:%s",
providerLayer)
}
}
// if layerProvider is not a provider.Tiler this will return nil, so
// no need to check ok, as nil is what we want here.
layer.Provider, _ = layerProvider.(aprd.Tiler)
layer.ID = string(cfg.ID)
layer.Name = string(cfg.Name)
layer.ProviderLayerID = plyrID
layer.DontSimplify = bool(cfg.DontSimplify)
layer.DontClip = bool(cfg.DontClip)
if cfg.MinZoom != nil {
layer.MinZoom = uint(*cfg.MinZoom)
}
if cfg.MaxZoom != nil {
layer.MaxZoom = uint(*cfg.MaxZoom)
}
return layer, nil
}
func selectProvider(prdID string, mapName string, newMap *atlas.Map, providers map[string]aprd.TilerUnion) (aprd.Layerer, error) {
if newMap.HasMVTProvider() {
if newMap.MVTProviderID() != prdID {
return nil, fmt.Errorf("ErrMVTDifferentProviders,Original:%s,Current:%s",
newMap.MVTProviderID(), prdID)
}
return newMap.MVTProvider(), nil
}
if prvd, ok := providers[prdID]; ok {
// Need to see what type of provider we got.
if prvd.Std != nil {
return prvd.Std, nil
}
if prvd.Mvt == nil {
return nil, fmt.Errorf("ErrProviderNotFound, %s", prdID)
}
if len(newMap.Layers) != 0 {
return nil, fmt.Errorf("ErrMixedProviders, Map:%s", string(mapName))
}
return newMap.SetMVTProvider(prdID, prvd.Mvt), nil
}
return nil, fmt.Errorf("ErrProviderNotFound, %s", prdID)
}