-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRtree.py
416 lines (344 loc) · 15.5 KB
/
Rtree.py
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
"""
Lewis Smith
Based loosely off the psuedo-code in:
Enhanced nearest neighbour search on the R-tree (Cheung, Fu 1998)
&
R-Trees: A Dynamic Index Structure for Spatial Searching (Antonn Guttmann, 1984)
"""
from random import randint
from math import ceil, sqrt
import sys
import timeit
class Rtree:
def __init__(self, root=None, b=6.0):
self.root = root
self.size = 0
self.b = b
self.seq_load = 0
self.depth = 0
def r_tree(self):
self.root = self.create_root(True)
def create_root(self, leaf_bool):
init_coordinates = [float("inf"), float("inf"), float("inf"), float("inf")]
return self.Node(leaf_bool, bounding_box=init_coordinates, children=[], root=True)
def load_points(self, args):
start_load = timeit.default_timer()
with open(args[0]) as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split() for x in content]
self.r_tree()
self.size = float(content[0][0])
content.pop(0)
if self.size > 10000:
self.b = self.size*0.01
[self.insert([int(x[1]), int(x[2])], x[0]) for x in content]
stop_load = timeit.default_timer()
self.seq_load = stop_load - start_load
print("Load time: " + str(self.seq_load))
start_query = timeit.default_timer()
with open(args[1]) as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split() for x in content]
[self.range_query([int(x[0]), float(x[3]), float(x[1]), float(x[2])]) for x in content]
stop_query = timeit.default_timer()
f = open('query_results.txt', 'a')
f.write('Total Time = ' + str(stop_query - start_query) + '\n' +
'Average Time = ' + str((stop_query - start_query)/100))
f.close()
start_nn = timeit.default_timer()
with open(args[2]) as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split() for x in content]
[self.nearest([float(x[0]), float(x[1])]) for x in content]
stop_nn = timeit.default_timer()
f = open('nn_results.txt', 'a')
f.write('Total Time = ' + str(stop_nn - start_nn) + '\n' +
'Average Time = ' + str((stop_query - start_query)/100))
f.close()
def insert(self, coordinates, id):
entry = self.Node.Entry(id, coordinates, self.Node)
leaf = self.find_leaf(self.root, entry)
leaf.children.append(entry)
self.size = self.size + 1
entry.parent = leaf
if len(leaf.children) > ceil(0.4*self.b):
new_splits = self.split_node(leaf)
self.adjust_tree(new_splits[0], new_splits[1])
else:
self.adjust_tree(leaf, None)
def range_query(self, coordinates):
results = []
self.query(coordinates, self.root, results)
f = open('query_results.txt', 'a')
f.write(str(len(results)) + '\n')
f.close()
def query(self, coordinates, node, results):
if node.leaf:
for child in node.children:
if self.overlap_leaf(coordinates, child.coordinates):
results.append(child.id)
else:
for child in node.children:
if self.overlaps(coordinates, child.bounding_box):
self.query(coordinates, child, results)
@staticmethod
def overlap_leaf(coordinates, leaf_coordinates):
if leaf_coordinates[0] < coordinates[0] or leaf_coordinates[0] > coordinates[3] or \
leaf_coordinates[1] < coordinates[3] or leaf_coordinates[1] > coordinates[1]:
return False
else:
return True
def nearest(self, coordinates):
point_id = []
distance = float("inf")
self.nearest_neighbor(coordinates, self.root, point_id, distance)
f = open('nn_results.txt', 'a')
f.write(str(min(point_id, key=lambda x: x[1])[0]) + '\n')
f.close()
def nearest_neighbor(self, coordinates, node, point_id, dist):
if node.leaf:
distance = float("inf")
for child in node.children:
temp = sqrt((child.coordinates[0] - coordinates[0])**2
+ (child.coordinates[1] - coordinates[1])**2)
if temp < distance:
distance = temp
point_id.append([child.id, temp])
else:
list = []
for child in node.children:
if child.bounding_box[0] < coordinates[0]:
dx = coordinates[0] - child.bounding_box[0]
elif child.bounding_box[0] > coordinates[0]:
dx = child.bounding_box[0] - coordinates[0]
else:
dx = 0
if child.bounding_box[3] < coordinates[1]:
dy = coordinates[1] - child.bounding_box[3]
elif child.bounding_box[3] > coordinates[1]:
dy = child.bounding_box[3] - coordinates[1]
else:
dy = 0
min_distance = dx * dx + dy * dy
list.append([child, min_distance])
ABL = sorted(list, key=lambda x: x[1])
for node in ABL:
if node[1] < dist:
self.nearest_neighbor(coordinates, node[0], point_id, dist)
@staticmethod
def overlaps(coordinates, child_box):
if coordinates[0] > child_box[2] or coordinates[1] < child_box[3] \
or coordinates[2] < child_box[0] or coordinates[3] > child_box[1]:
return False
else:
return True
def find_leaf(self, node, entry):
if node.leaf:
return node
min_increase = float("inf")
for child in node.children:
exp = self.get_expansion(child.bounding_box, entry)
if exp < min_increase:
min_increase = exp
next_child = child
elif exp == min_increase:
current_area = self.get_area(next_child.bounding_box)
new_area = self.get_area(node.bounding_box)
if new_area < current_area:
next_child = child
return self.find_leaf(next_child, entry)
@staticmethod
def get_area(bounding_box):
return (bounding_box[2]-bounding_box[0])*(bounding_box[1]-bounding_box[3])
def get_expansion(self, bounding_box, entry):
area = self.get_area(bounding_box)
new = list(bounding_box)
if type(entry) is self.Node.Entry:
if bounding_box[0] > entry.coordinates[0]:
new[0] = entry.coordinates[0]
if bounding_box[2] < entry.coordinates[0]:
new[2] = entry.coordinates[0]
if bounding_box[1] < entry.coordinates[1]:
new[1] = entry.coordinates[1]
if bounding_box[3] > entry.coordinates[1]:
new[3] = entry.coordinates[1]
else:
if bounding_box[0] < entry.bounding_box[0]:
new[0] = entry.bounding_box[0]
if bounding_box[2] > entry.bounding_box[2]:
new[2] = entry.bounding_box[2]
if bounding_box[1] > entry.bounding_box[1]:
new[1] = entry.bounding_box[1]
if bounding_box[3] < entry.bounding_box[3]:
new[3] = entry.bounding_box[3]
return self.get_area(new) - area
def adjust_tree(self, node_one, node_two):
if node_one.root:
if node_two is not None:
node_one.root = False
self.root = self.create_root(False)
self.root.children.append(node_one)
node_one.parent = self.root
self.root.children.append(node_two)
node_two.parent = self.root
self.tighten([self.root])
return
self.tighten([node_one])
if node_two is not None:
self.tighten([node_two])
if len(node_one.parent.children) > ceil(0.4*self.b):
splits = self.split_node(node_one.parent)
self.adjust_tree(splits[0], splits[1])
if node_one.parent is not None:
self.adjust_tree(node_one.parent, None)
def split_node(self, node):
nodes = [node, self.Node(node.leaf, bounding_box=node.bounding_box, parent=node.parent, children=[])]
if nodes[1].parent is not None:
nodes[1].parent.children.append(nodes[1])
childs = list(node.children)
node.children = []
seed_one, seed_two = self.pick_seeds(childs)
nodes[0].children.append(seed_one)
nodes[1].children.append(seed_two)
self.tighten(nodes)
while len(childs) != 0:
if len(nodes[0].children) >= 2 and len(nodes[1].children) + len(childs) == 2:
nodes[1].children.extend(list(childs))
childs = []
self.tighten(nodes)
return nodes
elif len(nodes[1].children) >= 2 and len(nodes[0].children) + len(childs) == 2:
nodes[0].children.extend(list(childs))
childs = []
self.tighten(nodes)
return nodes
seed = self.get_seed(childs)
expansion_one = self.get_expansion(nodes[0].bounding_box, seed)
expansion_two = self.get_expansion(nodes[1].bounding_box, seed)
if expansion_one < expansion_two:
ideal_node = nodes[0]
elif expansion_one > expansion_two:
ideal_node = nodes[1]
else:
area_one = self.get_area(nodes[0].bounding_box)
area_two = self.get_area(nodes[1].bounding_box)
if area_one < area_two:
ideal_node = nodes[0]
elif area_one > area_two:
ideal_node = nodes[1]
else:
if len(nodes[0].children) < len(nodes[1].children):
ideal_node = nodes[0]
elif len(nodes[0].children) > len(nodes[1].children):
ideal_node = nodes[1]
else:
ideal_node = nodes[randint(0, len(nodes) - 1)]
ideal_node.children.append(seed)
self.tighten([ideal_node])
return list(nodes)
@staticmethod
def get_seed(children):
return children.pop(0)
def pick_seeds(self, children):
found = False
best_separation = 0.0
for i in range(2):
lower_bound = float("inf")
upper_bound = -1*float("inf")
min_upper_bound = float("inf")
max_lower_bound = -1*float("inf")
for node in iter(children):
if type(node) is self.Node.Entry:
if node.coordinates[i] < lower_bound:
lower_bound = node.coordinates[i]
if node.coordinates[i] > upper_bound:
upper_bound = node.coordinates[i]
if node.coordinates[i] > max_lower_bound:
max_lower_bound = node.coordinates[i]
node_lower_bound = node
if node.coordinates[i] < min_upper_bound:
min_upper_bound = node.coordinates[i]
node_upper_bound = node
else:
if i == 0:
if node.bounding_box[i+2] > upper_bound:
upper_bound = node.bounding_box[i+2]
if node.bounding_box[i] < lower_bound:
lower_bound = node.bounding_box[i]
if node.bounding_box[i+2] < min_upper_bound:
min_upper_bound = node.bounding_box[i+2]
node_upper_bound = node
if node.bounding_box[i] > max_lower_bound:
max_lower_bound = node.bounding_box[i]
node_lower_bound = node
else:
if node.bounding_box[i] > upper_bound:
upper_bound = node.bounding_box[i]
if node.bounding_box[i+2] < lower_bound:
lower_bound = node.bounding_box[i+2]
if node.bounding_box[i] < min_upper_bound:
min_upper_bound = node.bounding_box[i]
node_upper_bound = node
if node.bounding_box[i+2] > max_lower_bound:
max_lower_bound = node.bounding_box[i+2]
node_lower_bound = node
if node_lower_bound == node_upper_bound:
separation = -1.0
else:
if min_upper_bound == max_lower_bound or upper_bound == lower_bound:
separation = -1.0
else:
separation = abs((max_lower_bound-min_upper_bound)/(upper_bound-lower_bound))
if separation >= best_separation:
seeds = [node_lower_bound, node_upper_bound]
best_separation = separation
found = True
if not found:
seeds = list([children[0], children[1]])
children.remove(seeds[0])
children.remove(seeds[1])
return seeds[0], seeds[1]
def tighten(self, nodes):
for node in iter(nodes):
min_coordinates = [float("inf"), -1*float("inf"), -1*float("inf"), float("inf")]
for child in iter(node.children):
if type(child) is self.Node.Entry:
if child.coordinates[0] < min_coordinates[0]:
min_coordinates[0] = child.coordinates[0]
if child.coordinates[0] > min_coordinates[2]:
min_coordinates[2] = child.coordinates[0]
if child.coordinates[1] > min_coordinates[1]:
min_coordinates[1] = child.coordinates[1]
if child.coordinates[1] < min_coordinates[3]:
min_coordinates[3] = child.coordinates[1]
else:
if child.bounding_box[0] < min_coordinates[0]:
min_coordinates[0] = child.bounding_box[0]
if child.bounding_box[2] > min_coordinates[2]:
min_coordinates[2] = child.bounding_box[2]
if child.bounding_box[1] > min_coordinates[1]:
min_coordinates[1] = child.bounding_box[1]
if child.bounding_box[3] < min_coordinates[3]:
min_coordinates[3] = child.bounding_box[3]
node.bounding_box = list(min_coordinates)
class Node:
def __init__(self, leaf, parent=None, children=None, bounding_box=None, root=False):
self.leaf = leaf
self.parent = parent
self.children = children
self.bounding_box = bounding_box
self.root = root
class Entry:
def __init__(self, id, coordinates, node):
self.id = id
self.node = node
self.leaf = True
self.coordinates = coordinates
if __name__ == "__main__":
r = Rtree()
if (len(sys.argv[1:]) != 3):
raise ValueError('Usage: Rtree.py dataset queryset nn_queryset')
r.load_points(sys.argv[1:])