Open Source Routing Machine (OSRM) is the a C++ routing engine for shortest paths in road networks. It can be used for suggesting directions between two locations, which is what I am hoping to use it for. Its data source is OpenStreetMap and it can process the osm.pbf (protocol buffer files).

There are two pre-processing pipelines available:

  • Contraction Hierarchies (CH)
  • Multi-Level Dijkstra (MLD)

I’ve used the second one (MLD) due to having better success with that. The partition and customise step below are replaced with a single contract step using osrm-contract.

Another open source routing engine for OpenStreetMap is Valhalla. I had intended to use this one initially, however I stumbled upon more information for OSRM so went with that. This project also seems to do more than just routing such as elevation (which seems to be for biking/walking as it accounts for steepness during routing).

Data Preparation

  • Download software from Releases on GitHub. I used version 5.27.1 for Windows (node_osrm-v5.27.1-node-v93-win32-x64-Release.tar.gz)
  • Download a relevant OpenStreetMap Data Extract from Geofabrik. For my projects I downloaded:
  • Perform the extraction step
    D:\Programs\Development\osrm\osrm-extract.exe `
      --profile=D:\Programs\Development\osrm\profiles\car.lua `
      G:\GeoData\Source\OpenStreetMap\osm\australia-oceania-2022-10-22.osm.pbf
    
  • Perform the partition step
    D:\programs\development\osrm\osrm-partition.exe `
      "G:\GeoData\Source\OpenStreetMap\osm\australia-oceania-2022-10-22.osrm"
    
  • Perform the customise step
    D:\programs\development\osrm\osrm-customize.exe `
      "G:\GeoData\Source\OpenStreetMap\osm\australia-oceania-2022-10-22.osrm"
    
  • Start web server for hosting the data. For a given data set, this is all you need going forward.
    D:\Programs\Development\osrm\osrm-routed.exe --algorithm mld `
      "G:\GeoData\Source\OpenStreetMap\osm\australia-oceania-2022-10-22.osrm"
    

Important note is it doesn’t generate a ‘osrm’ file which means tab completion in a terminal is not perfect. It a shame that it doesn’t create a .osrm directory instead as that would benefit tab completion and make it clearer that is that folder.

Problems

The big problem I faced before using osrm-routed was I was trying to use the routing.cpp example. It kept coming back as no distance, which made me think I had mucked up the creation of the osrm files.

Results

For Australia, the source data was 605MB and the OSRM data was 2.05GB. For US North East, the source data was 1.33GB and the OSRM data was 6.51GB.

For the HTTP API, the coordinates are longitude then comma then latitude pairs with the pairs separated by a semicolon (;).

For testing your coordinates and comparing it against the demonstration system hosted by the OSRM project. Unfortunately, the web front-end takes coordinates in latitude than longitude rather than the other way around like the back-end.

In this example we try to compute the route between Queen’s Ct in Adelaide to Spencer Street, nearby.

http://127.0.0.1:5000/route/v1/driving/138.588700255592,-34.9255684682013;138.589492995401,-34.9266129382409?steps=true

Expected output:

{
    "waypoints": [{
        "name": "Queen's Court",
        "location": [138.588695, -34.925484],
        "distance": 9.330084896,
        "hint": "_-4OgALvDoAOAAAADQAAAAAAAAAAAAAA4DmFQZECXUEAAAAAAAAAAA4AAAANAAAAAAAAAAAAAAChEAAAF7JCCFQU6_0cskIIABTr_QAAvxCwtqXD"
    }, {
        "name": "Spencer Street",
        "location": [138.589497, -34.926675],
        "distance": 6.887933993,
        "hint": "3u0OgP___38VAAAARwAAAAAAAAAAAAAADCltQWyzCkIAAAAAAAAAABUAAABHAAAAAAAAAAAAAAChEAAAObVCCK0P6_01tUII6w_r_QAAvxCwtqXD"
    }],
    "routes": [ <omitted> ],
}

Same query on the demonstration front-end.

Next

The next stage of what I want to do with this is draw the path on a map.

As well as the OSRM Text Instructions, assuming that is the component that produces the navigation system-like text of the form:

  • Head north on King’s Court (55m)
  • Turn left onto Currie Street (100m)

Other uses

The library provides several other operations other than directions between two locations (a route). It also supports directions between multiple locations.

The other tasks it can perform are:

  • Snapping coordinates to the street network and returns the nearest matches.
  • Computing the duration or distances of the fastest route between all pairs of supplied coordinates.
  • Snapping noisy GPS traces to the road network in the most plausible way.
  • Solving the Travelling Salesman Problem using a greedy heuristic.
  • Generating Mapbox Vector Tiles with internal routing metadata.