-
Notifications
You must be signed in to change notification settings - Fork 129
/
hpr_tree.go
339 lines (299 loc) · 9.45 KB
/
hpr_tree.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
// Package hprtree a hprtree is a spatial index structure .
// This is a static R-tree which is packed by using the Hilbert ordering of the tree items.
package hprtree
import (
"log"
"math"
"sort"
"github.com/spatial-go/geoos/algorithm/matrix/envelope"
"github.com/spatial-go/geoos/index"
)
// HPRTree const parameter.
const (
EnvSize = 4
HilbertLevel = 12
DefaultNodeCapacity = 16
)
// HPRTree A Hilbert-Packed R-tree. This is a static R-tree which is packed by
// using the Hilbert ordering of the tree items.
// The tree is constructed by sorting the items by the Hilbert code of the midpoint of their envelope.
type HPRTree struct {
nodeCapacity int
totalExtent *envelope.Envelope
Items []interface{}
layerStartIndex []int
nodeBounds []float64
isBuilt bool
}
// NewHPRTree return default NewHPRTree.
func NewHPRTree() *HPRTree {
h := &HPRTree{}
h.nodeCapacity = DefaultNodeCapacity
h.totalExtent = &envelope.Envelope{}
return h
}
// Size Gets the number of items in the index.
func (h *HPRTree) Size() int {
return len(h.Items)
}
// Insert Adds a spatial item with an extent specified by the given Envelope to the index
func (h *HPRTree) Insert(itemEnv *envelope.Envelope, item interface{}) error {
if h.isBuilt {
return index.ErrHPRInsert
}
h.Items = append(h.Items, &Item{itemEnv, item})
h.totalExtent.ExpandToIncludeEnv(itemEnv)
return nil
}
// Query Queries the index for all items whose extents intersect the given search Envelope
// Note that some kinds of indexes may also return objects which do not in fact
// intersect the query envelope.
func (h *HPRTree) Query(searchEnv *envelope.Envelope) interface{} {
h.build()
if !h.totalExtent.IsIntersects(searchEnv) {
return h.Items
}
visitor := &index.ArrayVisitor{}
if err := h.QueryVisitor(searchEnv, visitor); err != nil {
log.Println(err)
}
return visitor.Items
}
// QueryVisitor Queries the index for all items whose extents intersect the given search Envelope,
// and applies an ItemVisitor to them.
// Note that some kinds of indexes may also return objects which do not in fact
// intersect the query envelope.
func (h *HPRTree) QueryVisitor(searchEnv *envelope.Envelope, visitor index.ItemVisitor) error {
h.build()
if !h.totalExtent.IsIntersects(searchEnv) {
return index.ErrHPRNotIsIntersects
}
if h.layerStartIndex == nil {
h.queryItems(0, searchEnv, visitor)
} else {
h.queryTopLayer(searchEnv, visitor)
}
return nil
}
func (h *HPRTree) queryTopLayer(searchEnv *envelope.Envelope, visitor index.ItemVisitor) {
layerIndex := len(h.layerStartIndex) - 2
layerSize := h.layerSize(layerIndex)
// query each node in layer
for i := 0; i < layerSize; i += EnvSize {
h.queryNode(layerIndex, i, searchEnv, visitor)
}
}
func (h *HPRTree) queryNode(layerIndex, nodeOffset int, searchEnv *envelope.Envelope, visitor index.ItemVisitor) {
layerStart := h.layerStartIndex[layerIndex]
nodeIndex := layerStart + nodeOffset
if !h.isIntersects(nodeIndex, searchEnv) {
return
}
if layerIndex == 0 {
childNodesOffset := nodeOffset / EnvSize * h.nodeCapacity
h.queryItems(childNodesOffset, searchEnv, visitor)
} else {
childNodesOffset := nodeOffset * h.nodeCapacity
h.queryNodeChildren(layerIndex-1, childNodesOffset, searchEnv, visitor)
}
}
func (h *HPRTree) isIntersects(nodeIndex int, env *envelope.Envelope) bool {
//nodeIntersectsCount++;
isBeyond := (env.MaxX < h.nodeBounds[nodeIndex]) || (env.MaxY < h.nodeBounds[nodeIndex+1]) ||
(env.MinX > h.nodeBounds[nodeIndex+2]) || (env.MinY > h.nodeBounds[nodeIndex+3])
return !isBeyond
}
func (h *HPRTree) queryNodeChildren(layerIndex, blockOffset int, searchEnv *envelope.Envelope, visitor index.ItemVisitor) {
layerStart := h.layerStartIndex[layerIndex]
layerEnd := h.layerStartIndex[layerIndex+1]
for i := 0; i < h.nodeCapacity; i++ {
nodeOffset := blockOffset + EnvSize*i
// don't query past layer end
if layerStart+nodeOffset >= layerEnd {
break
}
h.queryNode(layerIndex, nodeOffset, searchEnv, visitor)
}
}
func (h *HPRTree) queryItems(blockStart int, searchEnv *envelope.Envelope, visitor index.ItemVisitor) {
for i := 0; i < h.nodeCapacity; i++ {
itemIndex := blockStart + i
// don't query past end of items
if itemIndex >= h.Size() {
break
}
// visit the item if its envelope intersects search env
item := h.Items[itemIndex].(*Item)
//nodeIntersectsCount++;
if h.isIntersectsEnv(item.Env, searchEnv) {
//if (item.getEnvelope().intersects(searchEnv)) {
visitor.VisitItem(item.Item)
}
}
}
// isIntersectsEnv Tests whether two envelopes intersect.
// Avoids the nil check in.
func (h *HPRTree) isIntersectsEnv(env1, env2 *envelope.Envelope) bool {
return !(env2.MinX > env1.MaxX ||
env2.MaxX < env1.MinX ||
env2.MinY > env1.MaxY ||
env2.MaxY < env1.MinY)
}
func (h *HPRTree) layerSize(layerIndex int) int {
layerStart := h.layerStartIndex[layerIndex]
layerEnd := h.layerStartIndex[layerIndex+1]
return layerEnd - layerStart
}
// Remove Removes a single item from the tree.
func (h *HPRTree) Remove(itemEnv *envelope.Envelope, item interface{}) bool {
// TODO Auto-generated method stub
return false
}
// build Builds the index, if not already built.
func (h *HPRTree) build() {
// skip if already built
if h.isBuilt {
return
}
h.isBuilt = true
// don't need to build an empty or very small tree
if h.Size() <= h.nodeCapacity {
return
}
h.sortItems()
//dumpItems(items);
h.layerStartIndex = h.computeLayerIndices(h.Size(), h.nodeCapacity)
// allocate storage
nodeCount := h.layerStartIndex[len(h.layerStartIndex)-1] / 4
h.nodeBounds = h.createBoundsArray(nodeCount)
// compute tree nodes
h.computeLeafNodes(h.layerStartIndex[1])
for i := 1; i < len(h.layerStartIndex)-1; i++ {
h.computeLayerNodes(i)
}
//dumpNodes();
}
func (h *HPRTree) createBoundsArray(size int) []float64 {
a := make([]float64, 4*size)
for i := 0; i < size; i++ {
index := 4 * i
a[index] = math.MaxFloat64
a[index+1] = math.MaxFloat64
a[index+2] = -math.MaxFloat64
a[index+3] = -math.MaxFloat64
}
return a
}
func (h *HPRTree) computeLayerNodes(layerIndex int) {
layerStart := h.layerStartIndex[layerIndex]
childLayerStart := h.layerStartIndex[layerIndex-1]
layerSize := h.layerSize(layerIndex)
childLayerEnd := layerStart
for i := 0; i < layerSize; i += EnvSize {
childStart := childLayerStart + h.nodeCapacity*i
h.computeNodeBounds(layerStart+i, childStart, childLayerEnd)
}
}
func (h *HPRTree) computeNodeBounds(nodeIndex, blockStart, nodeMaxIndex int) {
for i := 0; i <= h.nodeCapacity; i++ {
index := blockStart + 4*i
if index >= nodeMaxIndex {
break
}
h.updateNodeBounds(nodeIndex, h.nodeBounds[index], h.nodeBounds[index+1], h.nodeBounds[index+2], h.nodeBounds[index+3])
}
}
func (h *HPRTree) computeLeafNodes(layerSize int) {
for i := 0; i < layerSize; i += EnvSize {
h.computeLeafNodeBounds(i, h.nodeCapacity*i/4)
}
}
func (h *HPRTree) computeLeafNodeBounds(nodeIndex, blockStart int) {
for i := 0; i <= h.nodeCapacity; i++ {
itemIndex := blockStart + i
if itemIndex >= h.Size() {
break
}
env := h.Items[itemIndex].(Item).Env
h.updateNodeBounds(nodeIndex, env.MinX, env.MinY, env.MaxX, env.MaxY)
}
}
func (h *HPRTree) updateNodeBounds(nodeIndex int, minX, minY, maxX, maxY float64) {
if minX < h.nodeBounds[nodeIndex] {
h.nodeBounds[nodeIndex] = minX
}
if minY < h.nodeBounds[nodeIndex+1] {
h.nodeBounds[nodeIndex+1] = minY
}
if maxX > h.nodeBounds[nodeIndex+2] {
h.nodeBounds[nodeIndex+2] = maxX
}
if maxY > h.nodeBounds[nodeIndex+3] {
h.nodeBounds[nodeIndex+3] = maxY
}
}
//TODO
// func (h *HPRTree) getNodeEnvelope(i int) *envelope.Envelope {
// return envelope.FourFloat(h.nodeBounds[i], h.nodeBounds[i+1], h.nodeBounds[i+2], h.nodeBounds[i+3])
// }
func (h *HPRTree) computeLayerIndices(itemSize, nodeCapacity int) []int {
layerIndexList := []int{}
layerSize := itemSize
index := 0
for layerSize > 1 {
layerIndexList = append(layerIndexList, index)
layerSize := h.numNodesToCover(layerSize, nodeCapacity)
index += EnvSize * layerSize
}
return layerIndexList
}
// numNodesToCover Computes the number of blocks (nodes) required to
// cover a given number of children.
func (h *HPRTree) numNodesToCover(nChild, nodeCapacity int) int {
mult := nChild / nodeCapacity
total := mult * nodeCapacity
if total == nChild {
return mult
}
return mult + 1
}
//TODO
//getBounds return a list of the internal node extents
// func (h *HPRTree) getBounds() []*envelope.Envelope {
// numNodes := len(h.nodeBounds) / 4
// bounds := make([]*envelope.Envelope, numNodes)
// // create from largest to smallest
// for i := numNodes - 1; i >= 0; i-- {
// boundIndex := 4 * i
// bounds[i] = envelope.FourFloat(h.nodeBounds[boundIndex], h.nodeBounds[boundIndex+2],
// h.nodeBounds[boundIndex+1], h.nodeBounds[boundIndex+3])
// }
// return bounds
// }
func (h *HPRTree) sortItems() {
comp := &ItemComparator{items: h.Items, encoder: NewHilbertEncoder(HilbertLevel, h.totalExtent)}
sort.Sort(comp)
h.Items = comp.items
}
// ItemComparator sort items by HilbertEncoder
type ItemComparator struct {
items []interface{}
encoder *HilbertEncoder
}
// Len ...
func (it *ItemComparator) Len() int {
return len(it.items)
}
// Less ...
func (it *ItemComparator) Less(i, j int) bool {
hCode1 := it.encoder.encode(it.items[i].(Item).Env)
hCode2 := it.encoder.encode(it.items[j].(Item).Env)
return hCode1 < hCode2
}
// Swap ...
func (it ItemComparator) Swap(i, j int) {
it.items[i], it.items[j] = it.items[j], it.items[i]
}
var (
_ index.SpatialIndex = &HPRTree{}
)