import json import algorithms import store import time from lib import run_server, get, post, read_html nodes = None @get('/') def index(): global nodes nodes = store.extract_osm_nodes("university.osm") return read_html('templates/index.html') @get('/show-area') def show_area(): rect = dict() for (k, node) in enumerate(store.select_nodes_in_rectangle(nodes, 58.3984, 58.3990, 15.5733, 15.576)): rect[node.id] = node.coord_tuple() return json.dumps(rect) @post('/shortest-path') def shortest_path(body): body = json.loads(body) source_id = algorithms.get_closest_node_id(nodes, store.Node(-1, body['lat1'], body['lng1'])) target_id = algorithms.get_closest_node_id(nodes, store.Node(-1, body['lat2'], body['lng2'])) start = time.time() astar = algorithms.find_shortest_path_astar(nodes, source_id, target_id) stop = time.time() print(f"A* {stop-start:.4f}s {algorithms.path_length(nodes, astar)}") start = time.time() dijkstra = algorithms.find_shortest_path_dijkstra(nodes, source_id, target_id) stop = time.time() print(f"Dij {stop-start:.4f}s {algorithms.path_length(nodes, dijkstra)}") response = {"path": [(nodes[node].lat, nodes[node].lng) for node in astar]} return json.dumps(response) run_server()