Skip to content

Instantly share code, notes, and snippets.

@dhersz
Last active February 18, 2021 22:10
Show Gist options
  • Save dhersz/14c4b7927a9ef91aeddcc3bf8bd0712a to your computer and use it in GitHub Desktop.
Save dhersz/14c4b7927a9ef91aeddcc3bf8bd0712a to your computer and use it in GitHub Desktop.
library(opentripplanner)
library(data.table)
# ADJUST THESE VARIABLES BEFORE RUNNING THE CODE
# total points used in the paper: 1227
n_points <- 10
n_cores <- 2
# create file structure
demodir <- file.path(tempdir(), "otp_demo")
dir.create(demodir)
otpdir <- file.path(demodir, "otp")
dir.create(otpdir)
graphs <- file.path(otpdir, "graphs")
dir.create(graphs)
poa_graph <- file.path(graphs, "poa")
dir.create(poa_graph)
# download otp jar into 'otpdir' (otp_demo/otp)
if (!file.exists(file.path(otpdir, "otp.jar")))
otp_path <- otp_dl_jar(otpdir, file_name = "otp.jar", cache = FALSE)
# move relevant files to 'poa_graph' (otp_demo/otp/graphs/poa)
poa_path <- system.file("extdata/poa", package = "r5r")
invisible(file.copy(
from = file.path(poa_path, "poa_osm.pbf"),
to = file.path(poa_graph, "poa_osm.pbf")
))
invisible(file.copy(
from = file.path(poa_path, "poa.zip"),
to = file.path(poa_graph, "poa.zip")
))
# build graph
log_graph <- otp_build_graph(otp = otp_path, dir = otpdir, router = "poa")
# configurations
# setClampInitial = 0 means that initial waiting time is not subtracted out of
# total trip duration
router_config <- otp_make_config("router")
router_config$routingDefaults$clampInitialWait <- 0
otp_write_config(router_config, dir = otpdir, router = "poa")
# setup otp server
# using wait = TRUE seems not to be very reliable, 'Sys.sleep()' prevents trying
# to connect to the server while it is still setting up
log_setup <- otp_setup(
otp = otp_path,
dir = otpdir,
router = "poa",
port = 8801,
securePort = 8802,
wait = FALSE
)
Sys.sleep(30)
# connect to server
otpcon <- otp_connect(router = "poa", port = 8801)
# load od points
od_points <- fread(file.path(poa_path, "poa_hexgrid.csv"))
od_points <- head(od_points, n_points)
od_ids <- od_points$id
od_coords <- as.matrix(od_points[, .(lon, lat)])
# setup from_place and to_place to allow for all to all routing
from_place <- od_coords[rep(1:nrow(od_coords), each = nrow(od_points)), ]
from_place_ids <- od_ids[rep(1:nrow(od_coords), each = nrow(od_points))]
to_place <- od_coords[rep(1:nrow(od_coords), times = nrow(od_points)), ]
to_place_ids <- od_ids[rep(1:nrow(od_coords), times = nrow(od_points))]
# calculate routes
running_time <- system.time(
routes <- otp_plan(
otpcon,
from_place,
to_place,
fromID = from_place_ids,
toID = to_place_ids,
mode = c("WALK", "TRANSIT"),
date_time = as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S"),
maxWalkDistance = 1000,
numItineraries = 1,
ncores = n_cores,
distance_balance = TRUE,
get_geometry = FALSE
)
)
# select desired columns and filter dataset to only show one entry per OD
# ('routes' has information for every leg of each itinerary)
tt_matrix <- routes[, .(fromPlace, toPlace, duration)]
tt_matrix <- tt_matrix[tt_matrix[, .I[1], keyby = .(fromPlace, toPlace)]$V1]
# convert 'duration' from seconds to minutes
tt_matrix[, duration := duration / 60]
# kill running java processes
otp_stop(warn = FALSE)
@rafapereirabr
Copy link

The code in r5r for an equivalent comparison.

library(r5r)

 # build transport network
 r5r_core <- setup_r5(data_path = poa_path)

 # set departure time
 departure_datetime <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S")

 # estimate travel time matrix
 running_time_r5r <- system.time( ttm <- travel_time_matrix(r5r_core,
                                             origins = od_points,
                                             destinations = od_points,
                                             mode = c("WALK", "TRANSIT"),
                                             departure_datetime = departure_datetime,
                                             max_walk_dist = 1000,
                                             max_trip_duration = 1200,
                                             n_threads = n_cores)
                           )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment