Search code examples
openstreetmapgeopyoverpass-api

OSM get duration and directions from route A to B in Python


I am new to working with Maps and search algorithms. Currently I am using geopy package to get distances from Nominatim

from geopy.geocoders import Nominatim
from geopy.distance import vincenty

nom = Nominatim()
chicago = nom.geocode("chicago")
dallas = nom.geocode("dallas")
chicago_gps = (chicago.latitude, chicago.longitude)
dallas_gps = (dallas.latitude, dallas.longitude)
distance = vincenty(chicago_gps, dallas_gps).km
print('Distance in kms: {}'.format(distance))
print(chicago.raw)

output

Distance in kms: 1294.7623005649557
{'lat': '41.8755546', 'osm_id': '122604', 'boundingbox': ['41.643919', '42.0230219', '-87.940101', '-87.5239841'], 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', 'lon': '-87.6244212', 'place_id': '178038280', 'class': 'place', 'icon': 'https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png', 'osm_type': 'relation', 'importance': 0.29566190262222, 'display_name': 'Chicago, Cook County, Illinois, United States of America', 'type': 'city'}

So for each place I can calculate the distance. Now there are few questions

  1. Is it an airline distance ? Also does OSM provide duration of the journey like Google does ?
  2. How can I get directions if I want to go from "Chicago" to "Dallas" like google ? Is there way we get the routing directly from OSM apart from using APIs MapQuest etc ?
  3. How can we implement traffic layers in our model ? I need some good resources in that and if there are any python implementations of that it would be great.

Solution

  • Is it an airline distance?

    Yes, see the geopy documentation on distance calculation. geopy doesn't support real routing at the moment.

    Also does OSM provide duration of the journey like Google does?

    Yes it does if you use a real router. Take a look at OSM-based online routers. Several of them, such as GraphHopper and OSRM, provide turn-by-turn instructions.

    How can I get directions if I want to go from "Chicago" to "Dallas" like google ? Is there way we get the routing directly from OSM apart from using APIs MapQuest etc?

    See my previous answer. Use the API of one of the many online routers. Alternatively run your own routing instance. Many of these routers are open source and can be installed locally.

    How can we implement traffic layers in our model ? I need some good resources in that and if there are any python implementations of that it would be great.

    Can't help you with that. I would start by taking a look at http://opentraffic.io/ and https://github.com/graphhopper/open-traffic-collection.