Osmnx and Networkx

Does anyone know how I can get this to work?

I am using osmnx and networkx to get the shortest path between two places

I first used the osmnx distance.nearest_nodes to get the nearest node to the longitude and latitude value but that kept giving me and error so I decided to remove it but that also does not work

I have no clue what to do

Here’s the link:


It’s hard to tell what it is without seeing the error…

What I can do for you is try to improve the call, for example:

import osmnx as ox
import networkx as nx

def fastest_path(start_coord, end_coord):
    # First load the graph
    G = ox.graph_from_place('Winchester', network_type='drive')
    # Get the nearest nodes to the provided coordinates
    origin_node = ox.distance.nearest_nodes(G, start_coord[1], start_coord[0])
    destination_node = ox.distance.nearest_nodes(G, end_coord[1], end_coord[0])
    # And calculate the shortest path
    path = nx.shortest_path(G, origin_node, destination_node, weight='length')
    return path

# After that you convert postcodes to coordinates
def get_coordinates(postcode):
    return ox.geocode(postcode)

start_postcode = input('Enter start postcode: ')
end_postcode = input('Enter end postcode: ')

start_coord = get_coordinates(start_postcode)
end_coord = get_coordinates(end_postcode)

fastest_path_result = fastest_path(start_coord, end_coord)

This is the error is shows every time. distance is also underlined red and replit says it is not a knows member of the module.

Enter start postcode: SO23 0PR
Enter end postcode: SO23 9NA
Traceback (most recent call last):
  File "/home/runner/GlitteringOrderlyPixels/main.py", line 24, in <module>
    fastest_path_result = fastest_path(start_coord, end_coord)
  File "/home/runner/GlitteringOrderlyPixels/main.py", line 8, in fastest_path
    origin_node = ox.distance.nearest_nodes(G, start_coord[1], start_coord[0])
  File "/home/runner/GlitteringOrderlyPixels/.pythonlibs/lib/python3.10/site-packages/osmnx/distance.py", line 293, in nearest_nodes
    raise ImportError(msg)
ImportError: scikit-learn must be installed to search an unprojected graph