--- title: 'Travel time matrices' date: "`r Sys.Date()`" output: rmarkdown::html_vignette abstract: "This vignette shows how to use the travel_time_matrix() and expanded_travel_time_matrix() functions in r5r." urlcolor: blue vignette: > %\VignetteIndexEntry{Travel time matrices} %\VignetteEngine{knitr::rmarkdown} \usepackage[utf8]{inputenc} bibliography: references.json --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = identical(tolower(Sys.getenv("NOT_CRAN")), "true"), out.width = "100%" ) ## removes files previously created by 'build_network()' #data_path <- system.file("extdata/poa", package = "r5r") #existing_files <- list.files(data_path) #files_to_keep <- c( # "poa_hexgrid.csv", # "poa_osm.pbf", # "poa_points_of_interest.csv", # "poa_eptc.zip", # "poa_trensurb.zip", # 'fares' # ) #files_to_remove <- existing_files[! existing_files %in% files_to_keep] #invisible(file.remove(file.path(data_path, files_to_remove))) ``` # 1. Introduction Many transport planning and modeling tasks need travel time estimates between origins and destinations. `R5` computes realistic door-to-door travel times in multimodal networks very fast, and `r5r` offers three functions for it: - `travel_time_matrix()` - `expanded_travel_time_matrix()` - `arrival_travel_time_matrix()` This vignette shows, with a reproducible example, how they work and how they differ. # 2. Build routable transport network with `build_network()` We use the Porto Alegre (Brazil) sample data included in `r5r`. ```{r, message = FALSE} # increase Java memory old_options <- options(java.parameters = "-Xmx2G") # load libraries library(r5r) library(data.table) library(ggplot2) # build a routable transport network with r5r data_path <- system.file("extdata/poa", package = "r5r") r5r_network <- build_network(data_path) # routing inputs mode <- c('walk', 'transit') max_trip_duration <- 60 # minutes # departure time departure_datetime <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S") # load origin/destination points points <- fread(file.path(data_path, "poa_points_of_interest.csv")) ``` # 3. The `travel_time_matrix()` function `travel_time_matrix()` quickly computes travel times between all origin/destination pairs for a departure time and transport mode. Other parameters include: - `max_trip_duration`: maximum trip duration - `max_rides`: maximum number of public transport rides - `max_walk_time` and `max_bike_time`: maximum walking or cycling time to and from public transport - `walk_speed` and `bike_speed`: average walking or cycling speed (km/h) - `max_fare`: maximum monetary cost in public transport. [See this vignette](https://ipea.github.io/r5r/articles/fare_structure.html). ```{r, message = FALSE} # estimate travel time matrix ttm <- travel_time_matrix( r5r_network, origins = points, destinations = points, mode = mode, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime ) head(ttm, n = 10) ``` Travel times can vary significantly across the day with public transport service levels. The `time_window` and `percentiles` parameters handle this efficiently: R5 computes travel times for departures every minute within `time_window` and returns the selected percentiles ([time window vignette](https://ipea.github.io/r5r/articles/time_window.html)). # 4. The `expanded_travel_time_matrix()` function `expanded_travel_time_matrix()` returns more than the total travel time: by default, it also lists the public transport routes taken between each origin/destination pair. With `breakdown = TRUE`, it adds each trip's number of public transport rides and its access, waiting, in-vehicle, transfer and egress times, which can be slower for large data sets. *A general call to expanded_travel_time_matrix()* ```{r, message = FALSE} ettm <- expanded_travel_time_matrix( r5r_network, origins = points, destinations = points, mode = mode, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime ) head(ettm, n = 10) ``` *Calling expanded_travel_time_matrix() with `breakdown = TRUE`* ```{r, message = FALSE} ettm2 <- expanded_travel_time_matrix( r5r_network, origins = points, destinations = points, mode = mode, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime, breakdown = TRUE ) head(ettm2, n = 10) ``` Over its `time_window` (10 minutes by default), `expanded_travel_time_matrix()` returns the fastest route departing at each minute. This can be very memory intensive for large data sets and time windows. ```{r, message = FALSE} ettm_window <- expanded_travel_time_matrix( r5r_network, origins = points, destinations = points, mode = mode, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime, breakdown = TRUE, time_window = 10 ) ettm_window[15:25,] ``` # 5. The `arrival_travel_time_matrix()` function `travel_time_matrix()` and `expanded_travel_time_matrix()` take a **departure** time. To route by **arrival** time, use `arrival_travel_time_matrix()`: given the latest acceptable arrival time and a maximum trip duration, it returns the travel time of the trip with the latest departure that still arrives in time. This models trips where arriving by a set time matters, such as getting to work or school by 9 a.m.: people usually take the latest departure that gets them there on time, not the fastest trip that leaves them waiting at the destination. As in `expanded_travel_time_matrix()`, the output has additional columns (and `breakdown = TRUE` is available). *A general call to arrival_travel_time_matrix()* ```{r, message = FALSE} arrival_datetime <- as.POSIXct( "13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S" ) arrival_ttm <- arrival_travel_time_matrix( r5r_network, origins = points, destinations = points, mode = c("WALK", "TRANSIT"), arrival_datetime = arrival_datetime, max_trip_duration = 60 ) head(arrival_ttm, n = 10) ``` ### Cleaning up after usage Stop the network and run Java's garbage collector to free the memory it used: ```{r, message = FALSE} r5r::stop_r5(r5r_network) rJava::.jgc(R.gc = TRUE) options(old_options) ``` If you have any suggestions or want to report an error, please visit [the package GitHub page](https://github.com/ipea/r5r). ## References