-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
567 lines (382 loc) · 13.8 KB
/
database.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
package pmtiles
import ()
import (
_ "github.com/aaronland/gocloud-blob-s3"
_ "github.com/whosonfirst/go-whosonfirst-spatial-sqlite"
_ "gocloud.dev/blob/fileblob"
_ "gocloud.dev/docstore/awsdynamodb"
_ "gocloud.dev/docstore/memdocstore"
)
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"strconv"
"strings"
"sync"
aa_log "github.com/aaronland/go-log/v2"
aa_docstore "github.com/aaronland/gocloud-docstore"
"github.com/jtacoma/uritemplates"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/mvt"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/maptile"
"github.com/protomaps/go-pmtiles/pmtiles"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/whosonfirst/go-ioutil"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-spatial"
"github.com/whosonfirst/go-whosonfirst-spatial/database"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-uri"
"gocloud.dev/docstore"
)
func init() {
ctx := context.Background()
database.RegisterSpatialDatabase(ctx, "pmtiles", NewPMTilesSpatialDatabase)
reader.RegisterReader(ctx, "pmtiles", NewPMTilesSpatialDatabaseReader)
}
type PMTilesSpatialDatabase struct {
database.SpatialDatabase
server *pmtiles.Server
logger *log.Logger
database string
layer string
enable_feature_cache bool
cache_manager *CacheManager
zoom int
}
func NewPMTilesSpatialDatabaseReader(ctx context.Context, uri string) (reader.Reader, error) {
return NewPMTilesSpatialDatabase(ctx, uri)
}
func NewPMTilesSpatialDatabase(ctx context.Context, uri string) (database.SpatialDatabase, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}
q := u.Query()
q_tile_path := q.Get("tiles")
q_database := q.Get("database")
q_layer := q.Get("layer")
if q_layer == "" {
q_layer = q_database
}
logger := log.Default()
cache_size := 64
zoom := 12
q_cache_size := q.Get("pmtiles-cache-size")
if q_cache_size != "" {
sz, err := strconv.Atoi(q_cache_size)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?pmtiles-cache-size= parameter, %w", err)
}
cache_size = sz
}
q_zoom := q.Get("zoom")
if q_zoom != "" {
z, err := strconv.Atoi(q_zoom)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?zoom= parameter, %w", err)
}
zoom = z
}
server, err := pmtiles.NewServer(q_tile_path, "", logger, cache_size, "", "")
if err != nil {
return nil, fmt.Errorf("Failed to create pmtiles.Loop, %w", err)
}
server.Start()
db := &PMTilesSpatialDatabase{
server: server,
database: q_database,
layer: q_layer,
logger: logger,
zoom: zoom,
}
enable_feature_cache := false
q_enable_cache := q.Get("enable-cache")
if q_enable_cache != "" {
enabled, err := strconv.ParseBool(q_enable_cache)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?enable-cache= parameter, %w", err)
}
enable_feature_cache = enabled
}
if enable_feature_cache {
cache_ttl := 300
q_cache_ttl := q.Get("cache-ttl")
if q_cache_ttl != "" {
ttl, err := strconv.Atoi(q_cache_ttl)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?cache-ttl= parameter, %w", err)
}
if ttl < 0 {
return nil, fmt.Errorf("Invalid cache-ttl value")
}
cache_ttl = ttl
}
feature_cache_uri_t := fmt.Sprintf("mem://%s/{key}", FEATURES_CACHE_TABLE)
q_feature_cache_uri_t := q.Get("feature-cache-uri")
if q_feature_cache_uri_t != "" {
feature_cache_uri_t = q_feature_cache_uri_t
}
feature_cache_key := "Id"
feature_cache_v := map[string]interface{}{
"key": feature_cache_key,
}
feature_cache, err := openCollection(ctx, feature_cache_uri_t, feature_cache_v)
if err != nil {
return nil, fmt.Errorf("could not open feature cache collection: %w", err)
}
cache_manager_opts := &CacheManagerOptions{
FeatureCollection: feature_cache,
Logger: logger,
CacheTTL: cache_ttl,
}
cache_manager := NewCacheManager(ctx, cache_manager_opts)
db.cache_manager = cache_manager
db.enable_feature_cache = enable_feature_cache
}
return db, nil
}
func (db *PMTilesSpatialDatabase) IndexFeature(context.Context, []byte) error {
return fmt.Errorf("Not implemented.")
}
func (db *PMTilesSpatialDatabase) RemoveFeature(context.Context, string) error {
return fmt.Errorf("Not implemented.")
}
func (db *PMTilesSpatialDatabase) PointInPolygon(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) (spr.StandardPlacesResults, error) {
/*
$> ./bin/server -tile-path file:///usr/local/whosonfirst/go-whosonfirst-tippecanoe -enable-example -example-database wof
2022/11/24 14:41:32 Listening for requests on http://localhost:8080
2022/11/24 14:41:48 fetching wof 0-16384
2022/11/24 14:41:48 fetched wof 0-0
2022/11/24 14:41:48 fetching wof 39541-13802
2022/11/24 14:41:48 fetched wof 39541-13802
2022/11/24 14:41:48 [200] served /wof/8/41/98.mvt in 3.485603ms
> go run cmd/query/main.go -spatial-database-uri 'pmtiles://?tiles=file:///usr/local/whosonfirst/go-whosonfirst-tippecanoe&database=wof'
2022/11/25 18:33:32 fetching wof 0-16384
2022/11/25 18:33:32 fetched wof 0-0
2022/11/25 18:33:32 fetching wof 39541-13802
2022/11/25 18:33:32 fetched wof 39541-13802
map[wof:0xc0001005a0]
*/
spatial_db, err := db.spatialDatabaseFromCoord(ctx, coord)
if err != nil {
return nil, fmt.Errorf("Failed to create spatial database, %w", err)
}
defer spatial_db.Disconnect(ctx)
return spatial_db.PointInPolygon(ctx, coord, filters...)
}
func (db *PMTilesSpatialDatabase) PointInPolygonCandidates(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) ([]*spatial.PointInPolygonCandidate, error) {
spatial_db, err := db.spatialDatabaseFromCoord(ctx, coord)
if err != nil {
return nil, fmt.Errorf("Failed to create spatial database, %w", err)
}
defer spatial_db.Disconnect(ctx)
return spatial_db.PointInPolygonCandidates(ctx, coord, filters...)
}
func (db *PMTilesSpatialDatabase) PointInPolygonWithChannels(ctx context.Context, spr_ch chan spr.StandardPlacesResult, err_ch chan error, done_ch chan bool, coord *orb.Point, filters ...spatial.Filter) {
spatial_db, err := db.spatialDatabaseFromCoord(ctx, coord)
if err != nil {
err_ch <- fmt.Errorf("Failed to create spatial database, %w", err)
return
}
defer spatial_db.Disconnect(ctx)
spatial_db.PointInPolygonWithChannels(ctx, spr_ch, err_ch, done_ch, coord, filters...)
}
func (db *PMTilesSpatialDatabase) PointInPolygonCandidatesWithChannels(ctx context.Context, pip_ch chan *spatial.PointInPolygonCandidate, err_ch chan error, done_ch chan bool, coord *orb.Point, filters ...spatial.Filter) {
spatial_db, err := db.spatialDatabaseFromCoord(ctx, coord)
if err != nil {
err_ch <- fmt.Errorf("Failed to create spatial database, %w", err)
return
}
defer spatial_db.Disconnect(ctx)
spatial_db.PointInPolygonCandidatesWithChannels(ctx, pip_ch, err_ch, done_ch, coord, filters...)
}
func (db *PMTilesSpatialDatabase) Disconnect(ctx context.Context) error {
db.cache_manager.Close(ctx)
return nil
}
// Read implements the whosonfirst/go-reader.Reader interface
func (db *PMTilesSpatialDatabase) Read(ctx context.Context, path string) (io.ReadSeekCloser, error) {
if !db.enable_feature_cache {
return nil, fmt.Errorf("Not found")
}
id, uri_args, err := uri.ParseURI(path)
if err != nil {
return nil, fmt.Errorf("Failed to path %s, %w", path, err)
}
fname, err := uri.Id2Fname(id, uri_args)
if err != nil {
return nil, fmt.Errorf("Failed to derive filename from %s, %w", path, err)
}
fname = strings.Replace(fname, ".geojson", "", 1)
fc, err := db.cache_manager.GetFeatureCache(ctx, fname)
if err != nil {
return nil, fmt.Errorf("Failed to get feature cache for %s, %w", path, err)
}
r := strings.NewReader(fc.Body)
rsc, err := ioutil.NewReadSeekCloser(r)
if err != nil {
return nil, fmt.Errorf("Failed to create ReadSeekCloser for %s, %w", path, err)
}
return rsc, nil
}
func (db *PMTilesSpatialDatabase) ReaderURI(ctx context.Context, path string) string {
return path
}
func (db *PMTilesSpatialDatabase) spatialDatabaseFromTile(ctx context.Context, t maptile.Tile) (database.SpatialDatabase, error) {
path := fmt.Sprintf("/%s/%d/%d/%d.mvt", db.database, t.Z, t.X, t.Y)
aa_log.Debug(db.logger, "Get tile at %s", path)
features, err := db.featuresForTile(ctx, t)
if err != nil {
return nil, fmt.Errorf("Failed to derive features for tile %s, %w", path, err)
}
spatial_db_uri := "sqlite://?dsn=modernc://mem"
spatial_db, err := database.NewSpatialDatabase(ctx, spatial_db_uri)
if err != nil {
return nil, fmt.Errorf("Failed to create spatial database for '%s', %w", spatial_db_uri, err)
}
seen := make(map[string]bool)
wg := new(sync.WaitGroup)
for idx, f := range features {
// START OF to remove once we've finished pruning layer data in featuresForTile
str_id := fmt.Sprintf("%v", f.ID)
if str_id != "" {
_, ok := seen[str_id]
if ok {
continue
}
}
seen[str_id] = true
// END OF to remove once we've finished pruning layer data in featuresForTile
body, err := f.MarshalJSON()
id_rsp := gjson.GetBytes(body, "properties.wof:id")
id := id_rsp.Int()
if err != nil {
return nil, fmt.Errorf("Failed to marshal JSON for feature %d at offset %d, %w", id, idx, err)
}
// START OF to remove once we've finished pruning layer data in featuresForTile
str_id = id_rsp.String()
_, ok := seen[str_id]
if ok {
continue
}
seen[str_id] = true
// END OF to remove once we've finished pruning layer data in featuresForTile
body, err = db.decodeMVT(ctx, body)
if err != nil {
return nil, fmt.Errorf("Failed to unfurl MVT for feature %d at offset %d, %w", id, idx, err)
}
if db.enable_feature_cache {
wg.Add(1)
go func(body []byte) {
defer wg.Done()
// TBD: Append/pass path to cache key here?
_, err := db.cache_manager.CacheFeature(ctx, body)
if err != nil {
aa_log.Warning(db.logger, "Failed to create new feature cache for %s, %v", path, err)
}
}(body)
}
err = spatial_db.IndexFeature(ctx, body)
if err != nil {
return nil, fmt.Errorf("Failed to index feature %d at offset %d, %w", id, idx, err)
}
}
wg.Wait()
return spatial_db, nil
}
func (db *PMTilesSpatialDatabase) spatialDatabaseFromCoord(ctx context.Context, coord *orb.Point) (database.SpatialDatabase, error) {
zoom := uint32(db.zoom)
z := maptile.Zoom(zoom)
t := maptile.At(*coord, z)
return db.spatialDatabaseFromTile(ctx, t)
}
func (db *PMTilesSpatialDatabase) featuresForTile(ctx context.Context, t maptile.Tile) ([]*geojson.Feature, error) {
path := fmt.Sprintf("/%s/%d/%d/%d.mvt", db.database, t.Z, t.X, t.Y)
// It's tempting to cache body (or the resultant FeatureCollection) here. Ancedotally
// at zoom level 12 it's very easy to blow past the 400kb size limit for items in DynamoDB.
// So, in an AWS context, we could write tile caches to a gocloud.dev/blob instance but
// will that read really be faster than reading from the PMTiles database also in S3? Maybe?
status_code, _, body := db.server.Get(ctx, path)
if status_code != 200 {
return nil, fmt.Errorf("Failed to get %s, unexpected status code %d", path, status_code)
}
// not sure what the semantics are here but it's not treated as an error in protomaps
// https://github.com/protomaps/go-pmtiles/blob/0ac8f97530b3367142cfd250585d60936d0ce643/pmtiles/loop.go#L296
var features []*geojson.Feature
if status_code == 204 {
features = make([]*geojson.Feature, 0)
} else {
layers, err := mvt.UnmarshalGzipped(body)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal tile, %w", err)
}
// Prune layers here
layers.ProjectToWGS84(t)
fc := layers.ToFeatureCollections()
_, exists := fc[db.layer]
if !exists {
return nil, fmt.Errorf("Missing %s layer", db.layer)
}
features = fc[db.layer].Features
}
return features, nil
}
// Expand WOF values that were stringified in the process of encoding them as MVT. Customs decoders are not yet supported.
// https://docs.mapbox.com/data/tilesets/guides/vector-tiles-standards/#how-to-encode-attributes-that-arent-strings-or-numbers
func (db *PMTilesSpatialDatabase) decodeMVT(ctx context.Context, body []byte) ([]byte, error) {
props := gjson.GetBytes(body, "properties")
if !props.Exists() {
return body, nil
}
for k, v := range props.Map() {
switch k {
case "wof:superseded_by", "wof:supersedes", "wof:belongsto":
var values []int64
err := json.Unmarshal([]byte(v.String()), &values)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal %s value (%s), %w", k, v.String(), err)
}
path := fmt.Sprintf("properties.%s", k)
body, err = sjson.SetBytes(body, path, values)
if err != nil {
return nil, fmt.Errorf("Failed to set %s, %w", path, err)
}
case "wof:hierarchy":
var values []map[string]int64
err := json.Unmarshal([]byte(v.String()), &values)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal %s value (%s), %w", k, v.String(), err)
}
path := fmt.Sprintf("properties.%s", k)
body, err = sjson.SetBytes(body, path, values)
if err != nil {
return nil, fmt.Errorf("Failed to set %s, %w", path, err)
}
default:
// pass
}
}
return body, nil
}
func openCollection(ctx context.Context, uri_t string, values map[string]interface{}) (*docstore.Collection, error) {
t, err := uritemplates.Parse(uri_t)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI template, %w", err)
}
col_uri, err := t.Expand(values)
if err != nil {
return nil, fmt.Errorf("Failed to expand URI template values, %w", err)
}
col, err := aa_docstore.OpenCollection(ctx, col_uri)
if err != nil {
return nil, fmt.Errorf("Failed to open collection, %w", err)
}
return col, nil
}