-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
553 lines (463 loc) · 24.7 KB
/
Copy pathapp.py
File metadata and controls
553 lines (463 loc) · 24.7 KB
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
from flask import Flask, render_template, request, json
import os
import math
import csv
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/index.html')
def return_to_home():
return render_template('index.html')
@app.route('/map.html')
def map():
return render_template('map.html')
@app.route('/resources.html')
def resources():
return render_template('resources.html')
@app.route('/get_started.html')
def get_started():
return render_template('get_started.html')
@app.route('/search', methods=['POST'])
def search():
output = request.get_json()
site_root = os.path.realpath(os.path.dirname(__file__))
location_json_url = os.path.join(site_root, "static", "locations.json")
teachers_csv_url = os.path.join(site_root, "static", "teachers.csv")
location_data = json.load(open(location_json_url))
teacher_data = csv.reader(open(teachers_csv_url))
results = {"search_results": []}
search_filter = output["search_filter"]
search_value = str(output["room_value"])
latitude = output["current_latitude"]
longitude = output["current_longitude"]
period = output["current_period"]
floor = output["current_floor"]
if search_filter == "room_number" or search_filter == "room_name":
for location in location_data["rooms"]:
temp_location = location
location_room_value = str(location["room_value"])
# Checks if the beginning part of the search matches a location name and if it is on the same floor as
# the user and will get the distance using longitude and latitude and sort the search results based on that
if len(search_value) != 0 and location_room_value[:len(search_value)].lower() == search_value.lower():
latitude_diff = abs(latitude - location["latitude"])
longitude_diff = abs(longitude - location["longitude"])
temp_location["distance"] = math.sqrt((latitude_diff ** 2) + (longitude_diff ** 2))
location_placed = False
search_results = results["search_results"]
if len(search_results) > 0:
for i in range(len(search_results) - 1):
if search_results[i]["distance"] < temp_location["distance"] < search_results[i + 1]["distance"]:
search_results.insert(i + 1, temp_location)
location_placed = True
if not location_placed:
if len(search_results) == 0 or search_results[len(search_results) - 1]["distance"] < temp_location["distance"]:
search_results.append(temp_location)
else:
search_results.insert(0, temp_location)
# Organizes the search list so that the current floor results are first
i = 0
place_at_end = []
curr_search_results = results["search_results"]
while i < len(curr_search_results):
floor_number = curr_search_results[i]["floor_number"]
if floor_number != floor:
place_at_end.append(curr_search_results[i])
curr_search_results.pop(i)
else:
i += 1
results["search_results"] = curr_search_results + place_at_end
else:
for row in teacher_data:
teacher = row[0]
search_results = results["search_results"]
room_value = ""
if teacher.lower() != "teacher" and teacher[:len(search_value)].lower() == search_value.lower():
if row[period] != "" and period != 0:
room_value = row[period]
else:
room_value = "Not Available"
search_results.append({"teacher": teacher, "room": room_value})
return json.dumps(results)
@app.route('/directions', methods=['POST'])
def generate_directions():
output = request.get_json()
site_root = os.path.realpath(os.path.dirname(__file__))
location_json_url = os.path.join(site_root, "static", "locations.json")
teachers_csv_url = os.path.join(site_root, "static", "teachers.csv")
node_map_json_url = os.path.join(site_root, "static", "node_map.json")
location_data = json.load(open(location_json_url))
teacher_data = csv.reader(open(teachers_csv_url))
node_map = json.load(open(node_map_json_url))
map_nodes_list = node_map["map_nodes"].copy()
directions = {"destination": None, "directions": []}
rooms = location_data["rooms"]
search_filter = output["search_type"]
room_value = str(output["room_value"])
period = output["current_period"]
latitude = output["current_latitude"]
longitude = output["current_longitude"]
floor = output["current_floor"]
mobility_accommodations = output["mobility_accommodations"]
"""
# Testing Variables
search_filter = "room_number"
room_value = "2728"
period = 1
latitude = 39.142784987
longitude = -77.419350719
floor = 1
mobility_accommodations = False
"""
# Gets the destination room based on the search filter
room_found = False
if search_filter == "teacher_name":
for row in teacher_data:
teacher = row[0]
if teacher.lower() != "teacher" and teacher.lower() == room_value.lower():
if row[period] != "" and period != 0:
directions["destination"] = row[period]
room_value = row[period]
room_found = True
else:
if search_filter == "room_number":
first_word_room_data = [str(room["room_value"]).split(" ")[0] for room in rooms]
actual_room_data = [str(room["room_value"]).lower()for room in rooms]
if room_value.lower() in first_word_room_data:
first_word_room_data_index = first_word_room_data.index(room_value)
directions["destination"] = actual_room_data[first_word_room_data_index]
room_value = actual_room_data[first_word_room_data_index]
room_found = True
else:
room_data = [str(room["room_value"]).lower() for room in rooms]
if room_value.lower() in room_data:
directions["destination"] = room_value
room_found = True
print(directions["destination"])
if not room_found:
return directions
else:
try:
# Changes the node map options available depending on whether the user has a mobility accommodation
map_nodes = node_map["map_nodes"]
path_endpoints = location_data["path_endpoints"]
i = 0
while i < len(map_nodes):
node = map_nodes[i]
name = str(node["room_name"]).lower()
if not mobility_accommodations:
if "ramp" in name or "elevator" in name:
map_nodes.pop(i)
else:
paths = node["paths"]
j = 0
while j < len(paths):
path_name = str(paths[j]["target_name"]).lower()
if "ramp" in path_name or "elevator" in path_name:
paths.pop(j)
else:
j += 1
i += 1
else:
if "stairs" in name and "intersection" not in name:
map_nodes.pop(i)
else:
paths = node["paths"]
j = 0
while j < len(paths):
path_name = str(paths[j]["target_name"]).lower()
if "stairs" in path_name and "intersection" not in path_name:
paths.pop(j)
else:
j += 1
i += 1
# Will also change the path endpoints which will be used for later
k = 0
while k < len(path_endpoints):
endpoint_room = str(path_endpoints[k]["room_value"]).lower()
if not mobility_accommodations:
if "ramp" in endpoint_room or "elevator" in endpoint_room:
path_endpoints.pop(k)
else:
k += 1
else:
if "stairs" in endpoint_room:
path_endpoints.pop(k)
else:
k += 1
# Omits this certain pathway since it contains stairs
extra_removal = [["ISP Hub Hallway Intersection Odd Rooms Side", 1518], ["ISP Hub", "Cafeteria"]]
if mobility_accommodations:
node_names = [map_node["room_name"] for map_node in map_nodes]
for removal in extra_removal:
first_removal_index = node_names.index(removal[1])
second_removal_index = node_names.index(removal[0])
first_removal_paths = map_nodes[first_removal_index]["paths"]
second_removal_paths = map_nodes[second_removal_index]["paths"]
first_removal_path_name = [first_removal_path["target_name"] for first_removal_path in first_removal_paths]
second_removal_path_name = [second_removal_path["target_name"] for second_removal_path in second_removal_paths]
first_removal_path_index = first_removal_path_name.index(removal[0])
second_removal_path_index = second_removal_path_name.index(removal[1])
first_removal_paths.pop(first_removal_path_index)
second_removal_paths.pop(second_removal_path_index)
# Gets the closest location to the current one
minimum_distance = math.inf
start = "0"
path_intersections = location_data["path_intersections"]
room_points = location_data["rooms"]
all_location_points = path_intersections + path_endpoints + room_points
for location_point in all_location_points:
location_point_latitude = location_point["latitude"]
location_point_longitude = location_point["longitude"]
location_point_floor = location_point["floor_number"]
distance = math.sqrt(((location_point_latitude - latitude) ** 2) + ((location_point_longitude - longitude) ** 2))
if distance < minimum_distance and location_point_floor == floor:
minimum_distance = distance
start = str(location_point["room_value"]).lower()
destination = str(room_value).lower()
room_names = [str(room_point["room_value"]).lower() for room_point in room_points]
destination_index = room_names.index(destination)
destination_item = room_points[destination_index]
destination_floor = destination_item["floor_number"]
route = [[start], [destination]]
# If the floor is not equivalent to the destination floor, it is split up so that it gets directions for each floor separately
if floor != destination_floor:
min_distance_to_destination = math.inf
first_endpoint = None
for path_endpoint in path_endpoints:
if path_endpoint["floor_number"] == floor and "ramp" not in str(path_endpoint["room_value"]).lower():
path_endpoint_latitude = path_endpoint["latitude"]
path_endpoint_longitude = path_endpoint["longitude"]
distance_to_destination = math.sqrt(((latitude - path_endpoint_latitude) ** 2) + ((longitude - path_endpoint_longitude) ** 2))
if distance_to_destination < min_distance_to_destination:
min_distance_to_destination = distance_to_destination
first_endpoint = path_endpoint["room_value"]
floor_transitions = node_map["floor_transitions"]
for floor_transition in floor_transitions:
if first_endpoint in floor_transition:
floor_transition_index = floor_transitions.index(floor_transition)
if floor == 1:
route[0].append(str(floor_transitions[floor_transition_index][1]).lower())
route[1].insert(0, str(floor_transitions[floor_transition_index][0]).lower())
else:
route[0].append(str(floor_transitions[floor_transition_index][0]).lower())
route[1].insert(0, str(floor_transitions[floor_transition_index][1]).lower())
print(route)
unseen_nodes_2 = node_map["map_nodes"].copy()
final_track_path = []
# Modified Djikstra's Algorithm
for i in range(len(route[0])):
shortest_distance = {}
track_predecessor = {}
directions["directions"] = []
unseen_nodes = node_map["map_nodes"]
track_path = []
infinity = math.inf
starting_point = route[0][i]
ending_point = route[1][i]
if i == 1:
unseen_nodes = unseen_nodes_2
for node in unseen_nodes:
shortest_distance[str(node["room_name"]).lower()] = infinity
shortest_distance[starting_point] = 0
current_room_values = [str(room["room_name"]).lower() for room in unseen_nodes]
start_index = current_room_values.index(starting_point.lower())
if i == 0:
directions["start_direction"] = unseen_nodes[start_index]["start_direction"]
while unseen_nodes:
room_values = [str(room["room_name"]).lower() for room in unseen_nodes]
min_distance_node = None
for node in unseen_nodes:
room_name = str(node["room_name"]).lower()
if min_distance_node is None:
min_distance_node = room_name
elif shortest_distance[room_name] < shortest_distance[min_distance_node]:
min_distance_node = room_name
min_distance_node_index = room_values.index(min_distance_node)
path_options = unseen_nodes[min_distance_node_index]["paths"]
for path in path_options:
child_node = str(path["target_name"]).lower()
weight = path["distance"]
if weight + shortest_distance[min_distance_node] < shortest_distance[child_node]:
shortest_distance[child_node] = weight + shortest_distance[min_distance_node]
track_predecessor[child_node] = min_distance_node
unseen_nodes.pop(room_values.index(min_distance_node))
current_node = ending_point
while current_node != starting_point:
track_path.insert(0, current_node)
current_node = track_predecessor[current_node]
track_path.insert(0, starting_point)
final_track_path += track_path
if shortest_distance[ending_point] != infinity:
print("Shortest distance is: " + str(shortest_distance[ending_point]))
print("Optimal path is: " + str(final_track_path))
for i in range(len(final_track_path)):
point_info = None
if i == 0:
point_info = {
"direction": "none",
"point_name": str(final_track_path[0])
}
elif ("stairs" in final_track_path[i - 1] and "stairs" in final_track_path[i] and not "intersection" in final_track_path[i - 1]) or ("elevator" in final_track_path[i - 1] and "elevator" in final_track_path[i]):
node_map_names = [str(path["room_name"]).lower() for path in map_nodes_list]
location_point_index = node_map_names.index(str(final_track_path[i]).lower())
point_info = {
"direction": map_nodes_list[location_point_index]["start_direction"],
"point_name": str(map_nodes_list[location_point_index]["room_name"])
}
else:
node_map_names = [str(path["room_name"]).lower() for path in map_nodes_list]
location_point_index = node_map_names.index(final_track_path[i - 1])
location_point = map_nodes_list[location_point_index]
path_targets = [str(point["target_name"]).lower() for point in location_point["paths"]]
path_target_index = path_targets.index(final_track_path[i])
target_info = location_point["paths"][path_target_index]
point_info = {
"direction": target_info["direction"],
"point_name": str(target_info["target_name"]),
"enter_perspective": target_info["enter_perspective"]
}
directions["directions"].append(point_info)
return directions
except ValueError:
return directions
except KeyError:
return directions
@app.route('/updateDirection', methods=['POST'])
def update_guide():
output = request.get_json()
landmark_points = output["landmark_points"]
current_latitude = output["current_latitude"]
current_longitude = output["current_longitude"]
current_floor = output["current_floor"]
site_root = os.path.realpath(os.path.dirname(__file__))
location_json_url = os.path.join(site_root, "static", "locations.json")
location_data = json.load(open(location_json_url))
all_location_data = location_data["path_intersections"] + location_data["path_endpoints"] + location_data["rooms"]
location_room_names = []
for location1 in all_location_data:
location_room_names.append(str(location1["room_value"]).lower())
"""
current_latitude = 39.142784987
current_longitude = -77.419350719
landmark_points = ['Main to Science Building Hallway Intersection', '1523', 'Stairs (2nd Floor Science Building Near Entrance)', '2523']
"""
minimum_distance = math.inf
max_distance_from_destination = 0.00005
closest_room = None
destination_reached = False
for landmark_point in landmark_points:
landmark_point_index = landmark_points.index(landmark_point)
location_index = location_room_names.index(str(landmark_point).lower())
location = all_location_data[location_index]
location_latitude = location["latitude"]
location_longitude = location["longitude"]
location_floor = location["floor_number"]
current_to_location_distance = math.sqrt(((location_latitude - current_latitude) ** 2) + ((location_longitude - current_longitude) ** 2))
if current_to_location_distance < minimum_distance and current_floor == location_floor:
closest_room = landmark_point
minimum_distance = current_to_location_distance
if landmark_point_index == len(landmark_points) - 1 and current_to_location_distance < max_distance_from_destination:
destination_reached = True
current_step = landmark_points.index(closest_room)
if destination_reached:
current_step += 1
color_directions = {"color_directions": []}
for i in range(len(landmark_points)):
if i >= current_step:
color_directions["color_directions"].append({"landmark_point": landmark_points[i], "color": "black"})
else:
color_directions["color_directions"].append({"landmark_point": landmark_points[i], "color": "gray"})
return color_directions
# Temp Functions
# Helps to auto collect the location and put it in the locations.json file
@app.route('/saveLocation', methods=['POST'])
def save_location():
output = request.get_json()
site_root = os.path.realpath(os.path.dirname(__file__))
location_json_url = os.path.join(site_root, "static", "locations.json")
location_data = json.load(open(location_json_url))
location_data["rooms"].append({"floor_number": 1, "room_value": output["room_value"], "latitude": output["current_latitude"], "longitude": output["current_longitude"]})
with open(location_json_url, "w") as f:
json.dump(location_data, f, indent=2)
return location_data
# Helps to auto calculate the distance between the current node to the target one
def calculate_distance():
site_root = os.path.realpath(os.path.dirname(__file__))
node_map_json_url = os.path.join(site_root, "static", "node_map.json")
location_json_url = os.path.join(site_root, "static", "locations.json")
location_data = json.load(open(location_json_url))
node_map = json.load(open(node_map_json_url))
path_intersections = [path_intersection["room_value"] for path_intersection in location_data["path_intersections"]]
path_endpoints = [path_endpoint["room_value"] for path_endpoint in location_data["path_endpoints"]]
rooms = [room["room_value"] for room in location_data["rooms"]]
for node in node_map["map_nodes"]:
room_name = node["room_name"]
latitude = 0
longitude = 0
if room_name in path_intersections:
path_intersection_index = path_intersections.index(room_name)
location = location_data["path_intersections"][path_intersection_index]
latitude = location["latitude"]
longitude = location["longitude"]
elif room_name in path_endpoints:
path_endpoints_index = path_endpoints.index(room_name)
location = location_data["path_endpoints"][path_endpoints_index]
latitude = location["latitude"]
longitude = location["longitude"]
else:
room_index = rooms.index(room_name)
location = location_data["rooms"][room_index]
latitude = location["latitude"]
longitude = location["longitude"]
for path in node["paths"]:
path_target_name = path["target_name"]
path_latitude = 0
path_longitude = 0
if path_target_name in path_intersections:
path_intersection_index = path_intersections.index(path_target_name)
location = location_data["path_intersections"][path_intersection_index]
path_latitude = location["latitude"]
path_longitude = location["longitude"]
elif path_target_name in path_endpoints:
path_endpoints_index = path_endpoints.index(path_target_name)
location = location_data["path_endpoints"][path_endpoints_index]
path_latitude = location["latitude"]
path_longitude = location["longitude"]
else:
room_index = rooms.index(path_target_name)
location = location_data["rooms"][room_index]
path_latitude = location["latitude"]
path_longitude = location["longitude"]
path["distance"] = math.sqrt(((path_latitude - latitude) ** 2) + ((path_longitude - longitude) ** 2))
with open("static/node_map.json", "w") as outfile:
json.dump(node_map, outfile, indent=2)
print("Updated node_map.json distances")
def check_node_map(floor):
site_root = os.path.realpath(os.path.dirname(__file__))
node_map_json_url = os.path.join(site_root, "static", "node_map.json")
location_json_url = os.path.join(site_root, "static", "locations.json")
location_data = json.load(open(location_json_url))
node_map = json.load(open(node_map_json_url))
points_forgotten = []
path_intersections = location_data["path_intersections"]
path_endpoints = location_data["path_endpoints"]
rooms = location_data["rooms"]
all_location_data = path_intersections + path_endpoints + rooms
map_nodes = node_map["map_nodes"]
for location in all_location_data:
if location["floor_number"] == floor:
room_value = location["room_value"]
room_found = False
for node in map_nodes:
if room_value == node["room_name"]:
room_found = True
if not room_found:
points_forgotten.append(room_value)
print("Here are the forgotten nodes: " + str(points_forgotten))
# Commented for execution purposes
# calculate_distance()
# generate_directions()
# check_node_map(1)
# check_node_map(2)
# update_guide()