How to get the driving distance between two geographical coordinates using python?

Karthik Katragadda picture Karthik Katragadda · Mar 14, 2019 · Viewed 8.7k times · Source

I want to get the driving distance between two points in python. I am not allowed to use Google Maps API. Are there any open source APIs that help to calculate the driving distance between two points? If yes, a small example is Python will be amazing. Thanks in advance.

Answer

Joery picture Joery · Mar 14, 2019

Example from the python package OSMNX:

example doc -> routing

import networkx as nx
import osmnx as ox

# get the nearest network node to each point
orig_node = ox.get_nearest_node(G, (37.828903, -122.245846))
dest_node = ox.get_nearest_node(G, (37.812303, -122.215006))

# how long is our route in meters?
nx.shortest_path_length(G, orig_node, dest_node, weight='length')

Documentation on how to install: https://osmnx.readthedocs.io/en/stable/#installation

Note the following excerpt:

'If you are pip installing OSMnx, install geopandas and rtree first. It’s easiest to use conda-forge to get these dependencies installed.'