0

These few lines of code took almost 10minutes to return me a number of meters between two locations. But using the same coordinates in here takes less than 1 second. In my code I haven't even requested to plot the route on the graph, just needed a distance between two coordinates.

Can I achieve the same quick online response speed from the python code?

This code with 10min response:

    import osmnx as ox

G = ox.graph.graph_from_place("Vestland, NORWAY", network_type='drive')
558.1109199523926
A = ox.distance.nearest_nodes(G, 5.462695, 60.433214, return_dist=False)
1.176299810409546
B = ox.distance.nearest_nodes(G, 5.770288, 60.392021, return_dist=False)
0.1539440155029297
route = ox.distance.shortest_path(G, A, B, weight='length', cpus=None)
0.0059931278228759766
edge_lengths = ox.utils_graph.get_route_edge_attributes(G, route, "length")
0.0
total = round((sum(edge_lengths)))
0.0
print(total)

EDIT:

I inserted the time for executing each line of the code, see above

ser-zhm
  • 83
  • 1
  • 8
  • Please provide the timings from profiling your code. What is the runtime of each line? The answer lies therein. – gboeing May 25 '22 at 02:33
  • Does this answer your question? [Extremely long response time with graph\_from\_place OSMNX](https://stackoverflow.com/questions/69571863/extremely-long-response-time-with-graph-from-place-osmnx) – wovano May 25 '22 at 10:34
  • Yes, probably trade off the model precision would be the right choice for me. BTW how can filter the number of nodes from A to B to have them max 5? – ser-zhm May 25 '22 at 11:12

1 Answers1

0

G = ox.graph.graph_from_place("Vestland, NORWAY", network_type='drive')

This line of code generates a graph from OSM within the boundary of "Vestland, NORWAY". As it makes various API calls by dividing the whole area into 50km x 50km area and populate the graph, time taken may vary according to "area size". You can follow this answer to optimize your code.

micro5
  • 415
  • 3
  • 6