summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--algorithms.py34
-rw-r--r--server.py27
-rw-r--r--store.py13
3 files changed, 56 insertions, 18 deletions
diff --git a/algorithms.py b/algorithms.py
index cc8d35f..6e01287 100644
--- a/algorithms.py
+++ b/algorithms.py
@@ -1,3 +1,4 @@
+import heapq
import math
@@ -23,7 +24,7 @@ def get_closest_node_id(nodes, source_node):
for node_id, node in nodes.items():
length = length_haversine(source_node, node)
- if min_node == None or length < min_value:
+ if min_node is None or length < min_value:
min_node = node_id
min_value = length
@@ -32,4 +33,33 @@ def get_closest_node_id(nodes, source_node):
def find_shortest_path(nodes, source_id, target_id):
""" Return the shortest path using Dijkstra's algortihm. """
- return []
+ # 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
+ # and walk_dist is the total length of the walk in meters
+ queue = [(0, (source_id,))]
+ visited = set()
+
+ while queue:
+ # consider an unchecked walk
+ walk_dist, walk = heapq.heappop(queue)
+ walk_end = walk[-1]
+ if walk_end == target_id:
+ # you have reached your destination
+ return walk
+ if walk_end in visited:
+ # there exists a shorter walk to walk_end
+ continue
+ # otherwise this is the shortest walk to walk_end
+ visited.add(walk_end)
+ # consider all our neighbours
+ for neighbour in nodes[walk_end].neighbours:
+ if neighbour in visited:
+ # there exists a shorter walk to neighbour
+ continue
+ # otherwise this MIGHT be the shortest walk to neighbour
+ # so put it in the queue
+ new_dist = walk_dist + length_haversine(nodes[walk_end], neighbour)
+ new_walk = walk + (neighbour.id,)
+ heapq.heappush(queue, (new_dist, new_walk))
+ # no path found
+ return None
diff --git a/server.py b/server.py
index e6ff6c2..6a93018 100644
--- a/server.py
+++ b/server.py
@@ -5,29 +5,34 @@ import store
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():
- all = dict()
- for (k, node) in enumerate(store.select_nodes_in_rectangle(store.extract_osm_nodes("university.osm"), 58.3984, 58.3990, 15.5733, 15.576)):
- all[node.id] = node.coord_tuple()
- return json.dumps(all)
+ 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(store.nodes, store.Node(-1, body['lat1'], body['lng1']))
- target_id = algorithms.get_closest_node_id(store.nodes, store.Node(-1, body['lat2'], body['lng2']))
- print(source_id, target_id)
- source_node = store.nodes[source_id]
- target_node = store.nodes[target_id]
- path = [(source_node.lat, source_node.lng), (target_node.lat, target_node.lng)]
- response = {'path': path}
+ 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 = algorithms.find_shortest_path(nodes, source_id, target_id)
+ print(path)
+ response = {"path": [(nodes[node].lat, nodes[node].lng) for node in path]}
+
return json.dumps(response)
diff --git a/store.py b/store.py
index e03ae77..9e6d4f7 100644
--- a/store.py
+++ b/store.py
@@ -6,7 +6,7 @@ class Node:
self.id = id
self.lat = float(lat)
self.lng = float(lng)
- self.neighbors = []
+ self.neighbours = []
def coord_tuple(self):
@@ -14,7 +14,6 @@ class Node:
parser = None # Have a global reusable parser object
-nodes = None
def add_neighbours(nodes):
@@ -28,15 +27,14 @@ def add_neighbours(nodes):
node1 = road[i]
node2 = road[i + 1]
- nodes[node1].neighbors.append(nodes[node2])
- nodes[node2].neighbors.append(nodes[node1])
+ nodes[node1].neighbours.append(nodes[node2])
+ nodes[node2].neighbours.append(nodes[node1])
return nodes
def extract_osm_nodes(f_name):
global parser
- global nodes
parser = get_default_parser(f_name)
nodes = dict()
@@ -45,6 +43,11 @@ def extract_osm_nodes(f_name):
add_neighbours(nodes)
+ # remove nodes without neighbours
+ for node_id, node in nodes.copy().items():
+ if not node.neighbours:
+ del nodes[node_id]
+
return nodes