Here is my code:
import osmnx as ox
# Use the following commands to download the graph file of NYC
G = ox.graph_from_place('New York City', network_type='drive', simplify=True)
# Coordinates for origin and destination
orig_x = 40.6662
orig_y = -73.9340
dest_x = 40.6576
dest_y = -73.9208
# Find the nearest nodes
orig_node = ox.distance.nearest_nodes(G, orig_x, orig_y, return_dist=True)
dest_node = ox.distance.nearest_nodes(G, dest_x, dest_y, return_dist=True)
print(f"Origin node: {orig_node}, Destination node: {dest_node}")
# Calculate the shortest path
route = ox.shortest_path(G, orig_node, dest_node, weight='length')
travel_secs = sum(G[u][v][0]['length'] for u, v in zip(route[:-1], route[1:])) * 186.411821
print(f"Travel time (seconds): {travel_secs}")
I’m trying to find the shortest path between these two points but I get “networkx.exception.NodeNotFound: Either source 15056632.490169104 or target 15056625.267485507 is not in G”
Sorry if this is something obvious, I’m new to this library and haven’t found any good documentation on this issue.
New contributor
Thales Protopapas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.