Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does new osmnx 'nearest_nodes' function return different results than old function 'get_nearest_node'?

Tags:

python

osmnx

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?

like image 513
katherinejohnson Avatar asked Aug 13 '26 09:08

katherinejohnson


1 Answers

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)
like image 179
Brylie Christopher Oxley Avatar answered Aug 16 '26 07:08

Brylie Christopher Oxley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!