
Calculate travel time matrix between origin destination pairs considering a time of arrival
Source:R/arrival_travel_time_matrix.R
arrival_travel_time_matrix.RdComputes travel times between origin-destination pairs for a given arrival
time: for each pair, the trip with the latest departure that arrives by
arrival_datetime. Departures are searched minute by minute between
arrival_datetime - max_trip_duration and arrival_datetime, so
max_trip_duration also sets the search window. For a departure time, use
travel_time_matrix(). This function wraps expanded_travel_time_matrix(),
so its output has more columns than that of travel_time_matrix(), and it
can be very memory intensive with a long max_trip_duration.
destinations can have at most 5000 rows, a limit of R5 for detailed path
information; split larger sets into chunks.
Usage
arrival_travel_time_matrix(
r5r_network,
origins,
destinations,
mode = "WALK",
mode_egress = "WALK",
arrival_datetime = Sys.time(),
breakdown = FALSE,
max_walk_time = Inf,
max_bike_time = Inf,
max_car_time = Inf,
max_trip_duration = 120L,
walk_speed = 3.6,
bike_speed = 12,
max_rides = 3,
max_lts = 2,
new_carspeeds = NULL,
carspeed_scale = 1,
new_lts = NULL,
draws_per_minute = 5L,
n_threads = Inf,
verbose = FALSE,
progress = FALSE,
output_dir = NULL,
r5r_core = deprecated()
)Arguments
- r5r_network
A routable transport network created with
build_network().- origins, destinations
Either a
POINT sfobject with WGS84 CRS, or adata.framecontaining the columnsid,lonandlat.- mode
A character vector. The transport modes allowed for access, transfer and vehicle legs of the trips. Defaults to
WALK. See details for other options.- mode_egress
A character vector. The transport mode used after egress from the last public transport. It can be either
WALK,BICYCLEorCAR. Defaults toWALK. Ignored when public transport is not used.- arrival_datetime
A POSIXct object. The time by which trips must arrive. When routing with public transport, services must run on the date of
arrival_datetime - max_trip_duration(seecheck_transit_availability()). Defaults toSys.time(). See details for how datetimes are parsed.- breakdown
A logical. Whether to include detailed information about each trip in the output. If
FALSE(the default), the output lists the total time between each origin-destination pair and the routes used to complete the trip for each minute of the specified time window. IfTRUE, the output also includes the access, waiting, in-vehicle, transfer and egress time of each trip, and its number of public transport rides. For trips made only by walking, cycling or driving (routesequal to[WALK],[BICYCLE]or[CAR]), these components are all0andtotal_timeholds the whole trip.- max_walk_time
An integer. The maximum walking time (in minutes) to access and egress the transit network, make transfers, or complete walk-only trips. Applies to each leg separately (e.g.
15allows up to 15 minutes to reach transit and another 15 after leaving it). Defaults toInf(no limit besidesmax_trip_duration). In walk-only trips, the lower ofmax_walk_timeandmax_trip_durationapplies.- max_bike_time
An integer. The maximum cycling time (in minutes) to access and egress the transit network, make transfers, or complete bicycle-only trips. Applies to each leg separately (e.g.
15allows up to 15 minutes to reach transit and another 15 after leaving it). Defaults toInf(no limit besidesmax_trip_duration). In bicycle-only trips, the lower ofmax_bike_timeandmax_trip_durationapplies.- max_car_time
An integer. The maximum driving time (in minutes) to access and egress the transit network, or to complete car-only trips. Applies to each leg separately (e.g.
15allows up to 15 minutes to reach transit and another 15 after leaving it). Defaults toInf(no limit besidesmax_trip_duration). In car-only trips, the lower ofmax_car_timeandmax_trip_durationapplies.- max_trip_duration
An integer. The maximum trip duration in minutes. Defaults to 120.
- walk_speed
A numeric. Average walk speed in km/h. Defaults to 3.6.
- bike_speed
A numeric. Average cycling speed in km/h. Defaults to 12.
- max_rides
An integer. The maximum number of public transport rides allowed in the same trip. Defaults to 3.
- max_lts
An integer between 1 and 4. The maximum level of traffic stress that cyclists will tolerate. A value of 1 means cyclists will only travel through the quietest streets, while a value of 4 indicates cyclists can travel through any road. Defaults to 2. See details.
- new_carspeeds
A
data.framespecifying the new car speed for each OSM edge id. This table must contain columnsosm_id,max_speedandspeed_type. The"speed_type"column is of class character and it indicates whether the values in"max_speed"should be interpreted as percentages of original speeds ("scale") or as absolute speeds ("km/h"). Alternatively, thenew_carspeedsparameter can receive ansf data.framewith POLYGON geometry that indicates the new car speed for all the roads that fall within each polygon. In this case, the table must contain the columnspoly_idwith a unique id for each polygon,scalewith the new speed scaling factors andpriority, which is a number ranking which polygon should be considered in case of overlapping polygons. See more info in the scenarios vignette (vignette("scenarios", package = "r5r")).- carspeed_scale
Numeric. The scaling factor applied to the car speed of road segments not specified in
new_carspeeds. Defaults to1, which keeps the speeds of the unlisted roads unchanged.- new_lts
A
data.framespecifying the new LTS levels for each OSM edge id. The table must contain columnsosm_idandlts. Alternatively, thenew_ltsparameter can receive ansf data.framewith LINESTRING geometry. R5 will then find the nearest road for each LINESTRING and update its LTS value accordingly.- draws_per_minute
An integer. The number of Monte Carlo draws to perform per minute of
time_window. Defaults to 5. This would mean 300 draws in a 60-minute time window, for example. This parameter only affects the results when the GTFS feeds contain afrequencies.txttable. If the GTFS feed does not have a frequency table, r5r still allows for multiple runs over the settime_windowbut in a deterministic way.- n_threads
An integer. The number of threads to use when running the router in parallel. Defaults to
Inf(all available threads).- verbose
A logical. Whether to show
R5informative messages when running the function. Defaults toFALSE(R5error messages are still shown).TRUEshows detailed output, useful for debugging issues not caught byr5r.- progress
A logical. Whether to show a progress counter when running the router. Defaults to
FALSE. Only works whenverboseisFALSE. May slightly slow computation, as the counter is synchronized across threads.- output_dir
Either
NULL(the default) or a path to an existing directory. When a path is given, the function writes the results as.csvfiles to that directory and returns the path instead of the results. Useful in memory-constrained settings, as results are not loaded into RAM. Missing values (NA) are written as empty fields.- r5r_core
The
r5r_coreargument is deprecated as of r5r v2.3.0. Use ther5r_networkargument instead.
Value
A data.table with one row per origin-destination pair that can be
reached by arrival_datetime, describing the trip with the latest
departure that still arrives in time: its departure_time, the routes
used and its total_time (in minutes), plus the columns added by
breakdown = TRUE (see expanded_travel_time_matrix()). Pairs that
cannot be reached in time are absent from the output. When the search
window crosses midnight, departure times after midnight are reported as
"24:MM:SS", and only the public transport services of the departure
day are considered. Trips made only by walking, cycling or driving have
travel times in whole minutes, so they can arrive up to 59 seconds after
arrival_datetime. If output_dir is not NULL, the function
returns the path specified in that parameter, in which the .csv files
containing the results are saved.
Transport modes
R5 allows for multiple combinations of transport modes. The options
include:
Transit modes:
TRAM,SUBWAY,RAIL,BUS,FERRY,CABLE_CAR,GONDOLA,FUNICULAR. The optionTRANSITautomatically considers all public transport modes available.Non transit modes:
WALK,BICYCLE,CAR.
Level of Traffic Stress (LTS)
When cycling is enabled in R5 (by passing the value BICYCLE to either
mode or mode_egress), setting max_lts will allow cycling only on
streets with a given level of danger/stress. Setting max_lts to 1, for
example, will allow cycling only on separated bicycle infrastructure or
low-traffic streets and routing will revert to walking when traversing any
links with LTS exceeding 1. Setting max_lts to 3 will allow cycling on
links with LTS 1, 2 or 3. Routing also reverts to walking if the street
segment is tagged as non-bikable in OSM (e.g. a staircase), independently of
the specified max LTS.
The default methodology for assigning LTS values to network edges is based on commonly tagged attributes of OSM ways. See more info about LTS in the original documentation of R5 from Conveyal at https://docs.conveyal.com/learn-more/traffic-stress. In summary:
LTS 1: Tolerable for children. This includes low-speed, low-volume streets, as well as those with separated bicycle facilities (such as parking-protected lanes or cycle tracks).
LTS 2: Tolerable for the mainstream adult population. This includes streets where cyclists have dedicated lanes and only have to interact with traffic at formal crossing.
LTS 3: Tolerable for "enthused and confident" cyclists. This includes streets which may involve close proximity to moderate- or high-speed vehicular traffic.
LTS 4: Tolerable only for "strong and fearless" cyclists. This includes streets where cyclists are required to mix with moderate- to high-speed vehicular traffic.
For advanced users, you can provide custom LTS values by adding a tag <key = "lts"> to the osm.pbf file.
Datetime parsing
r5r ignores the timezone attribute of datetime objects when parsing dates
and times, using the study area's timezone instead. For example, let's say
you are running some calculations using Rio de Janeiro, Brazil, as your study
area. The datetime as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S") will be parsed as May 13th, 2019, 14:00h in
Rio's local time, as expected. But as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S", tz = "Europe/Paris") will also be parsed as
the exact same date and time in Rio's local time, perhaps surprisingly,
ignoring the timezone attribute.
Routing algorithm
The travel_time_matrix(), expanded_travel_time_matrix(),
arrival_travel_time_matrix() and accessibility() functions use an
R5-specific extension to the RAPTOR routing algorithm (see Conway et al.,
2017). This RAPTOR extension uses a systematic sample of one departure per
minute over the time window set by the user in the 'time_window' parameter.
A detailed description of base RAPTOR can be found in Delling et al (2015).
However, whenever the user includes transit fares inputs to these functions,
they automatically switch to use an R5-specific extension to the McRAPTOR
routing algorithm.
Conway, M. W., Byrd, A., & van der Linden, M. (2017). Evidence-based transit and land use sketch planning using interactive accessibility methods on combined schedule and headway-based networks. Transportation Research Record, 2653(1), 45-53. doi:10.3141/2653-06
Delling, D., Pajor, T., & Werneck, R. F. (2015). Round-based public transit routing. Transportation Science, 49(3), 591-604. doi:10.1287/trsc.2014.0534
Examples
library(r5r)
# build transport network
data_path <- system.file("extdata/poa", package = "r5r")
r5r_network <- build_network(data_path )
#> Using cached R5 version from /home/runner/.cache/R/r5r/r5_jar_v7.5.1/r5-v7.5-1-gf3631e9-all.jar
#> ℹ Using cached network from
#> /home/runner/work/_temp/Library/r5r/extdata/poa/network.dat.
# load origin/destination points
points <- read.csv(file.path(data_path, "poa_points_of_interest.csv"))
arrival_datetime <- as.POSIXct(
"13-05-2019 14:00:00",
format = "%d-%m-%Y %H:%M:%S"
)
# by default only returns the total time between each pair in each minute of
# the specified time window
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)
#> from_id to_id departure_time draw_number routes
#> <char> <char> <char> <int> <char>
#> 1: public_market public_market 13:59:00 1 [WALK]
#> 2: public_market bus_central_station 13:45:00 1 LINHA1
#> 3: public_market gasometer_museum 13:45:00 1 2441
#> 4: public_market santa_casa_hospital 13:44:00 1 [WALK]
#> 5: public_market townhall 13:56:00 1 [WALK]
#> 6: public_market piratini_palace 13:42:00 1 [WALK]
#> total_time
#> <num>
#> 1: 0.0
#> 2: 13.8
#> 3: 11.7
#> 4: 15.4
#> 5: 3.6
#> 6: 17.4
# when breakdown = TRUE the output contains much more information
arrival_ttm2 <- arrival_travel_time_matrix(
r5r_network,
origins = points,
destinations = points,
mode = c("WALK", "TRANSIT"),
arrival_datetime = arrival_datetime,
max_trip_duration = 60,
breakdown = TRUE
)
head(arrival_ttm2)
#> from_id to_id departure_time draw_number access_time
#> <char> <char> <char> <int> <num>
#> 1: public_market public_market 13:59:00 1 0.0
#> 2: public_market bus_central_station 13:45:00 1 4.8
#> 3: public_market gasometer_museum 13:45:00 1 3.5
#> 4: public_market santa_casa_hospital 13:44:00 1 0.0
#> 5: public_market townhall 13:56:00 1 0.0
#> 6: public_market piratini_palace 13:42:00 1 0.0
#> wait_time ride_time transfer_time egress_time routes n_rides total_time
#> <num> <num> <num> <num> <char> <int> <num>
#> 1: 0.0 0.0 0 0.0 [WALK] 0 0.0
#> 2: 1.2 1.6 0 6.2 LINHA1 1 13.8
#> 3: 1.5 4.9 0 1.8 2441 1 11.7
#> 4: 0.0 0.0 0 0.0 [WALK] 0 15.4
#> 5: 0.0 0.0 0 0.0 [WALK] 0 3.6
#> 6: 0.0 0.0 0 0.0 [WALK] 0 17.4
stop_r5(r5r_network)
#> r5r_network has been successfully stopped.