-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathver5_2.py
254 lines (180 loc) · 5.49 KB
/
ver5_2.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
#---------------------NEAREST NEIGHBOURS USING KD-TREES----------------------
#Imported modules and files
import math
import matplotlib.pyplot as plt
import time
from kd_tree import KDTree
def find_places(tree,d,pts):
x,y=[],[]
x_all,y_all=[],[]
for i in pts[0] :
x_all.append(i[0][0])
y_all.append(i[0][1])
#print(x_all,"--",y_all)
print("Do you want to find the closest ",pts[1],"\n\t1) Enter the no. closest points\n\t2) In a given radius")
t=int(input())
start_time=time.time()
if t==1:
l=int(input("Enter no. of closest points required : "))
print("\n\n---- CLOSEST ",l," ",pts[1]," ----")
best=tree.nearest_neighbor(d,t,l)
for i in range(l):
x.append(best[i][0][0])
y.append(best[i][0][1])
print("mindistance :",math.sqrt(best[i][2]))
print("label:",best[i][1])
print("closest point",best[i][0])
print("counter of recursive_search:",tree.returncounter())
print("- - - - - - - - - - - - - - - ")
print("-------",time.time()-start_time,"-------")
patch=plt.Circle((d[0],d[1]),radius=math.sqrt(best[0][2]),color="#98ffff",alpha=0.2)
ax=plt.gca()
ax.add_patch(patch)
# plt.axis('scaled')
plt.scatter(d[0], d[1], label="My Location", color= "black",marker= "^", s=140)
# plt.scatter(x, y, label= pts[1], color= "green",marker= "*", s=30)
plt.scatter(x_all, y_all, label= pts[1], color= pts[2],marker= "*", s=30)
plt.scatter(x,y,s=80,facecolors='none',edgecolors='b')
# plt.xlabel('x - axis')
# plt.ylabel('y - axis')
# plt.title("CLOSEST ", pts[1])
plt.legend()
plt.show()
if t==2:
#l=int(input("Enter no. of closest points required : "))
print(" ---- CLOSEST ",pts[1]," ----")
r=float(input("\nEnter search radius :"))
best=tree.nearest_neighbor(d,t,0,r)
for i in range(len(best)):
x.append(best[i][0][0])
y.append(best[i][0][1])
print("mindistance :",math.sqrt(best[i][2]))
print("label:",best[i][1])
print("closest point",best[i][0])
print("counter of recursive_search:",tree.returncounter())
print("- - - - - - - - - - - - - - - ")
patch=plt.Circle((d[0],d[1]),radius=r,color="#98ffff",alpha=0.2)
ax=plt.gca()
ax.add_patch(patch)
# plt.axis('scaled')
plt.scatter(d[0], d[1], label="My Location", color= "black",marker= "^", s=140)
# plt.scatter(x, y, label= pts[1], color= "green",marker= "*", s=30)
plt.scatter(x_all, y_all, label= pts[1], color= pts[2],marker= "*", s=30)
plt.scatter(x,y,s=80,facecolors='none',edgecolors='b')
# plt.xlabel('x - axis')
# plt.ylabel('y - axis')
# plt.title("CLOSEST ", pts[1])
plt.axis('scaled')
plt.legend()
plt.show()
def plotgraph(*args):
for arg in args:
x=[]
y=[]
for i in arg[0] :
x.append(i[0][0])
y.append(i[0][1])
plt.scatter(x, y, label= arg[1], color=arg[2], marker= "*", s=80)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('CITY')
# showing legend
def hotels():
points=[]
c=0
infile=open('hotels.txt','r')
for i in infile:
points.append([])
points[c].append((list(map(float,i.rstrip().split(",")))))
points[c].append(c)
c+=1
return KDTree(points),points
def schools():
points=[]
c=0
infile=open('schools.txt','r')
for i in infile:
points.append([])
points[c].append((list(map(float,i.rstrip().split(",")))))
points[c].append(c)
c+=1
return KDTree(points),points
def police():
points=[]
c=0
infile=open('police.txt','r')
for i in infile:
points.append([])
points[c].append((list(map(float,i.rstrip().split(",")))))
points[c].append(c)
c+=1
return KDTree(points),points
def hospitals():
points=[]
c=0
infile=open('hospitals.txt','r')
for i in infile:
points.append([])
points[c].append((list(map(float,i.rstrip().split(",")))))
points[c].append(c)
c+=1
return KDTree(points),points
def petrol_bunk():
points=[]
c=0
infile=open('petrol_bunk.txt','r')
for i in infile:
points.append([])
points[c].append((list(map(float,i.rstrip().split(",")))))
points[c].append(c)
c+=1
return KDTree(points),points
def main():
#Entering own location :
print("Enter your location ( x y ): ")
d=list(map(float,input().split()))
#defining python lists to store attributes of a type of place
hotel_list =[None,"hotels","green"]
police_list =[None,"police","yellow"]
hospital_list=[None,"hospitals","red"]
petrol_list =[None,"petrol_bunk","cyan"]
school_list =[None,"schools","blue"]
#making respective trees of differents places
police_tree , police_list[0] = police()
hotel_tree , hotel_list[0] = hotels()
school_tree , school_list[0] = schools()
petrol_tree , petrol_list[0] = petrol_bunk()
hospital_tree,hospital_list[0] = hospitals()
#plotting graph
plotgraph(police_list,hotel_list,school_list,petrol_list,hospital_list)
# my location
plt.scatter(d[0], d[1], label="My_Location", color="black", marker= "^", s=140)
plt.legend()
# function to show the plot
plt.show()
#clear graph for future use
plt.clf()
print("Which closest place do you wanna find?",
"\n\t1.Police Station",
"\n\t2.Hotels",
"\n\t3.Schools",
"\n\t4.Petrol Bunk",
"\n\t5.Hospitals")
choice=int(input())
if choice==1:
find_places(police_tree,d,police_list)
elif choice==2:
find_places(hotel_tree,d,hotel_list)
elif choice==3:
find_places(school_tree,d,school_list)
elif choice==4:
find_places(petrol_tree,d,petrol_list)
elif choice==5:
find_places(hospital_tree,d,hospital_list)
else:
print("Wrong choice!")
if __name__ == '__main__':
main()