summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Hansson <steha708@edu.liu.se>2020-12-16 08:06:10 +0100
committerNewbyte <steha708@liu.se>2020-12-16 08:06:10 +0100
commita11a7b4cde5834781aa57e7f9c9a8a3a309d6411 (patch)
tree3de6e7616a9f2db627ad80870e68c8a77f24709e
parent7d31854216fd893e1805c6dd40517f329ee3e886 (diff)
downloadtdde25-a11a7b4cde5834781aa57e7f9c9a8a3a309d6411.tar.gz
wip
-rw-r--r--algorithms.py11
-rw-r--r--server.py42
-rw-r--r--store.py87
-rw-r--r--templates/index.js2
4 files changed, 78 insertions, 64 deletions
diff --git a/algorithms.py b/algorithms.py
index 855c69e..3bd9f5e 100644
--- a/algorithms.py
+++ b/algorithms.py
@@ -73,25 +73,18 @@ def get_closest_node(nodes, source_node):
Searches through all nodes in a specified grid and return node
closes to source node.
"""
-def get_closest_node_id(nodes, source_node, transport_mode):
- """ Search through all nodes and return the id of the node
- that is closest to 'source_node'. """
min_node = None
min_value = None
for node in nodes:
+ print(source_node)
print(node)
length = length_haversine(source_node, node)
- relevant_neighbours = get_relevant_neighbours(node, transport_mode)
-
- if (min_node is None or length < min_value) and relevant_neighbours:
+ if (min_node is None or length < min_value):
min_node = node
min_value = length
- print("min_node: ")
- print(min_node)
-
return min_node
diff --git a/server.py b/server.py
index b928400..5992aa2 100644
--- a/server.py
+++ b/server.py
@@ -4,17 +4,17 @@ import algorithms
import store
from lib import run_server, get, post, read_html
-grid = None
+grids = None
nodes = None
unconnected_nodes = None
@get('/')
def index():
global nodes
- global grid
+ global grids
global unconnected_nodes
- nodes, grid, unconnected_nodes = store.extract_osm_nodes("university.osm")
+ nodes, grids, unconnected_nodes = store.extract_osm_nodes("university.osm")
return read_html('templates/index.html')
@@ -22,9 +22,10 @@ def index():
@get('/show-area')
def show_area():
rect = dict()
+ # FIXME: Don't hardcode bikes maybe? Maybe just remove this altogether
for (k, node) in enumerate(
- store.select_nodes_in_rectangle(nodes, 58.3984, 58.3990, 15.5733,
- 15.576)):
+ store.select_nodes_in_rectangle(nodes['bike'], 58.3984, 58.3990,
+ 15.5733, 15.576)):
rect[node.id] = node.coord_tuple()
return json.dumps(rect)
@@ -41,33 +42,24 @@ def favicon():
@get('/show-unconnected-nodes')
def show_unconnected_nodes():
print(f"Showing {len(unconnected_nodes)} unconnected nodes")
- return json.dumps({node.id: node.coord_tuple() for node in unconnected_nodes})
+ return json.dumps({
+ node.id: node.coord_tuple() for node in unconnected_nodes['bike']
+ })
@post('/shortest-path')
def shortest_path(body):
body = json.loads(body)
-<<<<<<< HEAD
- source_id = algorithms.grid_search(grid,
- store.Node(-1, body['lat1'],
- body['lng1']))
- target_id = algorithms.grid_search(grid,
- store.Node(-1, body['lat2'],
- body['lng2']))
-=======
transport_mode = body['transport_mode']
- source_id = algorithms.get_closest_node_id(nodes,
- store.Node(-1, body['lat1'],
- body['lng1']),
- transport_mode)
- target_id = algorithms.get_closest_node_id(nodes,
- store.Node(-1, body['lat2'],
- body['lng2']),
- transport_mode)
->>>>>>> 532c0cb... wip
-
- path = algorithms.find_shortest_path(nodes, source_id, target_id, 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']))
+
+ path = algorithms.find_shortest_path(nodes[transport_mode], source_id, target_id)
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 e877eda..4821072 100644
--- a/store.py
+++ b/store.py
@@ -7,8 +7,7 @@ class Node:
self.id = id
self.lat = float(lat)
self.lng = float(lng)
- self.neighbours_bike = []
- self.neighbours_car = []
+ self.neighbours = []
self.parent = None
self.size = 1
@@ -46,7 +45,7 @@ class Node:
parser = None # Have a global reusable parser object
-def add_neighbours_bike(nodes):
+def add_neighbours(nodes):
for way in parser.iter_ways():
if 'highway' not in way['tags']:
continue
@@ -57,47 +56,69 @@ def add_neighbours_bike(nodes):
node1 = road[i]
node2 = road[i + 1]
-<<<<<<< HEAD
- nodes[node1].neighbours.append(nodes[node2])
- nodes[node2].neighbours.append(nodes[node1])
- # These two are neighbours and should be part of the same tree.
- nodes[node1].union(nodes[node2])
-=======
- if suitable_car(way):
- nodes[node1].neighbours_car.append(nodes[node2])
- nodes[node2].neighbours_car.append(nodes[node1])
-
if suitable_bike(way):
- nodes[node1].neighbours_bike.append(nodes[node2])
- nodes[node2].neighbours_bike.append(nodes[node1])
->>>>>>> 532c0cb... wip
+ bike_nodes = nodes['bike']
- return nodes
+ bike_nodes[node1].neighbours.append(bike_nodes[node2])
+ bike_nodes[node2].neighbours.append(bike_nodes[node1])
+ # These two are neighbours and should be part of the same tree.
+ bike_nodes[node1].union(bike_nodes[node2])
-def add_neighbours_car(nodes):
- for way in parser.iter_ways():
- if 'highway' not in way['tags']:
- continue
+ if suitable_car(way):
+ car_nodes = nodes['car']
+
+ car_nodes[node1].neighbours.append(car_nodes[node2])
+ car_nodes[node2].neighbours.append(car_nodes[node1])
+
+ # These two are neighbours and should be part of the same tree.
+ car_nodes[node1].union(car_nodes[node2])
+
+ return nodes
def extract_osm_nodes(f_name):
global parser
parser = get_default_parser(f_name)
- nodes = dict()
- grid = defaultdict()
+ nodes = {
+ 'bike': dict(),
+ 'car': dict()
+ }
for node in parser.iter_nodes():
- new_node = Node(node['id'], node['lat'], node['lon'])
- nodes[node['id']] = new_node
+ # FIXME: this can probably be solved better
+ nodes['bike'][node['id']] = Node(node['id'], node['lat'], node['lon'])
+ nodes['car'][node['id']] = Node(node['id'], node['lat'], node['lon'])
add_neighbours(nodes)
+ # FIXME: this can probably be solved better
# Remove nodes without neighbours
- for node_id, node in nodes.copy().items():
- if not node.neighbours_car or not node.neighbours_bike:
- del nodes[node_id]
+ for node_id, node in nodes['bike'].copy().items():
+ if not node.neighbours:
+ del nodes['bike'][node_id]
+ for node_id, node in nodes['car'].copy().items():
+ if not node.neighbours:
+ del nodes['car'][node_id]
+
+ nodes['bike'], unconnected_bike = make_forest(nodes['bike'])
+ nodes['car'], unconnected_car = make_forest(nodes['car'])
+
+ unconnected_nodes = {
+ 'bike': unconnected_bike,
+ 'car': unconnected_car
+ }
+
+ grids = {
+ 'bike': make_grid(nodes['bike']),
+ 'car': make_grid(nodes['car'])
+ }
+
+ return nodes, grids, unconnected_nodes
+
+
+def make_forest(nodes):
# Construct a forest of disjoint trees.
# The forest is a { root: tree }-dict. If we construct a relation where
# two nodes relate to each other if they are a part of the same tree,
@@ -106,6 +127,7 @@ def extract_osm_nodes(f_name):
# becomes the root of each node since it will be the same for all nodes
# in the same tree due our use of union-find.
forest = defaultdict(dict)
+
for node_id, node in nodes.items():
forest[node.find_root().id][node_id] = node
@@ -129,6 +151,13 @@ def extract_osm_nodes(f_name):
unconnected_nodes.append(node)
nodes = best_tree
+
+ return nodes, unconnected_nodes
+
+
+def make_grid(nodes):
+ grid = defaultdict()
+
# create a "grid" by grouping nearby nodes.
for node in nodes.copy().values():
@@ -141,7 +170,7 @@ def extract_osm_nodes(f_name):
else:
grid[key] = [node]
- return nodes, grid, unconnected_nodes
+ return grid
def select_nodes_in_rectangle(nodes, min_lat, max_lat, min_long, max_long):
diff --git a/templates/index.js b/templates/index.js
index 42f8398..1422fe6 100644
--- a/templates/index.js
+++ b/templates/index.js
@@ -69,4 +69,4 @@ async function showUnconnectedMarkers() {
var marker = L.marker(res[key]).addTo(map)
}
}
-showUnconnectedMarkers()
+//showUnconnectedMarkers()