forked from dhconnelly/rtreego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtree.go
612 lines (530 loc) · 15.8 KB
/
rtree.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
// Copyright 2012 Daniel Connelly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package rtreego is a library for efficiently storing and querying spatial data.
package rtreego
import (
"fmt"
"math"
"sort"
)
// 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
}
// Rtree represents an R-tree, a balanced search tree for storing and querying
// spatial objects. Dim specifies the number of spatial dimensions and
// MinChildren/MaxChildren specify the minimum/maximum branching factors.
type Rtree struct {
Dim int
MinChildren int
MaxChildren int
root *node
size int
height int
}
// NewTree creates a new R-tree instance.
func NewTree(Dim, MinChildren, MaxChildren int) *Rtree {
rt := Rtree{Dim: Dim, MinChildren: MinChildren, MaxChildren: MaxChildren}
rt.height = 1
rt.root = &node{}
rt.root.entries = []entry{}
rt.root.leaf = true
rt.root.level = 1
return &rt
}
// Size returns the number of objects currently stored in tree.
func (tree *Rtree) Size() int {
return tree.size
}
func (tree *Rtree) String() string {
return "foo"
}
// Depth returns the maximum depth of tree.
func (tree *Rtree) Depth() int {
return tree.height
}
// node represents a tree node of an Rtree.
type node struct {
parent *node
leaf bool
entries []entry
level int // node depth in the Rtree
}
func (n *node) String() string {
return fmt.Sprintf("node{leaf: %v, entries: %v}", n.leaf, n.entries)
}
// entry represents a spatial index record stored in a tree node.
type entry struct {
bb *Rect // bounding-box of all children of this entry
child *node
obj Spatial
}
func (e entry) String() string {
if e.child != nil {
return fmt.Sprintf("entry{bb: %v, child: %v}", e.bb, e.child)
}
return fmt.Sprintf("entry{bb: %v, obj: %v}", e.bb, e.obj)
}
// Spatial is an interface for objects that can be stored in an Rtree and queried.
type Spatial interface {
Bounds() *Rect
}
// Insertion
// Insert inserts a spatial object into the tree. If insertion
// causes a leaf node to overflow, the tree is rebalanced automatically.
//
// Implemented per Section 3.2 of "R-trees: A Dynamic Index Structure for
// Spatial Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.
func (tree *Rtree) Insert(obj Spatial) {
e := entry{obj.Bounds(), nil, obj}
tree.insert(e, 1)
tree.size++
}
// insert adds the specified entry to the tree at the specified level.
func (tree *Rtree) insert(e entry, level int) {
leaf := tree.chooseNode(tree.root, e, level)
leaf.entries = append(leaf.entries, e)
// update parent pointer if necessary
if e.child != nil {
e.child.parent = leaf
}
// split leaf if overflows
var split *node
if len(leaf.entries) > tree.MaxChildren {
leaf, split = leaf.split(tree.MinChildren)
}
root, splitRoot := tree.adjustTree(leaf, split)
if splitRoot != nil {
oldRoot := root
tree.height++
tree.root = &node{
parent: nil,
level: tree.height,
entries: []entry{
entry{bb: oldRoot.computeBoundingBox(), child: oldRoot},
entry{bb: splitRoot.computeBoundingBox(), child: splitRoot},
},
}
oldRoot.parent = tree.root
splitRoot.parent = tree.root
}
}
// chooseNode finds the node at the specified level to which e should be added.
func (tree *Rtree) chooseNode(n *node, e entry, level int) *node {
if n.leaf || n.level == level {
return n
}
// find the entry whose bb needs least enlargement to include obj
diff := math.MaxFloat64
var chosen entry
for _, en := range n.entries {
bb := boundingBox(en.bb, e.bb)
d := bb.size() - en.bb.size()
if d < diff || (d == diff && en.bb.size() < chosen.bb.size()) {
diff = d
chosen = en
}
}
return tree.chooseNode(chosen.child, e, level)
}
// adjustTree splits overflowing nodes and propagates the changes upwards.
func (tree *Rtree) adjustTree(n, nn *node) (*node, *node) {
// Let the caller handle root adjustments.
if n == tree.root {
return n, nn
}
// Re-size the bounding box of n to account for lower-level changes.
en := n.getEntry()
en.bb = n.computeBoundingBox()
// If nn is nil, then we're just propagating changes upwards.
if nn == nil {
return tree.adjustTree(n.parent, nil)
}
// Otherwise, these are two nodes resulting from a split.
// n was reused as the "left" node, but we need to add nn to n.parent.
enn := entry{nn.computeBoundingBox(), nn, nil}
n.parent.entries = append(n.parent.entries, enn)
// If the new entry overflows the parent, split the parent and propagate.
if len(n.parent.entries) > tree.MaxChildren {
return tree.adjustTree(n.parent.split(tree.MinChildren))
}
// Otherwise keep propagating changes upwards.
return tree.adjustTree(n.parent, nil)
}
// getEntry returns a pointer to the entry for the node n from n's parent.
func (n *node) getEntry() *entry {
var e *entry
for i := range n.parent.entries {
if n.parent.entries[i].child == n {
e = &n.parent.entries[i]
break
}
}
return e
}
// computeBoundingBox finds the MBR of the children of n.
func (n *node) computeBoundingBox() (bb *Rect) {
childBoxes := make([]*Rect, len(n.entries))
for i, e := range n.entries {
childBoxes[i] = e.bb
}
bb = boundingBoxN(childBoxes...)
return
}
// split splits a node into two groups while attempting to minimize the
// bounding-box area of the resulting groups.
func (n *node) split(minGroupSize int) (left, right *node) {
// find the initial split
l, r := n.pickSeeds()
leftSeed, rightSeed := n.entries[l], n.entries[r]
// get the entries to be divided between left and right
remaining := append(n.entries[:l], n.entries[l+1:r]...)
remaining = append(remaining, n.entries[r+1:]...)
// setup the new split nodes, but re-use n as the left node
left = n
left.entries = []entry{leftSeed}
right = &node{
parent: n.parent,
leaf: n.leaf,
level: n.level,
entries: []entry{rightSeed},
}
// TODO
if rightSeed.child != nil {
rightSeed.child.parent = right
}
if leftSeed.child != nil {
leftSeed.child.parent = left
}
// distribute all of n's old entries into left and right.
for len(remaining) > 0 {
next := pickNext(left, right, remaining)
e := remaining[next]
if len(remaining)+len(left.entries) <= minGroupSize {
assign(e, left)
} else if len(remaining)+len(right.entries) <= minGroupSize {
assign(e, right)
} else {
assignGroup(e, left, right)
}
remaining = append(remaining[:next], remaining[next+1:]...)
}
return
}
func assign(e entry, group *node) {
if e.child != nil {
e.child.parent = group
}
group.entries = append(group.entries, e)
}
// assignGroup chooses one of two groups to which a node should be added.
func assignGroup(e entry, left, right *node) {
leftBB := left.computeBoundingBox()
rightBB := right.computeBoundingBox()
leftEnlarged := boundingBox(leftBB, e.bb)
rightEnlarged := boundingBox(rightBB, e.bb)
// first, choose the group that needs the least enlargement
leftDiff := leftEnlarged.size() - leftBB.size()
rightDiff := rightEnlarged.size() - rightBB.size()
if diff := leftDiff - rightDiff; diff < 0 {
assign(e, left)
return
} else if diff > 0 {
assign(e, right)
return
}
// next, choose the group that has smaller area
if diff := leftBB.size() - rightBB.size(); diff < 0 {
assign(e, left)
return
} else if diff > 0 {
assign(e, right)
return
}
// next, choose the group with fewer entries
if diff := len(left.entries) - len(right.entries); diff <= 0 {
assign(e, left)
return
}
assign(e, right)
}
// pickSeeds chooses two child entries of n to start a split.
func (n *node) pickSeeds() (int, int) {
left, right := 0, 1
maxWastedSpace := -1.0
for i, e1 := range n.entries {
for j, e2 := range n.entries[i+1:] {
d := boundingBox(e1.bb, e2.bb).size() - e1.bb.size() - e2.bb.size()
if d > maxWastedSpace {
maxWastedSpace = d
left, right = i, j+i+1
}
}
}
return left, right
}
// pickNext chooses an entry to be added to an entry group.
func pickNext(left, right *node, entries []entry) (next int) {
maxDiff := -1.0
leftBB := left.computeBoundingBox()
rightBB := right.computeBoundingBox()
for i, e := range entries {
d1 := boundingBox(leftBB, e.bb).size() - leftBB.size()
d2 := boundingBox(rightBB, e.bb).size() - rightBB.size()
d := math.Abs(d1 - d2)
if d > maxDiff {
maxDiff = d
next = i
}
}
return
}
// Deletion
// Delete removes an object from the tree. If the object is not found, returns
// false, otherwise returns true. Uses the default comparator when checking
// equality.
//
// Implemented per Section 3.3 of "R-trees: A Dynamic Index Structure for
// Spatial Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.
func (tree *Rtree) Delete(obj Spatial) bool {
return tree.DeleteWithComparator(obj, defaultComparator)
}
// 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)
if n == nil {
return false
}
ind := -1
for i, e := range n.entries {
if cmp(e.obj, obj) {
ind = i
}
}
if ind < 0 {
return false
}
n.entries = append(n.entries[:ind], n.entries[ind+1:]...)
tree.condenseTree(n)
tree.size--
if !tree.root.leaf && len(tree.root.entries) == 1 {
tree.root = tree.root.entries[0].child
}
tree.height = tree.root.level
return true
}
// findLeaf finds the leaf node containing obj.
func (tree *Rtree) findLeaf(n *node, obj Spatial, cmp Comparator) *node {
if n.leaf {
return n
}
// if not leaf, search all candidate subtrees
for _, e := range n.entries {
if e.bb.containsRect(obj.Bounds()) {
leaf := tree.findLeaf(e.child, obj, cmp)
if leaf == nil {
continue
}
// check if the leaf actually contains the object
for _, leafEntry := range leaf.entries {
if cmp(leafEntry.obj, obj) {
return leaf
}
}
}
}
return nil
}
// condenseTree deletes underflowing nodes and propagates the changes upwards.
func (tree *Rtree) condenseTree(n *node) {
deleted := []*node{}
for n != tree.root {
if len(n.entries) < tree.MinChildren {
// remove n from parent entries
entries := []entry{}
for _, e := range n.parent.entries {
if e.child != n {
entries = append(entries, e)
}
}
if len(n.parent.entries) == len(entries) {
panic(fmt.Errorf("Failed to remove entry from parent"))
}
n.parent.entries = entries
// only add n to deleted if it still has children
if len(n.entries) > 0 {
deleted = append(deleted, n)
}
} else {
// just a child entry deletion, no underflow
n.getEntry().bb = n.computeBoundingBox()
}
n = n.parent
}
for _, n := range deleted {
// reinsert entry so that it will remain at the same level as before
e := entry{n.computeBoundingBox(), n, nil}
tree.insert(e, n.level+1)
}
}
// Searching
// SearchIntersect returns all objects that intersect the specified rectangle.
// Implemented per Section 3.1 of "R-trees: A Dynamic Index Structure for
// Spatial Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.
func (tree *Rtree) SearchIntersect(bb *Rect, filters ...Filter) []Spatial {
return tree.searchIntersect([]Spatial{}, tree.root, bb, filters)
}
// SearchIntersectWithLimit is similar to SearchIntersect, but returns
// immediately when the first k results are found. A negative k behaves exactly
// like SearchIntersect and returns all the results.
//
// Kept for backwards compatibility, please use SearchIntersect with a
// LimitFilter.
func (tree *Rtree) SearchIntersectWithLimit(k int, bb *Rect) []Spatial {
// backwards compatibility, previous implementation didn't limit results if
// k was negative.
if k < 0 {
return tree.SearchIntersect(bb)
}
return tree.SearchIntersect(bb, LimitFilter(k))
}
func (tree *Rtree) searchIntersect(results []Spatial, n *node, bb *Rect, filters []Filter) []Spatial {
for _, e := range n.entries {
if intersect(e.bb, bb) == nil {
continue
}
if !n.leaf {
results = tree.searchIntersect(results, e.child, bb, filters)
continue
}
refuse, abort := applyFilters(results, e.obj, filters)
if !refuse {
results = append(results, e.obj)
}
if abort {
break
}
}
return results
}
// NearestNeighbor returns the closest object to the specified point.
// Implemented per "Nearest Neighbor Queries" by Roussopoulos et al
func (tree *Rtree) NearestNeighbor(p Point) Spatial {
obj, _ := tree.nearestNeighbor(p, tree.root, math.MaxFloat64, nil)
return obj
}
// utilities for sorting slices of entries
type entrySlice struct {
entries []entry
dists []float64
pt Point
}
func (s entrySlice) Len() int { return len(s.entries) }
func (s entrySlice) Swap(i, j int) {
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
s.dists[i], s.dists[j] = s.dists[j], s.dists[i]
}
func (s entrySlice) Less(i, j int) bool {
return s.dists[i] < s.dists[j]
}
func sortEntries(p Point, entries []entry) ([]entry, []float64) {
sorted := make([]entry, len(entries))
dists := make([]float64, len(entries))
for i := 0; i < len(entries); i++ {
sorted[i] = entries[i]
dists[i] = p.minDist(entries[i].bb)
}
sort.Sort(entrySlice{sorted, dists, p})
return sorted, dists
}
func pruneEntries(p Point, entries []entry, minDists []float64) []entry {
minMinMaxDist := math.MaxFloat64
for i := range entries {
minMaxDist := p.minMaxDist(entries[i].bb)
if minMaxDist < minMinMaxDist {
minMinMaxDist = minMaxDist
}
}
// remove all entries with minDist > minMinMaxDist
pruned := []entry{}
for i := range entries {
if minDists[i] <= minMinMaxDist {
pruned = append(pruned, entries[i])
}
}
return pruned
}
func (tree *Rtree) nearestNeighbor(p Point, n *node, d float64, nearest Spatial) (Spatial, float64) {
if n.leaf {
for _, e := range n.entries {
dist := math.Sqrt(p.minDist(e.bb))
if dist < d {
d = dist
nearest = e.obj
}
}
} else {
branches, dists := sortEntries(p, n.entries)
branches = pruneEntries(p, branches, dists)
for _, e := range branches {
subNearest, dist := tree.nearestNeighbor(p, e.child, d, nearest)
if dist < d {
d = dist
nearest = subNearest
}
}
}
return nearest, d
}
// NearestNeighbors gets the closest Spatials to the Point.
func (tree *Rtree) NearestNeighbors(k int, p Point) []Spatial {
dists := make([]float64, k)
objs := make([]Spatial, k)
for i := 0; i < k; i++ {
dists[i] = math.MaxFloat64
objs[i] = nil
}
objs, _ = tree.nearestNeighbors(k, p, tree.root, dists, objs)
return objs
}
// insert obj into nearest and return the first k elements in increasing order.
func insertNearest(k int, dists []float64, nearest []Spatial, dist float64, obj Spatial) ([]float64, []Spatial) {
i := 0
for i < k && dist >= dists[i] {
i++
}
if i >= k {
return dists, nearest
}
left, right := dists[:i], dists[i:k-1]
updatedDists := make([]float64, k)
copy(updatedDists, left)
updatedDists[i] = dist
copy(updatedDists[i+1:], right)
leftObjs, rightObjs := nearest[:i], nearest[i:k-1]
updatedNearest := make([]Spatial, k)
copy(updatedNearest, leftObjs)
updatedNearest[i] = obj
copy(updatedNearest[i+1:], rightObjs)
return updatedDists, updatedNearest
}
func (tree *Rtree) nearestNeighbors(k int, p Point, n *node, dists []float64, nearest []Spatial) ([]Spatial, []float64) {
if n.leaf {
for _, e := range n.entries {
dist := math.Sqrt(p.minDist(e.bb))
dists, nearest = insertNearest(k, dists, nearest, dist, e.obj)
}
} else {
branches, branchDists := sortEntries(p, n.entries)
branches = pruneEntries(p, branches, branchDists)
for _, e := range branches {
nearest, dists = tree.nearestNeighbors(k, p, e.child, dists, nearest)
}
}
return nearest, dists
}