From 1afff10fa0418647f35fdfc1ec95bff92e37ec11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 6 Jan 2021 18:51:34 +0100 Subject: remove demo-code --- server.py | 19 ------------------- templates/index.js | 23 ----------------------- 2 files changed, 42 deletions(-) diff --git a/server.py b/server.py index 8055e63..7790a61 100644 --- a/server.py +++ b/server.py @@ -19,17 +19,6 @@ def index(): return read_html('templates/index.html') -@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['bike'], 58.3984, 58.3990, - 15.5733, 15.576)): - rect[node.id] = node.coord_tuple() - return json.dumps(rect) - - @get('/favicon.ico') def favicon(): with open("data/favicon.ico", "rb") as image: @@ -39,14 +28,6 @@ def favicon(): return image_bytes -@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['bike'] - }) - - @post('/shortest-path') def shortest_path(body): body = json.loads(body) diff --git a/templates/index.js b/templates/index.js index 1422fe6..2fbab55 100644 --- a/templates/index.js +++ b/templates/index.js @@ -47,26 +47,3 @@ function handleMapClick ({latlng}){ } map.on('click', handleMapClick) - - -function showMarker(){ - var marker = L.marker([58.3984, 15.5733]).addTo(map) -} -showMarker() - -async function showMarkers() { - res = await fetch('/show-area'); - res = await res.json(); - for (let key in res) { - var marker = L.marker(res[key]).addTo(map) - } -} -showMarkers() - -async function showUnconnectedMarkers() { - const res = await fetch('/show-unconnected-nodes').then(res => res.json()); - for (let key in res) { - var marker = L.marker(res[key]).addTo(map) - } -} -//showUnconnectedMarkers() -- cgit v1.2.1 From b52a7325c1e0d1b6e87e2bbe96ebafcc22be167a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 6 Jan 2021 18:55:57 +0100 Subject: pep8 --- algorithms.py | 8 +------- lib.py | 2 +- osm_parser.py | 3 ++- server.py | 2 +- store.py | 4 +++- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/algorithms.py b/algorithms.py index 9f3fd78..e2da8f0 100644 --- a/algorithms.py +++ b/algorithms.py @@ -10,8 +10,7 @@ def length_haversine(p1, p2): lat1, lng1, lat2, lng2 = map(math.radians, [lat1, lng1, lat2, lng2]) dlat = lat2 - lat1 dlng = lng2 - lng1 - a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin( - dlng / 2) ** 2 + a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlng / 2) ** 2 c = 2 * math.asin(math.sqrt(a)) return 6372797.560856 * c # return the distance in meters @@ -33,7 +32,6 @@ def grid_search(grid, source_node): closest_nodes.append(get_closest_node(tile_nodes, source_node)) closest_node_id = get_closest_node(closest_nodes, source_node).id - return closest_node_id @@ -56,14 +54,10 @@ def look_for_nodes(offset, grid, source_key): tiles = [] for i in range(-offset, offset + 1): for j in range(-offset, offset + 1): - if i in (-offset, offset) or j in (-offset, offset): - key = (source_key[0] + j, source_key[1] + i) - if key in grid.keys(): tiles.append(grid[key]) - return tiles diff --git a/lib.py b/lib.py index 8a31276..d00d7ee 100644 --- a/lib.py +++ b/lib.py @@ -162,4 +162,4 @@ def run_server(port=8314): server_address = ('127.0.0.1', port) # Should make the port a command line argument server = HTTPServer(server_address, Handler) print('Starting server on http://{}:{}.'.format(*server_address)) - server.serve_forever() \ No newline at end of file + server.serve_forever() diff --git a/osm_parser.py b/osm_parser.py index 7145726..b3eed86 100644 --- a/osm_parser.py +++ b/osm_parser.py @@ -121,7 +121,8 @@ class OSMParser: for node in el.iter(OSM_WAYNODE)) tags = {tag.get(OSM_TAG_KEY): tag.get(OSM_TAG_VALUE) for tag in el.iter(OSM_TAG) - if self.allow_all or tag.get(OSM_TAG_KEY) in self.way_tags} + if self.allow_all + or tag.get(OSM_TAG_KEY) in self.way_tags} # Yield the edge id, the road and the tags yield { diff --git a/server.py b/server.py index 7790a61..8def0ba 100644 --- a/server.py +++ b/server.py @@ -8,12 +8,12 @@ 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') diff --git a/store.py b/store.py index 4bca5ee..5438740 100644 --- a/store.py +++ b/store.py @@ -189,6 +189,7 @@ suitable_highway_types_bike = [ 'path' ] + def suitable_bike(way): tags = way['tags'] @@ -213,8 +214,9 @@ suitable_highway_types_car = [ 'tertiary_link', # Special road types 'living_street', 'service', - 'pedestrian', 'road' # FIXME: Handle predestrian and road differently? + 'pedestrian', 'road' # FIXME: Handle predestrian and road differently? ] + def suitable_car(way): return way['tags']['highway'] in suitable_highway_types_car -- cgit v1.2.1 From 62683f4cc52f2ff81719868e8f06b29af84d1269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 6 Jan 2021 18:56:56 +0100 Subject: comments --- algorithms.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/algorithms.py b/algorithms.py index e2da8f0..5423398 100644 --- a/algorithms.py +++ b/algorithms.py @@ -3,6 +3,11 @@ import math def length_haversine(p1, p2): + """ + The distance in meters between two coordinates on a sphere. + + Assumes p1=(lat1, lng1) and p2=(lat2, lng2) in degrees. + """ lat1 = p1.lat lng1 = p1.lng lat2 = p2.lat @@ -42,6 +47,7 @@ def find_nodes(offset, grid, source_key): """ tiles = None + # search further out until we find nodes while not tiles: tiles = look_for_nodes(offset, grid, source_key) offset += 1 @@ -50,7 +56,7 @@ def find_nodes(offset, grid, source_key): def look_for_nodes(offset, grid, source_key): - """Search for nearest tile containing nodes in an outward spiral.""" + """Search for nearest tile containing nodes in a spiral.""" tiles = [] for i in range(-offset, offset + 1): for j in range(-offset, offset + 1): @@ -63,8 +69,8 @@ def look_for_nodes(offset, grid, source_key): def get_closest_node(nodes, source_node): """ - Searches through all nodes in a specified grid and return node - closes to source node. + Search through all nodes in a specified grid and return the node + closest to source_node. """ min_node = None min_value = None -- cgit v1.2.1 From a526d6b7ce8c2e506dfc0a6356e9e2ce470bc3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 6 Jan 2021 18:58:21 +0100 Subject: reorder params, only take lat/lng as params --- algorithms.py | 31 ++++++++++++++++--------------- server.py | 25 +++++++++++++------------ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/algorithms.py b/algorithms.py index 5423398..8c63968 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,5 +1,6 @@ import heapq import math +import store def length_haversine(p1, p2): @@ -21,26 +22,25 @@ def length_haversine(p1, p2): return 6372797.560856 * c # return the distance in meters -def grid_search(grid, source_node): +def grid_search(grid, lat, lng): """ Finds closest node to source node by comparing distance to nodes within nearest tiles in the grid. """ - closest_nodes = [] - - source_key = (int(round(source_node.lat, 3) * 1000), int(round( - source_node.lng, 3) * 1000)) - - closest_tiles = find_nodes(0, grid, source_key) - - for tile_nodes in closest_tiles: - closest_nodes.append(get_closest_node(tile_nodes, source_node)) - + source_node = store.Node(-1, lat, lng) + source_key = ( + int(round(source_node.lat, 3) * 1000), + int(round(source_node.lng, 3) * 1000), + ) + + closest_tiles = find_nodes(grid, source_key) + closest_nodes = [get_closest_node(tile_nodes, source_node) + for tile_nodes in closest_tiles] closest_node_id = get_closest_node(closest_nodes, source_node).id return closest_node_id -def find_nodes(offset, grid, source_key): +def find_nodes(grid, source_key, offset=0): """ Searches a grid in an outward "spiral" from source node fetching all tiles which contain nodes. @@ -49,13 +49,14 @@ def find_nodes(offset, grid, source_key): # search further out until we find nodes while not tiles: - tiles = look_for_nodes(offset, grid, source_key) + tiles = look_for_nodes(grid, source_key, offset) offset += 1 - return tiles + look_for_nodes(offset + 1, grid, source_key) + # include another layer since they might contain closer nodes + return tiles + look_for_nodes(grid, source_key, offset + 1) -def look_for_nodes(offset, grid, source_key): +def look_for_nodes(grid, source_key, offset): """Search for nearest tile containing nodes in a spiral.""" tiles = [] for i in range(-offset, offset + 1): diff --git a/server.py b/server.py index 8def0ba..33372ba 100644 --- a/server.py +++ b/server.py @@ -31,23 +31,24 @@ def favicon(): @post('/shortest-path') def shortest_path(body): body = json.loads(body) - transport_mode = body['transport_mode'] + transport_mode = body['transport_mode'] relevant_grid = grids[transport_mode] + relevant_nodes = nodes[transport_mode] source_id = algorithms.grid_search(relevant_grid, - store.Node(-1, body['lat1'], - body['lng1'])) + 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": [(relevant_nodes[node].lat, relevant_nodes[node].lng) for node in path]} - - return json.dumps(response) + 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() -- cgit v1.2.1