diff options
Diffstat (limited to 'server.py')
| -rw-r--r-- | server.py | 33 |
1 files changed, 26 insertions, 7 deletions
@@ -23,20 +23,39 @@ def show_area(): return json.dumps(rect) +def color_edges(nodes, colorer): + """ Color all edges in a list of nodes with a coloring function. """ + return [{"path": [[node0.lat, node0.lng], + [node1.lat, node1.lng]], + "color": colorer(colorer_t)} + for node0, node1, colorer_t in nodes] + + @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'])) - path, iterations = algorithms.find_shortest_path(nodes, source_id, target_id) - edges = [] - for i in range(len(path)-1): - edges.append({"path": [[nodes[path[i]].lat, nodes[path[i]].lng], - [nodes[path[i+1]].lat, nodes[path[i+1]].lng]], - "color": "#ff0000"}) + path, total_iters, edges = algorithms.find_shortest_path(nodes, source_id, target_id) + + def shortest_path_color(_): + """ Color edge as the shortest path. """ + return "#2674CA" + + def edge_color_iter(iters): + """ Color edge depending on how many iterations were taken to get there. """ + return algorithms.lerp_color("#ff0000", "#00cc00", iters/total_iters) + + shortest_path = color_edges([(nodes[path[i+0]], nodes[path[i+1]], None) + for i in range(len(path)-1)], + shortest_path_color) + + all_edges = color_edges([(nodes[node0], nodes[node1], iterations) + for node0, node1, iterations in edges.values()], + edge_color_iter) - return json.dumps({"edges": edges}) + return json.dumps({"edges": all_edges + shortest_path}) run_server() |
