# Initialising and Querying locally from the OpenSourceRoutingMachine (OSRM)¶

OSRM is an open source equivalent of Google maps and its distance API. Effectively, if you provide OSRM a street network (usually from OpenStreetMap, OSM) and a destination and origin pair, it will determine the fastest route and report the distance and time taken.
This can be done with either walking, biking, or driving and modifying the assumptions for different transport profiles is possible.

This instructions are similar to those provided here.

## 1. Directory structure¶

Create a directory structure, e.g. C:\osrm-data

I will refer to this directory location as XXX

## 2. Build Docker¶

### 2.1 Linux/Mac¶

1. Install docker, for linux this will be different

2. Open a terminal and run the following code

In [ ]:
docker build -t "cartography/osrm-backend-docker" https://github.com/cartography/osrm-backend-docker


This may require sudo as a prefix

This will build OSRM on your computer

### 2.2 Windows¶

Download my GitHub repo using the download zip option from my github. Unzip this and just copy the osrm folder (within code) into the directory of you created.

## 3. Download the street network¶

For this example we'll use a coordinate pair within the state of Delaware (because it is one of the smallest download files).

1. In your internet browser, navigate to http://download.geofabrik.de/
2. Then choose North America and download the .osm.pbf file for Michigan.
3. This will download a file called "michigan-latest.osm.pbf"

## 4. Profiles¶

Profiles refer to the transport mode and inform OSRM the speed someone travels over surfaces etc. and penalties for intersections.

You can download the general profiles from the OSRM github page and edit them as you like.

You do not need to download the profiles if you are using the Windows setup

## 5. Initialise OSRM¶

### 5.1 Mac/Linux¶

Run the following code (you may need to preface with sudo)

In [ ]:
docker run -p 5000:5000 --name osrm-api -v XXX/:/osrm-data/ -v XXX/foot.lua:/osrm-build/profile.lua cartography/osrm-backend-docker:latest osrm delaware-latest


The network will take a while to build, but the result should say "running and waiting for requests"

### 5.2 Windows (may not work as OSRM is still working on it)¶

The following code needs to be pasted into the Windows terminal

In [ ]:
XXX/osrm/osrm-extract.exe XXX/delaware-latest.osm.pbf -p XXX/osrm/profiles/foot.luab

In [ ]:
XXX/osrm/osrm-contract.exe XXX/delaware-latest.osrm

In [ ]:
XXX/osrm//osrm-routed.exe XXX/delaware-latest.osrm --port 5000


The network will take a while to build, but the result should say "running and waiting for requests"

## 6. Test¶

Effectively you're done. You've set up OSRM on your computer and now it is waiting for queries.

To test, try pasting the following into your browser url

In [ ]:
http://localhost:5000/route/v1/walking/-75.556521,39.746364;-75.545551,39.747228?overview=false


Note that this is the url of the server, the mode, the lon & lat of the origin ; then the lon & lat of the destination

To run this with a large number of queries you need to generate wrapper code and a set of origin-destination points to run. Examples of such code can be found here:

1. Andrew Nisbet's github - a summary of his work (Python)
2. My github - the link to our paper (pending) - a link to a website explaining it (R)
3. Logan Noel's code as part of UChicago's GeoDa work (Python)
Created with Jupyter, by Tom Logan.