API, what is it and why do I need it to get the shortest driving route between two locations

Hi folks
I thought it would be easy with just few lines of code to get the shortest driving route between two LAT,LON locations but it seems that I need some kind of API key for this.

I’m developing a Flask app in python for my company where employees should register their addresses and the code should return the driving distance between their homes and our company location.

Firstly I tried the module called geopy and Nominatim but they return only a flying distance (straight line) and don’t consider the roads. Geopy works very fast but distance isn’t correct.

Then I read about osmnx module. This is basically what I really want and the code is just 4 lines, like this:

G = ox.graph.graph_from_place(“Vestland, NORWAY”, network_type=‘drive’)
orig = ox.distance.nearest_nodes(G, X=5.462695, Y=60.433214)
dest = ox.distance.nearest_nodes(G, X=5.770288, Y=60.392021)
total=nx.shortest_path_length(G, orig, dest, weight=‘length’)

But to return the number of meters between those two locations takes about 10min. This is definitely not acceptable and I guess all our employees would “love” me for making them wait 10min before they register their forms. I also read that I can use a local map in osm extension instead of request server. So I downloaded the map area from openstreetmap and excluded everything in there but motorways used for driving using JOSM software. That method with local map file didn’t make much difference.

What I don’t really understand is why when I use online routing using OpenStreetMap I can get a distance between locations in the whole planet in just seconds but when I use a local file or a server request from just a small part of a country I need to wait minutes before the number is returned. Moreover I don’t really need to plot a map with the route shown on it.

What I’m looking for is some kind of python example how to return fast the distance between two locations given in LAT and LON within the Vestland region in Norway.

Thank you very much in advance. I hope someone can read it through and give me some tips.

Hi @ser-zhm,

take a look at our python SDK for requesting openrouteservice. There are also some code examples.
You should be able to do this pretty easily using either directions or matrix endpoint, depending on whether you actually would use the geometry.
Just be aware of your quotas and the additional restrictions that apply.

Best regards

2 Likes

Hey,

This is because the part that takes long is converting data from osm into a routable graph with weights and all.
Time your example-code, and you should see the first line taking way longer than the other three combined, if my guess is correct.

Best regards

1 Like

I have a similar problem, which I need to have a graph to find the nearest node first:

*# Fetch OSM street network from the location*
  • G = ox.graph_from_place(‘Taiwan’, network_type=“drive”, simplify=True)*

  • origin_node = ox.distance.nearest_nodes(G, startlong, startlat)*

  • destin_node = ox.distance.nearest_nodes(G, endlong, endlat)*

  • gnode01 = G.nodes[origin_node]*

  • gnode02 = G.nodes[destin_node]*

  • loc01 = [gnode01[‘x’], gnode01[‘y’]]*

  • loc02 = [gnode02[‘x’], gnode02[‘y’]]*

  • location_list = [loc01, loc02]*

  • profile = ‘driving-car’*

  • direction = router.directions(location_list, profile)*

Yes, it is time consuming to load the graph data.

Since it is impossible to load all the graph data if the scope of the route is huge, so I abandon the idea to use the graph data generated with osmnx and solely rely on ORS to find the end points for routing service.

*loc01 = [startlong, startlat]*
  • loc02 = [endlong, endlat]*

  • location_list = [loc01, loc02]*

  • profile = ‘driving-car’*

  • radiuses = [-1,-1]*

  • direction = router.directions(location_list, profile, radiuses=radiuses)*

Please note the radiuses is the radius range to find a end point of the route network.
[-1,-1] means infinite range to both the origin and desitination.

Let’s say, if given a number of [500,500] meters, it is sometimes possible not be able to find a feasible node and return an error message:

routingpy.exceptions.RouterApiError: 404 ({‘error’: {‘code’: 2010, ‘message’: ‘Could not find routable point within a radius of 5000.0 meters of specified coordinate 1: 139.3900000 35.2700000.’}, ‘info’: {‘engine’: {‘version’: ‘6.8.1’, ‘build_date’: ‘2023-05-22T14:27:28Z’}, ‘timestamp’: 1686318966056}})

It is infeasbile to load the graph for every routing service, so the 2nd solution ORS routing provided is much better.