OSRM giving wrong response for distance between 2 points
Asked Answered
J

1

7

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

July answered 12/1, 2018 at 6:54 Comment(6)
If I remember well, we can add that osrm will do a projection of your location on his closest known map. So if you swap lon-lat and get to the sea, it will project to the closest map and the two projected points might be really close.Integrity
@roganjosh Yes that helped.! swapped lat and long positions! :) Thanks. But still i think OSRM map data is not updated frequently as google maps.July
@user8150031 I actually found that quite frustrating with OSRM. In graphhopper, for example, it was easy to set a radius flag for bad lat/long data so that it didn't snap to a coastal road after a certain distance. OSRM seems more fault tolerant when I had hoped it would throw an error (I imagine there is a flag somewhere to set, but I had no success doing it). Eventually it does give up looking for a road though, and these locations were way out to seaExecutioner
@SRingne I'm not sure what you mean. Geofabrik has updated osm.pbf files on a daily basis. The map has nothing to do with OSRM if you are self-hosting, it depends only on how often to decide the process a new graph. But, OSM (which builds the maps) is crowd-sourced data so it's not going to be as complete as one of the world's biggest tech companies making a full effort. However, Google isn't free for any reasonable volume of requests. It depends what you want, unfortunately you can't have everything...Executioner
Hmm, ok, I see you're actually using their own site API to do the routing for you. Note that this service is for fair-use so don't be hammering it with requests or it will block you. It's very easy to self-host the service on one of your own instances and then you have full control on how often the maps are updated and can generate as many requests as you want.Executioner
@Executioner Any leads on how to self host? I'll be really helpful if i get steps or documentationJuly
E
8

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.

Executioner answered 17/1, 2018 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.