import json import algorithms import store from lib import run_server, get, post, read_html grids = None nodes = None unconnected_nodes = None @get('/') def index(): global nodes global grids global unconnected_nodes nodes, grids, unconnected_nodes = store.extract_osm_nodes("linkoping.osm") return read_html('templates/index.html') @get('/favicon.ico') def favicon(): with open("data/favicon.ico", "rb") as image: read_image = image.read() image_bytes = bytearray(read_image) return image_bytes @post('/shortest-path') def shortest_path(body): body = json.loads(body) transport_mode = body['transport_mode'] relevant_grid = grids[transport_mode] relevant_nodes = nodes[transport_mode] source_id = algorithms.grid_search(relevant_grid, body['lat1'], body['lng1']) target_id = algorithms.grid_search(relevant_grid, body['lat2'], body['lng2']) path = algorithms.find_shortest_path(nodes[transport_mode], source_id, target_id) return json.dumps({"path": [(relevant_nodes[node].lat, relevant_nodes[node].lng) for node in path]}) run_server()