Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_routing_folium():
# calculate shortest path and plot as static image and leaflet web map
import networkx as nx
G = ox.graph_from_address('398 N. Sicily Pl., Chandler, Arizona', distance=800, network_type='drive')
origin = (33.307792, -111.894940)
destination = (33.312994, -111.894998)
origin_node = ox.get_nearest_node(G, origin)
destination_node = ox.get_nearest_node(G, destination, method='euclidean')
route = nx.shortest_path(G, origin_node, destination_node)
attributes = ox.get_route_edge_attributes(G, route, 'length')
fig, ax = ox.plot_graph_route(G, route, save=True, filename='route', file_format='png')
fig, ax = ox.plot_graph_route(G, route, origin_point=origin, destination_point=destination,
save=True, filename='route', file_format='png')
# test multiple routes
fig, ax = ox.plot_graph_routes(G, [route, route])
graph_map = ox.plot_graph_folium(G, popup_attribute='name')
route_map = ox.plot_route_folium(G, route)
def test_routing_folium():
# calculate shortest path and plot as static image and leaflet web map
import networkx as nx
G = ox.graph_from_address('398 N. Sicily Pl., Chandler, Arizona', distance=800, network_type='drive')
origin = (33.307792, -111.894940)
destination = (33.312994, -111.894998)
origin_node = ox.get_nearest_node(G, origin)
destination_node = ox.get_nearest_node(G, destination, method='euclidean')
route = nx.shortest_path(G, origin_node, destination_node)
attributes = ox.get_route_edge_attributes(G, route, 'length')
fig, ax = ox.plot_graph_route(G, route, save=True, filename='route', file_format='png')
fig, ax = ox.plot_graph_route(G, route, origin_point=origin, destination_point=destination,
save=True, filename='route', file_format='png')
# test multiple routes
fig, ax = ox.plot_graph_routes(G, [route, route])
graph_map = ox.plot_graph_folium(G, popup_attribute='name')
route_map = ox.plot_route_folium(G, route)
origin_x = origin['geometry']['coordinates'][0]
origin_y = origin['geometry']['coordinates'][1]
dest_x = destination['geometry']['coordinates'][0]
dest_y = destination['geometry']['coordinates'][1]
# Find shortest path between the two
point1_x, point1_y = transform(projUTM, projWGS84, origin_x, origin_y)
point2_x, point2_y = transform(projUTM, projWGS84, dest_x, dest_y)
# gotcha: osmnx needs (lng, lat)
point1 = (point1_y, point1_x)
point2 = (point2_y, point2_x)
# TODO improve by finding nearest edge, routing to/from node at either end
origin_node = ox.get_nearest_node(G, point1)
destination_node = ox.get_nearest_node(G, point2)
try:
if origin_node != destination_node:
# Find the shortest path over the network between these nodes
route = nx.shortest_path(G, origin_node, destination_node, weight='length')
# Retrieve route nodes and lookup geographical location
routeline = []
routeline.append((origin_x, origin_y))
for node in route:
routeline.append((transform(projWGS84, projUTM, G.nodes[node]['x'], G.nodes[node]['y'])))
routeline.append((dest_x, dest_y))
line = routeline
else:
line = [(origin_x, origin_y), (dest_x, dest_y)]
except nx.exception.NetworkXNoPath:
for origin in origins:
dest_x, dest_y = return_object_coordinates(destination)
origin_x, origin_y = return_object_coordinates(origin)
# Find shortest path between the two
point1_x, point1_y = transform(projUTM, projWGS84, origin_x, origin_y)
point2_x, point2_y = transform(projUTM, projWGS84, dest_x, dest_y)
# gotcha: osmnx needs (lng, lat)
point1 = (point1_y, point1_x)
point2 = (point2_y, point2_x)
# TODO improve by finding nearest edge, routing to/from node at either end
origin_node = ox.get_nearest_node(G, point1)
destination_node = ox.get_nearest_node(G, point2)
try:
if origin_node != destination_node:
# Find the shortest path over the network between these nodes
route = nx.shortest_path(G, origin_node, destination_node, weight='length')
# Retrieve route nodes and lookup geographical location
routeline = []
routeline.append((origin_x, origin_y))
for node in route:
routeline.append((transform(projWGS84, projUTM, G.nodes[node]['x'], G.nodes[node]['y'])))
routeline.append((dest_x, dest_y))
line = routeline
else:
line = [(origin_x, origin_y), (dest_x, dest_y)]
def snap_point_to_graph_node(point_x, point_y, G):
# osmnx nearest node method
node_id = ox.get_nearest_node(G, (point_x, point_y))
return (G.node[node_id]['x'], G.node[node_id]['y'])
def snap_point_to_graph_node(point_x, point_y, G):
# osmnx nearest node method
node_id = ox.get_nearest_node(G, (point_x, point_y))
return (G.node[node_id]['x'], G.node[node_id]['y'])
for origin in origins:
dest_x, dest_y = return_object_coordinates(destination)
origin_x, origin_y = return_object_coordinates(origin)
# Find shortest path between the two
point1_x, point1_y = transform(projUTM, projWGS84, origin_x, origin_y)
point2_x, point2_y = transform(projUTM, projWGS84, dest_x, dest_y)
# gotcha: osmnx needs (lng, lat)
point1 = (point1_y, point1_x)
point2 = (point2_y, point2_x)
# TODO improve by finding nearest edge, routing to/from node at either end
origin_node = ox.get_nearest_node(G, point1)
destination_node = ox.get_nearest_node(G, point2)
try:
if origin_node != destination_node:
# Find the shortest path over the network between these nodes
route = nx.shortest_path(G, origin_node, destination_node, weight='length')
# Retrieve route nodes and lookup geographical location
routeline = []
routeline.append((origin_x, origin_y))
for node in route:
routeline.append((transform(projWGS84, projUTM, G.nodes[node]['x'], G.nodes[node]['y'])))
routeline.append((dest_x, dest_y))
line = routeline
else:
line = [(origin_x, origin_y), (dest_x, dest_y)]
except nx.exception.NetworkXNoPath: