r5ris an R package for rapid realistic routing on multimodal transport networks (walk, bike, public transport and car) using R5. The package allows users to generate detailed routing analysis or calculate travel time matrices using seamless parallel computing on top of the R5 Java machine https://github.com/conveyal/r5
r5r is an R package for rapid realistic routing on multimodal transport networks (walk, bike, public transport and car). It provides a simple and friendly interface to R5, a really fast and open source Java-based routing engine developed separately by Conveyal. R5 stands for Rapid Realistic Routing on Real-world and Reimagined networks. More details about r5r can be found on the package webpage or on this paper.
You can install
r5r from CRAN, or the development
version from github.
# CRAN install.packages('r5r') # dev version on github ::install_github("ipeaGIT/r5r", subdir = "r-package")devtools
Please bear in mind that you need to have Java SE Development Kit
11 installed on your computer to use
r5r. No worries,
you don’t have to pay for it. The jdk 11 is freely available from the
If you don’t know what version of Java you have installed on your computer, you can check it by running this on R console.
::.jinit() rJava::.jcall("java.lang.System", "S", "getProperty", "java.version") rJava
Before we start, we need to increase the memory available to Java.
This is necessary because, by default,
R allocates only
512MB of memory for Java processes, which is not enough for large
r5r. To increase available memory to 2GB, for
example, we need to set the
java.parameters option at the
beginning of the script, as follows:
options(java.parameters = "-Xmx2G")
Note: It’s very important to allocate enough memory before loading
r5r or any other Java-based package, since
rJava starts a Java Virtual Machine only once for each R
session. It might be useful to restart your R session and execute the
code above right after, if you notice that you haven’t succeeded in your
Then we can load the packages used in this vignette:
library(r5r) library(sf) library(data.table) library(ggplot2)
r5r package has seven fundamental
setup_r5() to initialize an instance of
r5r, that also builds a routable transport
accessibility() for fast computation of access to
opportunities considering a selected decay function;
travel_time_matrix() for fast computation of travel
time estimates between origin/destination pairs;
expanded_travel_time_matrix() for calculating travel
matrices between origin destination pairs with additional information
such routes used and total time disaggregated by access, waiting,
in-vehicle and transfer times.
detailed_itineraries() to get detailed information
on one or multiple alternative routes between origin/destination
pareto_frontier() for analyzing the trade-off
between the travel time and monetary costs of multiple route
alternatives between origin/destination pairs.
isochrone() to estimate the polygons of the areas
that can be reached from an origin point at different travel time
Most of these functions also allow users to account for monetary travel costs when generating travel time matrices and accessibility estimates. More info about how to consider monetary costs can be found in this vignette.
The package also includes a few support functions.
street_network_to_sf() to extract OpenStreetMap
network in sf format from a
transit_network_to_sf() to extract transit network
in sf format from a
find_snap() to find snapped locations of input
points on street network.
r5r_sitrep() to generate a situation report to help
debug eventual errors.
r5r, you will need: - A road network data set
from OpenStreetMap in
.pbf format (mandatory) - A
public transport feed in
GTFS.zip format (optional) - A
raster file of Digital Elevation Model data in
Here are a few places from where you can download these data sets:
Let’s have a quick look at how
r5r works using a sample
To illustrate the functionalities of
r5r, the package
includes a small sample data for the city of Porto Alegre (Brazil). It
includes seven files:
poa_hexgrid.csvfile with spatial coordinates of a regular hexagonal grid covering the sample area, which can be used as origin/destination pairs in a travel time matrix calculation.
poa_points_of_interest.csvfile containing the names and spatial coordinates of 15 places within Porto Alegre
fares_poa.zipfile with the fare rules of the city’s public transport system.
<- system.file("extdata/poa", package = "r5r") data_path list.files(data_path)
The points of interest data can be seen below. In this example, we will be looking at transport alternatives between some of those places.
<- fread(file.path(data_path, "poa_points_of_interest.csv")) poi head(poi)
The data with origin destination pairs is shown below. In this example, we will be using 200 points randomly selected from this data set.
<- fread(file.path(data_path, "poa_hexgrid.csv")) points # sample points <- sample(1:nrow(points), 200, replace=TRUE) sampled_rows <- points[ sampled_rows, ] points head(points)
The first step is to build the multimodal transport network used for
routing in R5. This is done with the
function. This function does two things: (1) downloads/updates a
compiled JAR file of R5 and stores it locally in the
r5r package directory for future use; and (2) combines the
osm.pbf and gtfs.zip data sets to build a routable network object.
# Indicate the path where OSM and GTFS data are stored <- setup_r5(data_path = data_path)r5r_core
The faster way to calculate accessibility estimates is using the
accessibility() function. In this example, we calculate the
number of schools and health care facilities accessible in less than 60
minutes by public transport and walking. More details in this vignette
and visualizing Accessibility.
# set departure datetime input <- as.POSIXct("13-05-2019 14:00:00", departure_datetime format = "%d-%m-%Y %H:%M:%S") # calculate accessibility <- accessibility(r5r_core = r5r_core, access origins = points, destinations = points, opportunities_colnames = c("schools", "healthcare"), mode = c("WALK", "TRANSIT"), departure_datetime = departure_datetime, decay_function = "step", cutoffs = 60 )head(access)
For fast routing analysis, r5r currently has three
travel_time_matrix() function is a really simple and
fast function to compute travel time estimates between one or multiple
origin/destination pairs. The origin/destination input can be either a
sf POINT object, or a
containing the columns
id, lon, lat. The function also
receives as inputs the max walking distance, in meters, and the
max trip duration, in minutes. Resulting travel times are also
output in minutes.
This function also allows users to very efficiently capture the travel time uncertainties inside a given time window considering multiple departure times. More info on this vignette.
# set inputs <- c("WALK", "TRANSIT") mode <- 30 # minutes max_walk_time <- 120 # minutes max_trip_duration <- as.POSIXct("13-05-2019 14:00:00", departure_datetime format = "%d-%m-%Y %H:%M:%S") # calculate a travel time matrix <- travel_time_matrix(r5r_core = r5r_core, ttm origins = poi, destinations = poi, mode = mode, departure_datetime = departure_datetime, max_walk_time = max_walk_time, max_trip_duration = max_trip_duration) head(ttm)
For those interested in more detailed outputs, the
expanded_travel_time_matrix() works very similarly with
travel_time_matrix() but it brings much more information.
It estimates for each origin destination pair the routes used and total
time disaggregated by access, waiting, in-vehicle and transfer times.
Please note this function can be very memory intensive for large data
# calculate a travel time matrix <- expanded_travel_time_matrix(r5r_core = r5r_core, ettm origins = poi, destinations = poi, mode = mode, departure_datetime = departure_datetime, breakdown = TRUE, max_walk_time = max_walk_time, max_trip_duration = max_trip_duration) head(ettm)
Most routing packages only return the fastest route. A key advantage
detailed_itineraries() function is that is allows
for fast routing analysis while providing multiple alternative routes
between origin destination pairs. The output also brings detailed
information for each route alternative at the trip segment level,
including the transport mode, waiting times, travel time and distance of
each trip segment.
In this example below, we want to know some alternative routes between one origin/destination pair only.
# set inputs <- poi[10,] origins <- poi[12,] destinations <- c("WALK", "TRANSIT") mode <- 60 # minutes max_walk_time <- as.POSIXct("13-05-2019 14:00:00", departure_datetime format = "%d-%m-%Y %H:%M:%S") # calculate detailed itineraries <- detailed_itineraries(r5r_core = r5r_core, det origins = origins, destinations = destinations, mode = mode, departure_datetime = departure_datetime, max_walk_time = max_walk_time, shortest_path = FALSE) head(det)
The output is a
data.frame sf object, so we can easily
visualize the results.
Static visualization with
package: To provide a geographic context for the visualization of the
ggplot2, you can also use the
street_network_to_sf() function to extract the OSM street
network used in the routing.
# extract OSM network <- street_network_to_sf(r5r_core) street_net # extract public transport network <- r5r::transit_network_to_sf(r5r_core) transit_net # plot ggplot() + geom_sf(data = street_net$edges, color='gray85') + geom_sf(data = det, aes(color=mode)) + facet_wrap(.~option) + theme_void()
r5r objects are still allocated to any amount of memory
previously set after they are done with their calculations. In order to
remove an existing
r5r object and reallocate the memory it
had been using, we use the
stop_r5 function followed by a
call to Java’s garbage collector, as follows:
::stop_r5(r5r_core) r5r::.jgc(R.gc = TRUE)rJava
If you have any suggestions or want to report an error, please visit the package GitHub page.