def test_nearest_node_not_found(): # These are three points with no nodes in a 100 meter vicinity. vicinity = 100 nodes_not_found = [(42.39801801801802, -72.50198198198198), (42.425045045045046, -72.47495495495495), (42.605225225225226, -72.29477477477477)] for point in nodes_not_found: lat, lon = point node = nearest_node(credentials, lat=lat, lon=lon, meters=vicinity) assert (node is None)
def time_nearest_node_not_found(): nodes_not_found = [(42.39801801801802, -72.50198198198198), (42.425045045045046, -72.47495495495495), (42.605225225225226, -72.29477477477477)] for point in nodes_not_found: lat, lon = point start = time.time() nodes = nearest_node(credentials, lat=lat, lon=lon) end = time.time() print("searching for", point, "took %f sec" % (end - start))
def test_nearest_node(): """ Test if a nearest node can be found """ sample_lat, sample_lon = 42.299944, -72.588062 nn = nearest_node(credentials, lat=sample_lat, lon=sample_lon, meters=100) # make sure we get the right return type assert (type(nn) == tuple) # make sure each node is a row with (id, lat, lon) assert (len(nn) == 3) # make sure we are within .1 degree of latitude and longitude assert (abs(float(nn[1]) - sample_lat) < .1 and abs(float(nn[2]) - sample_lon) < .1)
def time_nearest_node_queries(): amherst = ('amherst', 42.375582, -72.519629) boston = ('boston', 42.356417, -71.067728) shutesbury = ('shutesbury', 42.454855, -72.410792) # Test different towns (sparse, medium, dense) for point in [shutesbury, amherst, boston]: # Unpack values name, lat, lon = point start = time.time() nodes = nearest_node(credentials, lat=lat, lon=lon) end = time.time() print("%s: nearest node found in %f sec" % (name, end - start))
def time_random_nearest_nodes(): x_dim = 100 y_dim = 50 count = 0 for i in range(x_dim): for j in range(y_dim): lon = -73.2 + (-71.5 - -73.2) * 1.0 * i /x_dim lat = 42.1 + (42.6 - 42.1) * 1.0 * j /y_dim count += 1 start = time.time() nodes = nearest_node(credentials, lat=lat, lon=lon) end = time.time() print("%i, %f, %f, %f" % (count, lat, lon, end - start))
def optimize(A, B, preferences, debug=False): """ Main driver of the graph optimization. @input A: tuple of floats - (lat, lon) B: tuple of floats - (lat, lon) preferences: tuple of floats - (flatness_val, bicycle_val, distance_val, motorway_val, highway_val, residential_val) @return route - an ordered list of nodes [(lat, lon), (lat, lon)] from A to B """ # Unpack values A_lat, A_lon = A B_lat, B_lon = B # Validate input parameters if not valid_point(A_lat, A_lon): print("Invalid start point", A) return [] if not valid_point(B_lat, B_lon): print("Invalid end point", B) return [] if any([preference < 0 or preference > 100 for preference in preferences]): print("Invalid preferences", preferences) return [] AB_dist = dist_2d(A, B) if AB_dist > MAX_DISTANCE: print("Start and end points are further than maximum allowed distance", AB_dist, "meters", MAX_DISTANCE, "allowed.") return [] # Find nearest nodes in our database to A and B s = nearest_node(credentials, lat=A_lat, lon=A_lon) if s is None: print("No node found near", A) return [] else: s_id, s_lat, s_lon = s t = nearest_node(credentials, lat=B_lat, lon=B_lon) if t is None: print("No node found near", B) return [] else: t_id, t_lat, t_lon = t # Calculate midpoint m_lat, m_lon = midpoint((s_lat, s_lon), (t_lat, t_lon)) # Calculate search radius r = search_radius((s_lat, s_lon), (t_lat, t_lon)) # Get data from search radius edges_to_search = edges_within_radius(credentials, lat=m_lat, lon=m_lon, radius=r) # Print debug messages, if specified. if debug: print("Graph will be created with ", len(edges_to_search), "edges") print("radius:", r) print("midpoint:", m_lat, m_lon) print("s:", s) print("t:", t) print("AB_dist:", AB_dist) print("st_dist:", dist_2d((s_lat, s_lon), (t_lat, t_lon))) #print("sample edges:", edges_to_search[:10]) print("preferences:", preferences) # TODO: build the graph g, call g.dijkstra_path, and return the shortest path g = Graph() for way in edges_to_search: start_node_id = way[0] if (g.nodes.get(start_node_id) == None): start_node_lat = way[1] start_node_lon = way[2] g.add_node(Node( start_node_id, (start_node_lat, start_node_lon))) #add start node to graph end_node_id = way[3] if (g.nodes.get(end_node_id) == None): end_node_lat = way[4] end_node_lon = way[5] g.add_node( Node(end_node_id, (end_node_lat, end_node_lon ))) #add end node to graph, for closest_node() method distance = way[6] highway = way[7] bicycle = way[8] incline = way[9] g.add_edge(start_node_id, end_node_id, distance, highway, bicycle, incline, preferences) shortest_path = g.dijkstra_path(s_id, t_id) if debug: print("shortest_path found:") print(shortest_path) return shortest_path