5

The output of the following code

import osmnx as ox
import networkx as nx
import folium

G = ox.graph_from_place('Wuppertal, Germany', network_type='drive')

G = ox.speed.add_edge_speeds(G)
G = ox.speed.add_edge_travel_times(G)

orig = ox.get_nearest_node(G, (51.262336765,7.143472955))
print(orig)
dest = ox.get_nearest_node(G, (51.2521799,7.1491453))
print(dest)
orig = ox.nearest_nodes(G, X=51.262336765, Y=7.143472955)
print(orig)
dest = ox.nearest_nodes(G, X=51.2521799, Y=7.1491453)
print(dest)

is

279086833

269525495

317558105

317558105

Why does nearest_node return a result different from get_nearest_node? In particular, I have a problem with this, because when I plot the shortest route between orig and dest using get_nearest_node using the following code

route_map = ox.plot_route_folium(G, route)
route_map.save('test_1.html') 

everything works fine. When I try to do the same using nearest_nodes I get the error

Traceback (most recent call last):
  File "/Users/danielagaul/sciebo/PycharmProjects/distances/test_osm.py", line 20, in <module>
    route_map = ox.plot_route_folium(G, route)
  File "/Users/danielagaul/opt/anaconda3/envs/osm_new/lib/python3.8/site-packages/osmnx/folium.py", line 137, in plot_route_folium
    gdf_edges = utils_graph.graph_to_gdfs(G.subgraph(route), nodes=False).loc[uvk]
  File "/Users/danielagaul/opt/anaconda3/envs/osm_new/lib/python3.8/site-packages/osmnx/utils_graph.py", line 65, in graph_to_gdfs
    raise ValueError("graph contains no edges")
ValueError: graph contains no edges

How can I get the old results using the new function?

  • 5
    You seem to have swapped your x and y values when calling `nearest_nodes`, given typical latitudes and longitudes in Germany. – gboeing Sep 30 '21 at 15:20
  • 1
    @gboeing I used `get_nearest_node` but got **module 'osmnx' has no attribute 'get_nearest_node'**. It's an **AttributeError**. I got the latest version of osmnx. – AnonymousUser Jul 08 '22 at 05:37
  • So I used `nearest_nodes` instead and now it works. so X should be longitude and Y should be Latitude. – AnonymousUser Jul 08 '22 at 05:46

1 Answers1

1

Try using nearest_nodes with graph, X, and Y arguments:

orig = ox.nearest_nodes(G, 51.262336765, 7.143472955)
dest = ox.nearest_nodes(G, 51.2521799,7.1491453)
Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34