This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
673 lines (474 loc) · 13.5 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
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
package rtree
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/dhconnelly/rtreego"
gocache "github.com/patrickmn/go-cache"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
"github.com/whosonfirst/go-ioutil"
"github.com/whosonfirst/go-whosonfirst-feature/alt"
"github.com/whosonfirst/go-whosonfirst-feature/geometry"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-spatial"
"github.com/whosonfirst/go-whosonfirst-spatial/database"
"github.com/whosonfirst/go-whosonfirst-spatial/filter"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-uri"
)
func init() {
ctx := context.Background()
database.RegisterSpatialDatabase(ctx, "rtree", NewRTreeSpatialDatabase)
}
type RTreeCache struct {
Geometry *geojson.Geometry `json:"geometry"`
SPR spr.StandardPlacesResult `json:"properties"`
}
// PLEASE DISCUSS WHY patrickm/go-cache AND NOT whosonfirst/go-cache HERE
type RTreeSpatialDatabase struct {
database.SpatialDatabase
index_alt_files bool
rtree *rtreego.Rtree
gocache *gocache.Cache
mu *sync.RWMutex
strict bool
}
type RTreeSpatialIndex struct {
Rect *rtreego.Rect
Id string
FeatureId string
IsAlt bool
AltLabel string
}
func (i *RTreeSpatialIndex) Bounds() rtreego.Rect {
return *i.Rect
}
type RTreeResults struct {
spr.StandardPlacesResults `json:",omitempty"`
Places []spr.StandardPlacesResult `json:"places"`
}
func (r *RTreeResults) Results() []spr.StandardPlacesResult {
return r.Places
}
func NewRTreeSpatialDatabase(ctx context.Context, uri string) (database.SpatialDatabase, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
q := u.Query()
strict := true
if q.Get("strict") == "false" {
strict = false
}
expires := 0 * time.Second
cleanup := 0 * time.Second
str_exp := q.Get("default_expiration")
str_cleanup := q.Get("cleanup_interval")
if str_exp != "" {
int_expires, err := strconv.Atoi(str_exp)
if err != nil {
return nil, err
}
expires = time.Duration(int_expires) * time.Second
}
if str_cleanup != "" {
int_cleanup, err := strconv.Atoi(str_cleanup)
if err != nil {
return nil, err
}
cleanup = time.Duration(int_cleanup) * time.Second
}
index_alt_files := false
str_index_alt := q.Get("index_alt_files")
if str_index_alt != "" {
index_alt, err := strconv.ParseBool(str_index_alt)
if err != nil {
return nil, err
}
index_alt_files = index_alt
}
gc := gocache.New(expires, cleanup)
rtree := rtreego.NewTree(2, 25, 50)
mu := new(sync.RWMutex)
db := &RTreeSpatialDatabase{
rtree: rtree,
index_alt_files: index_alt_files,
gocache: gc,
strict: strict,
mu: mu,
}
return db, nil
}
func (r *RTreeSpatialDatabase) Disconnect(ctx context.Context) error {
return nil
}
func (r *RTreeSpatialDatabase) IndexFeature(ctx context.Context, body []byte) error {
is_alt := alt.IsAlt(body)
alt_label, _ := properties.AltLabel(body)
if is_alt && !r.index_alt_files {
return nil
}
if is_alt && alt_label == "" {
return fmt.Errorf("Invalid alt label")
}
err := r.setCache(ctx, body)
if err != nil {
return fmt.Errorf("Failed to cache feature, %w", err)
}
feature_id, err := properties.Id(body)
if err != nil {
return fmt.Errorf("Failed to derive ID, %w", err)
}
str_id := strconv.FormatInt(feature_id, 10)
// START OF put me in go-whosonfirst-feature/geometry
geojson_geom, err := geometry.Geometry(body)
if err != nil {
return fmt.Errorf("Failed to derive geometry, %w", err)
}
orb_geom := geojson_geom.Geometry()
bounds := make([]orb.Bound, 0)
switch orb_geom.GeoJSONType() {
case "MultiPolygon":
for _, poly := range orb_geom.(orb.MultiPolygon) {
for _, ring := range poly {
bounds = append(bounds, ring.Bound())
}
}
case "Polygon":
for _, ring := range orb_geom.(orb.Polygon) {
bounds = append(bounds, ring.Bound())
}
default:
bounds = append(bounds, orb_geom.Bound())
}
// END OF put me in go-whosonfirst-feature/geometry
for i, bbox := range bounds {
sp_id, err := spatial.SpatialIdWithFeature(body, i)
if err != nil {
return fmt.Errorf("Failed to derive spatial ID, %v", err)
}
min := bbox.Min
max := bbox.Max
min_x := min[0]
min_y := min[1]
max_x := max[0]
max_y := max[1]
llat := max_y - min_y
llon := max_x - min_x
pt := rtreego.Point{min_x, min_y}
rect, err := rtreego.NewRect(pt, []float64{llon, llat})
if err != nil {
if r.strict {
return fmt.Errorf("Failed to derive rtree bounds, %w", err)
}
slog.Error("Failed to index feature", "id", sp_id, "error", err)
return nil
}
sp := &RTreeSpatialIndex{
Rect: &rect,
Id: sp_id,
FeatureId: str_id,
IsAlt: is_alt,
AltLabel: alt_label,
}
r.mu.Lock()
r.rtree.Insert(sp)
r.mu.Unlock()
}
return nil
}
/*
TO DO: figure out suitable comparitor
/ DeleteWithComparator removes an object from the tree using a custom
// comparator for evaluating equalness. This is useful when you want to remove
// an object from a tree but don't have a pointer to the original object
// anymore.
func (tree *Rtree) DeleteWithComparator(obj Spatial, cmp Comparator) bool {
n := tree.findLeaf(tree.root, obj, cmp)
// Comparator compares two spatials and returns whether they are equal.
type Comparator func(obj1, obj2 Spatial) (equal bool)
func defaultComparator(obj1, obj2 Spatial) bool {
return obj1 == obj2
}
*/
func (r *RTreeSpatialDatabase) RemoveFeature(ctx context.Context, id string) error {
obj := &RTreeSpatialIndex{
Rect: nil,
Id: id,
}
comparator := func(obj1, obj2 rtreego.Spatial) bool {
// 2021/10/12 11:17:11 COMPARE 1: '101737491#:0' 2: '101737491'
// log.Printf("COMPARE 1: '%v' 2: '%v'\n", obj1.(*RTreeSpatialIndex).Id, obj2.(*RTreeSpatialIndex).Id)
obj1_id := obj1.(*RTreeSpatialIndex).Id
obj2_id := obj2.(*RTreeSpatialIndex).Id
return strings.HasPrefix(obj1_id, obj2_id)
}
ok := r.rtree.DeleteWithComparator(obj, comparator)
if !ok {
return fmt.Errorf("Failed to remove %s from rtree", id)
}
return nil
}
func (r *RTreeSpatialDatabase) PointInPolygon(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) (spr.StandardPlacesResults, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
rsp_ch := make(chan spr.StandardPlacesResult)
err_ch := make(chan error)
done_ch := make(chan bool)
results := make([]spr.StandardPlacesResult, 0)
working := true
go r.PointInPolygonWithChannels(ctx, rsp_ch, err_ch, done_ch, coord, filters...)
for {
select {
case <-ctx.Done():
return nil, nil
case <-done_ch:
working = false
case rsp := <-rsp_ch:
results = append(results, rsp)
case err := <-err_ch:
return nil, err
}
if !working {
break
}
}
spr_results := &RTreeResults{
Places: results,
}
return spr_results, nil
}
func (r *RTreeSpatialDatabase) PointInPolygonWithChannels(ctx context.Context, rsp_ch chan spr.StandardPlacesResult, err_ch chan error, done_ch chan bool, coord *orb.Point, filters ...spatial.Filter) {
defer func() {
done_ch <- true
}()
rows, err := r.getIntersectsByCoord(coord)
if err != nil {
err_ch <- err
return
}
r.inflateResultsWithChannels(ctx, rsp_ch, err_ch, rows, coord, filters...)
return
}
func (r *RTreeSpatialDatabase) PointInPolygonCandidates(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) ([]*spatial.PointInPolygonCandidate, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
rsp_ch := make(chan *spatial.PointInPolygonCandidate)
err_ch := make(chan error)
done_ch := make(chan bool)
candidates := make([]*spatial.PointInPolygonCandidate, 0)
working := true
go r.PointInPolygonCandidatesWithChannels(ctx, rsp_ch, err_ch, done_ch, coord, filters...)
for {
select {
case <-ctx.Done():
return nil, nil
case <-done_ch:
working = false
case rsp := <-rsp_ch:
candidates = append(candidates, rsp)
case err := <-err_ch:
return nil, err
}
if !working {
break
}
}
return candidates, nil
}
func (r *RTreeSpatialDatabase) PointInPolygonCandidatesWithChannels(ctx context.Context, rsp_ch chan *spatial.PointInPolygonCandidate, err_ch chan error, done_ch chan bool, coord *orb.Point, filters ...spatial.Filter) {
defer func() {
done_ch <- true
}()
intersects, err := r.getIntersectsByCoord(coord)
if err != nil {
err_ch <- err
return
}
for _, raw := range intersects {
sp := raw.(*RTreeSpatialIndex)
// bounds := sp.Bounds()
c := &spatial.PointInPolygonCandidate{
Id: sp.Id,
FeatureId: sp.FeatureId,
AltLabel: sp.AltLabel,
// FIX ME
// Bounds: bounds,
}
rsp_ch <- c
}
return
}
func (r *RTreeSpatialDatabase) getIntersectsByCoord(coord *orb.Point) ([]rtreego.Spatial, error) {
lat := coord.Y()
lon := coord.X()
pt := rtreego.Point{lon, lat}
rect, err := rtreego.NewRect(pt, []float64{0.0001, 0.0001}) // how small can I make this?
if err != nil {
return nil, fmt.Errorf("Failed to derive rtree bounds, %w", err)
}
return r.getIntersectsByRect(&rect)
}
func (r *RTreeSpatialDatabase) getIntersectsByRect(rect *rtreego.Rect) ([]rtreego.Spatial, error) {
results := r.rtree.SearchIntersect(*rect)
return results, nil
}
func (r *RTreeSpatialDatabase) inflateResultsWithChannels(ctx context.Context, rsp_ch chan spr.StandardPlacesResult, err_ch chan error, possible []rtreego.Spatial, c *orb.Point, filters ...spatial.Filter) {
seen := make(map[string]bool)
mu := new(sync.RWMutex)
wg := new(sync.WaitGroup)
for _, row := range possible {
sp := row.(*RTreeSpatialIndex)
wg.Add(1)
go func(sp *RTreeSpatialIndex) {
sp_id := sp.Id
feature_id := sp.FeatureId
defer wg.Done()
select {
case <-ctx.Done():
return
default:
// pass
}
mu.RLock()
_, ok := seen[feature_id]
mu.RUnlock()
if ok {
return
}
mu.Lock()
seen[feature_id] = true
mu.Unlock()
cache_item, err := r.retrieveCache(ctx, sp)
if err != nil {
slog.Error("Failed to retrieve cache item", "id", sp_id, "error", err)
return
}
s := cache_item.SPR
for _, f := range filters {
err = filter.FilterSPR(f, s)
if err != nil {
return
}
}
geom := cache_item.Geometry
orb_geom := geom.Geometry()
geom_type := orb_geom.GeoJSONType()
contains := false
switch geom_type {
case "Polygon":
contains = planar.PolygonContains(orb_geom.(orb.Polygon), *c)
case "MultiPolygon":
contains = planar.MultiPolygonContains(orb_geom.(orb.MultiPolygon), *c)
default:
slog.Debug("Geometry has unsupported geometry", "type", geom.Type)
}
if !contains {
return
}
rsp_ch <- s
}(sp)
}
wg.Wait()
}
func (r *RTreeSpatialDatabase) setCache(ctx context.Context, body []byte) error {
s, err := spr.WhosOnFirstSPR(body)
if err != nil {
return err
}
geom, err := geometry.Geometry(body)
if err != nil {
return fmt.Errorf("Failed to derive geometry for feature, %w", err)
}
alt_label, err := properties.AltLabel(body)
if err != nil {
return fmt.Errorf("Failed to derive alt label, %w", err)
}
feature_id, err := properties.Id(body)
if err != nil {
return fmt.Errorf("Failed to derive feature ID, %w", err)
}
cache_key := fmt.Sprintf("%d:%s", feature_id, alt_label)
cache_item := &RTreeCache{
Geometry: geom,
SPR: s,
}
r.gocache.Set(cache_key, cache_item, -1)
return nil
}
func (r *RTreeSpatialDatabase) retrieveCache(ctx context.Context, sp *RTreeSpatialIndex) (*RTreeCache, error) {
cache_key := fmt.Sprintf("%s:%s", sp.FeatureId, sp.AltLabel)
cache_item, ok := r.gocache.Get(cache_key)
if !ok {
return nil, fmt.Errorf("Invalid cache ID '%s'", cache_key)
}
return cache_item.(*RTreeCache), nil
}
// whosonfirst/go-reader interface
func (r *RTreeSpatialDatabase) Read(ctx context.Context, str_uri string) (io.ReadSeekCloser, error) {
id, _, err := uri.ParseURI(str_uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI %s, %w", str_uri, err)
}
// TO DO : ALT STUFF HERE
str_id := strconv.FormatInt(id, 10)
sp := &RTreeSpatialIndex{
FeatureId: str_id,
AltLabel: "",
}
cache_item, err := r.retrieveCache(ctx, sp)
if err != nil {
return nil, fmt.Errorf("Failed to retrieve cache, %w", err)
}
// START OF this is dumb
enc_spr, err := json.Marshal(cache_item.SPR)
if err != nil {
return nil, fmt.Errorf("Failed to marchal cache record, %w", err)
}
var props map[string]interface{}
err = json.Unmarshal(enc_spr, &props)
if err != nil {
return nil, err
}
// END OF this is dumb
orb_geom := cache_item.Geometry.Geometry()
f := geojson.NewFeature(orb_geom)
if err != nil {
return nil, err
}
f.Properties = props
enc_f, err := f.MarshalJSON()
if err != nil {
return nil, err
}
br := bytes.NewReader(enc_f)
return ioutil.NewReadSeekCloser(br)
}
func (r *RTreeSpatialDatabase) ReaderURI(ctx context.Context, str_uri string) string {
return str_uri
}
// whosonfirst/go-writer interface
func (r *RTreeSpatialDatabase) Write(ctx context.Context, key string, fh io.ReadSeeker) (int64, error) {
return 0, fmt.Errorf("Not implemented")
}
func (r *RTreeSpatialDatabase) WriterURI(ctx context.Context, str_uri string) string {
return str_uri
}
func (r *RTreeSpatialDatabase) Flush(ctx context.Context) error {
return nil
}
// Close defined above
func (r *RTreeSpatialDatabase) SetLogger(ctx context.Context, logger *log.Logger) error {
return nil
}