gtfsrouter icon indicating copy to clipboard operation
gtfsrouter copied to clipboard

Update route function to reflect new traveltimes algorithm

Open mpadge opened this issue 4 years ago • 0 comments

The traveltimes algorithm is more efficient, and more accurate. This is an example, with code taken directly from #57:

library (lubridate)
library (gtfsrouter)
packageVersion ("gtfsrouter")
#> [1] '0.0.4.154'
gtfs <- extract_gtfs("vbb.zip")
#> ▶ Unzipping GTFS archive
#> ✔ Unzipped GTFS archive
#> ▶ Extracting GTFS feed✔ Extracted GTFS feed 
#> ▶ Converting stop times to seconds✔ Converted stop times to seconds 
#> ▶ Converting transfer times to seconds✔ Converted transfer times to seconds
gtfs$calendar [1, ]
#>    service_id monday tuesday wednesday thursday friday saturday sunday
#> 1:          1      0       0         0        0      0        0      0
#>    start_date end_date
#> 1:   20201023 20201212

transfers <- gtfsrouter::gtfs_transfer_table (gtfs, network_times = FALSE)
transfers <- gtfs$transfers %>%
  select(.data$from_stop_id, .data$to_stop_id, .data$transfer_type, .data$min_transfer_time) %>%
  rbind(transfers) %>%
  group_by(.data$from_stop_id, .data$to_stop_id) %>%
  summarise(across(everything(), first)) %>% # take min_transfer_time from gtfs$transfers if present
  data.table::data.table()
gtfs$transfers <- transfers
gtfs <- gtfs_timetable(gtfs, day = "tuesday")

from <- "Berlin Hauptbahnhof"
start_time <- 8 * 3600
iso <- gtfs_traveltimes (gtfs, from, start_time)

route_stats <- function (gtfs, from, to, start_time) {
    r <- gtfs_route (gtfs, from, to, start_time)
    tstart <- hms (r$departure_time [1])
    tend <- hms (tail (r$arrival_time, 1))
    dur_sec <- as.duration (tend - tstart) / dseconds (1)
    dur <- seconds_to_period (dur_sec)
    ntr <- length (unique (r$trip_name)) - 1
    dur_fmt <- sprintf ('%02d:%02d:%02d', dur@hour, minute (dur), second (dur))
    message (ntr, " transfers; trip duration = ", dur_fmt)
}

to <- "Berlin, Goslarer Platz"
this_iso <- iso [grep (to, iso$stop_name), ]
print (this_iso)
#>       duration ntransfers      stop_id              stop_name stop_lon stop_lat
#> 288   00:26:00          2 900000001155 Berlin, Goslarer Platz 13.31426 52.52579
#> 26701 00:24:00          1 070101001168 Berlin, Goslarer Platz 13.31426 52.52579
#> 26851 00:17:00          1 070101001337 Berlin, Goslarer Platz 13.31426 52.52579
route_stats (gtfs, from, to, start_time)
#> 1 transfers; trip duration = 00:24:00

Created on 2021-01-22 by the reprex package (v0.3.0)

And the 17:00 trip is not found by gtfs_route(), even though the extra code in #57 clearly shows that it is correct.

The main C++ routines of gtfs_route() need to be replaced by the algorithms developed for traveltimes. ping @AlexandraKapp

mpadge avatar Jan 22 '21 10:01 mpadge