AbstractThis vignette shows how to do route planning using the
detailed_itineraries()function in r5r.
r5r has some extremely efficient functions to run multimodal routing and accessibility analysis. In general, though, these functions output only the essential information required by most transport planning applications and simulation models. Moreover, the algorithms behind these function return only the optimal route in terms of minimizing travel times and/or monetary costs. Sometimes, though, we would like to do more simple route planning analysis and extract more information for each route. Also, we might be interested in finding not only the fastest route but some other suboptimal route alternatives too.
This is where the
detailed_itineraries() function comes
in. This function outputs for each origin destination pair a detailed
route plan with information per leg, meaning a route taken by a single
mode such as a walk to the bus stop. In R5’s documentation these legs
are referred to as ‘segments’, a word more usually used to describe
small sections on the transport network. Results contain information on
the mode, waiting times, travel times and distances for each leg (or
‘segment’ in R5 documentation) and the trip geometry. Moreover, the
detailed_itineraries() function can also return results for
multiple route alternatives. Let’s see how this function works using a
obs. We only recommend using
detailed_itineraries() in case you are interested in
finding suboptimal alternative routes and/or need the geometry
information of the outputs. If you only want to have route information
detailed by trip segments, then we would strongly encourage you to use
expanded_travel_time_matrix() function. More info
First, let’s load some libraries and build our multimodal transport
network. In this example we’ll be using the a sample data set for the
city of Porto Alegre (Brazil) included in
# increase Java memory options(java.parameters = "-Xmx2G") # load libraries library(r5r) library(sf) library(ggplot2) library(data.table) # build a routable transport network with r5r <- system.file("extdata/poa", package = "r5r") data_path <- setup_r5(data_path) r5r_core # routing inputs <- c('walk', 'transit') mode <- 60 # minutes max_trip_duration # departure time <- as.POSIXct("13-05-2019 14:00:00", departure_datetime format = "%d-%m-%Y %H:%M:%S") # load origin/destination points <- fread(file.path(data_path, "poa_points_of_interest.csv"))poi
In this example below, we want to know some alternative routes
between a single origin/destination pair. To get multiple route
alternatives, we need to set
shortest_path = FALSE.
# set inputs <- poi[10,] origins <- poi[12,] destinations <- c("WALK", "TRANSIT") mode <- 60 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, suboptimal_minutes = 8, 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() + fig geom_sf(data = street_net$edges, color='gray85') + geom_sf(data = subset(det, option <4), aes(color=mode)) + facet_wrap(.~option) + theme_void() fig
# SAVE image ggsave(plot = fig, filename = 'inst/img/vig_detailed_ggplot.png', height = 5, width = 15, units='cm', dpi=200)
detailed_itineraries()will query routes between the 1st origin to the 1st destination, then the 2nd origin to the 2nd destination, and so on. If you would like to query routes between all origins to all destinations you can simply set
all_to_all = TRUE.
detailed_itineraries()will not return the spatial geometry of results. To retrieve this information you can simply set
geometry = TRUE.
Please note that the
does not run on frequency-based GTFS feeds. A simple hack to overcome
this problem is to convert your GTFS data from frequencies to time
tables. This can be easily done using the
package. Here is how:
library(gtfstools) # location of your frequency-based GTFS <- system.file("extdata/spo/spo.zip", package = "r5r") freq_gtfs_file # read GTFS data <- gtfstools::read_gtfs(freq_gtfs_file) freq_gtfs # convert from frequencies to time tables <- gtfstools::frequencies_to_stop_times(freq_gtfs) stop_times_gtfs # save it as a new GTFS.zip file ::write_gtfs(gtfs = stop_times_gtfs, gtfstoolspath = tempfile(pattern = 'stop_times_gtfs', fileext = '.zip'))
… and now you can use
r5r on this
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.