-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_tree.py
197 lines (143 loc) · 5.35 KB
/
r_tree.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
#Eftihia Kiafa AM:3003
import pymorton
import sys
import csv
import node
import math
global no_of_rectangles
count=-1
nodes=0
child_no=-1
global level
level=-1
global tree
tree=[]
global n
n=0
def find_mbr(mbr):
x_array=[]
y_array=[]
for i in mbr:
x_array.append(float(i[0]))
x_array.append(float(i[1]))
y_array.append(float(i[2]))
y_array.append(float(i[3]))
x_low=min(x_array)
x_high=max(x_array)
y_low=min(y_array)
y_high=max(y_array)
return [float(x_low), float(x_high), float(y_low), float(y_high)]
def minimum_bounding_rectangle(coords_array):
x_array=[]
y_array=[]
for i in coords_array:
i=i.split(",")
x_array.append(float(i[0]))
y_array.append(float(i[1]))
x_low=min(x_array)
x_high=max(x_array)
y_low=min(y_array)
y_high=max(y_array)
return [float(x_low), float(x_high), float(y_low), float(y_high)]
def divide_chunks(lst, n):
global tree
global child_no
global lst2
global count
#this is for leaves slice not for nodes
lst2=[]
if int(len(lst)/20)==absolute:
for i in range(0, len(lst), n):
if (len(lst[i+n:i + 2*n])<8 and lst[i+n:i + 2*n]!=[]):
n=20-(8-len(lst[i+n:i + 2*n]))
if i== len(lst)-len(lst)%20 and len(lst)%20!=0 :
lst2=lst[-8:]
else:
lst2=lst[i:i + n]
count+=1
yield lst2
tree.append([0,count,lst[i:i + n]])
else:
#this is for nodes
lst2=[]
for i in range(0, len(lst),n):
if (len(lst[i+n:i + 2*n])<8 and lst[i+n:i + 2*n]!=[]):
n=20-(8-len(lst[i+n:i + 2*n]))
count+=1
if i== len(lst)-len(lst)%20 and len(lst)%20!=0 :
lst2=lst[-8:]
else:
lst2=lst[i:i + n]
recs=list()#this is child list for each node
for k in lst2:
child_no+=1
k_mbrs=[]#this is for node mbrs
for r in k:
k_mbrs.append(r[1])
#collect mbrs and child_no for the list
recs.append([child_no,find_mbr(k_mbrs)])
lst[i:i + n]=recs
yield lst[i:i + n]
tree.append([1,count,recs])
def construct(no_of_rectangles,rectangles):
global leaves
global nodes
global level
#if no_of_rectangles<=1 then we can't bulk load anymore and we corrupt the recursion
if no_of_rectangles >1:
level+=1
max_capacity=20
no_of_nodes=math.ceil(no_of_rectangles/max_capacity)
print(str(no_of_nodes)+" nodes at level "+str(level))
leaves=divide_chunks(rectangles,max_capacity)
leaves=list(leaves)
no_of_rectangles=len(leaves)
construct(no_of_rectangles,leaves)
def main():
coords_file=sys.argv[1]
offsets_file=sys.argv[2]
coords_count=0
with open(coords_file,mode='r') as coords,open(offsets_file,mode='r')as offsets,open('Rtree.txt',mode='w',encoding='UTF8') as rtree:
coords_line=coords.readline()
offsets_line=offsets.readline()
global mbr_centres
global rectangle_objects
mbr_centres=dict()
rectangle_objects=dict()
while offsets_line and coords_line:
id=offsets_line.split(",")[0]
start=offsets_line.split(",")[1]
finish=offsets_line.split(",")[2]
coords_array=[]
while coords_count >= float(start) and float(finish) >= coords_count:
coords_array.append(coords_line)
coords_line=coords.readline()
coords_count+=1
#calculate minimum bounding rectangle and get its higher and lower point
mbr=minimum_bounding_rectangle(coords_array)
#reshape of mbr centre
x=float(float(mbr[0])+float(mbr[1]))/2
y=float(float(mbr[2])+float(mbr[3]))/2
#convert mbr centre to z-order value
centre=pymorton.interleave_latlng(y,x)
#collect all centres in dictionary by object id
mbr_centres[id]=int(centre)
rectangle_objects[id]=mbr
offsets_line=offsets.readline()
#sorting of reshaped mbr's z-order value array
mbr_centres=sorted(mbr_centres, key=lambda mbr : mbr_centres[mbr])
#convert array of strings to array of ints
m_c = [int(i) for i in mbr_centres]
rectangle_objects=[rectangle_objects[k] for k in mbr_centres]
rectangles=list(map(list, zip(m_c, rectangle_objects)))
global absolute
absolute=int(len(rectangle_objects)/20)
#beginning of tree construction
construct(len(rectangle_objects),rectangles)
for i in tree:
#change stdout for printing in file and not in terminal
sys.stdout = rtree
print(i)
rtree.close()
if __name__ == '__main__':
main()