summaryrefslogtreecommitdiffstats
path: root/algorithms.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2021-01-06 18:58:21 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2021-01-06 18:58:21 +0100
commita526d6b7ce8c2e506dfc0a6356e9e2ce470bc3e4 (patch)
tree0723e8b92e34371b130b1e1a579a872fad1bad6a /algorithms.py
parent62683f4cc52f2ff81719868e8f06b29af84d1269 (diff)
downloadtdde25-a526d6b7ce8c2e506dfc0a6356e9e2ce470bc3e4.tar.gz
reorder params, only take lat/lng as params
Diffstat (limited to 'algorithms.py')
-rw-r--r--algorithms.py31
1 files changed, 16 insertions, 15 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):