Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSRM giving wrong response for distance between 2 points

I am trying to get distance between two geo locations through projects-osrm. through python.

import requests
source_coordinates = '18.21231,42.50830;'
dest_coordinates = '18.20971,42.50075'
url =  'http://router.project-osrm.org/route/v1/driving/'+source_coordinates+dest_coordinates

payload = {"steps":"true","geometries":"geojson"}

response = requests.get(url,params=payload)

data = response.json()
print(data)

`
{
  "routes": [
    {
      "geometry": "oksbGmvonB??",
      "legs": [
        {
          "summary": "",
          "weight": 0,
          "duration": 0,
          "steps": [],
          "distance": 0
        }
      ],
      "weight_name": "routability",
      "weight": 0,
       "duration": 0,
      "distance": 0
  "code": "Ok"
}
`

As you can see i got distance as 0 as a response.

But when i am entering the same coordinates on to the site.

http://map.project-osrm.org/ and entering the same coordinates, i am getting 2.5km and 6 min.

Below is a snapshot: enter image description here

Can i please know why this would be happening and also is there any other way (opensource) to get distance and time between two places.

Thanks in advance

like image 487
Shubham R Avatar asked Jan 12 '18 06:01

Shubham R


1 Answers

Tying up all the different parts that came up in our discussion.

1. The main issue - giving 0 travel distance.

Current documentation for the API can be found here (for >V4.x). Note under "Requests" that coordinates are supplied in lon,lat order:

String of format {longitude},{latitude};{longitude},{latitude}[;{longitude},{latitude} ...] or polyline({polyline}) or polyline6({polyline6})

The current coordinates you are giving put both locations in the sea, assuming that they were in lon,lat pairing. Therefore, it is likely that you are instead supplying coordinates in the more natural lat,lon ordering. The web interface expects lat,lon so produces the output you expect. I honestly don't know why the API insists on lon,lat.

2. Other routing services

A list of the current routing software built on top of OSM data is found in their Wiki here. Of those, I've used Graphhopper and OSRM extensively, but none of the others. A few things I can say about the ones I've used:

  • OSRM is significantly faster than Graphhoper for large queries because it supports matrix calls while Graphhopper does not (in opensource format). For less than 300 queries/sec between location pairs, there is no difference.
  • Graphhopper comes with a front-end built in. OSRM front-end is a separate entity that requires JavaScript effort to integrate.
  • Graphhopper provides a cheap API service if you don't want to self-host.

3. Self-hosting.

The current API you are using for OSRM is subject to a fair-use policy. It's not meant for any significant work, just infrequent calls. You're also concerned that it isn't updated often enough. Both issues can be solved by self-hosting your own version of the software (Windows support was dropped for OSRM but you can build on a virtual machine and then bridge port 5000 so you can call the API from Windows, which is what I did).

  • Building OSRM instructions can be found here.
  • Instructions for extracting the map files can be found here
  • Geofabrik makes daily updates to the map files. You can download new map files whenever you want from here in .osm.pbf format.

    Side note for OSRM matrix calls. There is a debate here that I don't fully appreciate regarding the matrix API returning both the distance and time. If you need that functionality, you should load the table-distances branch of the forked project by niemeier-PSI here. I have never loaded a global map file, only on a per-country basis, so any memory concerns about this approach have been moot for me.

like image 154
roganjosh Avatar answered Oct 13 '22 01:10

roganjosh