I'm analysing the likely paths taken by commuters on their way to work, based on Census flow data, job density distributions and the transport network.

At the moment I'm using pgRouting and the pgRouting add-on for QGIS (described here) for my analysis:

routing in qgis

However, I'd like all the steps I take to be full replicable (see excellent article on this here), then I'd prefer to do my analysis from the command-line and save the script files for future reference.

R-geo packages fulfil this requirement well, as they are free, lightweight, and stable: download a tiny script file and you should be able to re-do all the things I have done (see here for an unrelated example of this on Rpubs).

The R packages I've identified for doing this are:

  • gdistance published December 2012, looks promising, but no sign of compatibility with osm transport networks
  • e1071 seems rather abstract and possibly overkill for what I want to do
  • pairdist.lpp function in spatstat package looks promising, but has little documentation

Before diving in to these, thought it worth asking people who may have faced similar dilemmas.

Is it possible to do my routing analysis in R, or would it be best to learn PostGIS properly and do it all in pgRouting?


Just to close this loose end, since I asked the question a new package was released called osmar which contains a vignette of how to implement shortest path algorithms in R using Open Street Map data: http://osmar.r-forge.r-project.org/ . It uses the function get.shortest.paths from the igraph package.

Excellent article on this can be found here: http://journal.r-project.org/archive/2013-1/eugster-schlesinger.pdf

And a tutorial by me can be found here: http://eprints.whiterose.ac.uk/77643/7/lovelace2.pdf


You may also use the r5rpackage for rapid realistic routing on multimodal transport networks. Once you download the OpenStreetMap as a .pbf file to your computer. You can easily run routing analysis with just a few lines of code. See detailed intro vignette on how to use r5r here.


# 1) build transport network, pointing to the path where OSM and GTFS data are stored
r5r_core <- setup_r5(data_path = path, verbose = FALSE)

# 2) load origin/destination points and set arguments
points <- read.csv(system.file("extdata/poa_hexgrid.csv", package = "r5r"))

# 3) calculate a travel time matrix
ttm <- travel_time_matrix(r5r_core,
                          origins = points,
                          destinations = points,
                          mode = "WALK",
                          departure_datetime = as.POSIXct("13-03-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S"),
                          max_walk_dist = 3000, # meters
                          max_trip_duration = 60, # minutes

I have used the package riverdist for similar applications. It was limited for what I wanted the use but it might be useful for other users.

I have also found this package stplanr that seems to do what you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.