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



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