diff options
| author | Stefan Hansson <steha708@edu.liu.se> | 2020-12-16 09:20:12 +0100 |
|---|---|---|
| committer | Newbyte <steha708@liu.se> | 2020-12-16 09:20:12 +0100 |
| commit | dd50eb5818501eb45cdde745cf239ac12b13a04e (patch) | |
| tree | 1ff5da81c859b65b3883226a1507914c0ba4ec87 | |
| parent | a11a7b4cde5834781aa57e7f9c9a8a3a309d6411 (diff) | |
| download | tdde25-dd50eb5818501eb45cdde745cf239ac12b13a04e.tar.gz | |
Implement mode of transport
| -rw-r--r-- | algorithms.py | 8 | ||||
| -rw-r--r-- | server.py | 18 | ||||
| -rw-r--r-- | store.py | 1 |
3 files changed, 14 insertions, 13 deletions
diff --git a/algorithms.py b/algorithms.py index 3bd9f5e..df2f874 100644 --- a/algorithms.py +++ b/algorithms.py @@ -77,8 +77,6 @@ def get_closest_node(nodes, source_node): min_value = None for node in nodes: - print(source_node) - print(node) length = length_haversine(source_node, node) if (min_node is None or length < min_value): @@ -88,7 +86,7 @@ def get_closest_node(nodes, source_node): return min_node -def find_shortest_path(nodes, source_id, target_id, transport_mode: str): +def find_shortest_path(nodes, source_id, target_id): """ Return the shortest path using Dijkstra's algortihm. """ # queue contains multiple (walk_dist, (node_0, node_1, ... node_n))-tuples # where (node_0, node_1, ... node_n) is a walk to node_n @@ -112,9 +110,7 @@ def find_shortest_path(nodes, source_id, target_id, transport_mode: str): # consider all our neighbours end_node = nodes[walk_end] - relevant_neighbours = get_relevant_neighbours(end_node, transport_mode) - - for neighbour in relevant_neighbours: + for neighbour in end_node.neighbours: if neighbour in visited: # there exists a shorter walk to neighbour continue @@ -52,15 +52,19 @@ def shortest_path(body): body = json.loads(body) transport_mode = body['transport_mode'] - source_id = algorithms.get_closest_node(grids[transport_mode], - store.Node(-1, body['lat1'], - body['lng1'])) - target_id = algorithms.get_closest_node(grids[transport_mode], - store.Node(-1, body['lat2'], - body['lng2'])) + relevant_grid = grids[transport_mode] + + source_id = algorithms.grid_search(relevant_grid, + store.Node(-1, body['lat1'], + body['lng1'])) + target_id = algorithms.grid_search(relevant_grid, + store.Node(-1, body['lat2'], + body['lng2'])) + + relevant_nodes = nodes[transport_mode] path = algorithms.find_shortest_path(nodes[transport_mode], source_id, target_id) - response = {"path": [(nodes[node].lat, nodes[node].lng) for node in path]} + response = {"path": [(relevant_nodes[node].lat, relevant_nodes[node].lng) for node in path]} return json.dumps(response) @@ -56,6 +56,7 @@ def add_neighbours(nodes): node1 = road[i] node2 = road[i + 1] + # FIXME: lots of repeated code here if suitable_bike(way): bike_nodes = nodes['bike'] |
