From 40e715f84b6e36c98985bbecad15147c32c337fb Mon Sep 17 00:00:00 2001 From: mpadge Date: Thu, 15 Dec 2022 11:10:19 +0100 Subject: [PATCH] add 'update_trips_table_with_freqs' for #89 --- DESCRIPTION | 2 +- R/frequencies_to_stop_times.R | 43 +++++++++++++++++++++++++++++++++++ codemeta.json | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5a105f81..1d5a5e6f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gtfsrouter Title: Routing with GTFS (General Transit Feed Specification) Data -Version: 0.0.5.129 +Version: 0.0.5.130 Authors@R: c( person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre")), person("Marcin", "Stepniak", , "marcinstepniak@ucm.es", role = "aut", diff --git a/R/frequencies_to_stop_times.R b/R/frequencies_to_stop_times.R index df565a51..090e0519 100644 --- a/R/frequencies_to_stop_times.R +++ b/R/frequencies_to_stop_times.R @@ -82,6 +82,8 @@ frequencies_to_stop_times <- function (gtfs) { index <- which (names (gtfs_cp$stop_times) %in% names (res)) gtfs_cp$stop_times <- rbind (gtfs_cp$stop_times [, ..index], res) + gtfs_cp <- update_trips_table_with_freqs (gtfs_cp, sfx) + attr (gtfs_cp, "freq_sfx") <- sfx return (gtfs_cp) @@ -147,3 +149,44 @@ calc_num_new_timetables <- function (freqs) { return (freqs) } + +#' Expand each row of "trips" table to corresponding number of new trips with +#' frequency table extensions of `sfx` + increasing numbers. +#' @noRd +update_trips_table_with_freqs <- function (gtfs, sfx) { + + trip_ids <- unique (gtfs$stop_times$trip_id) + trip_ids_with_sfx <- grep (paste0 (sfx, "[0-9]*$"), trip_ids, value = TRUE) + + # Current ids in freqs table: + freqs_trips <- gsub (paste0 (sfx, "[0-9]*$"), "", trip_ids_with_sfx) + freqs_trips_tab <- table (freqs_trips) + freqs_trips <- names (freqs_trips_tab) + # `freqs_trips` are then the names of the original `trips$trip_id` trips, + # with `freqs_trips_tab` tallying how many times each are repeated in + # translating the frequencies table. + + index <- which (gtfs$trips$trip_id %in% freqs_trips) + if (length (index) == 0) { + return (gtfs) + } + + trips_no_freqs <- gtfs$trips [seq_len (nrow (gtfs$trips)) [-index], ] + trips_freqs <- gtfs$trips [index, ] + + freqs_trips <- freqs_trips [which (freqs_trips %in% trips_freqs$trip_id)] + freqs_trips_tab <- freqs_trips_tab [which (names (freqs_trips_tab) %in% trips_freqs$trip_id)] + + # The `trips_freqs` table then has one row for each original trip, with + # `trip_id` not containing the `sfx` version for frequency table entries. + # Each row needs to be expanded to the corresponding number of + # frequency-table trips. + freqs_trips_tab <- freqs_trips_tab [match (trips_freqs$trip_id, freqs_trips)] + index <- rep (seq_len (nrow (trips_freqs)), times = freqs_trips_tab) + trips_freqs_exp <- trips_freqs [index, ] + trips_freqs_exp$trip_id <- trip_ids + + gtfs$trips <- rbind (trips_no_freqs, trips_freqs_exp) + + return (gtfs) +} diff --git a/codemeta.json b/codemeta.json index 1a7df694..b801102d 100644 --- a/codemeta.json +++ b/codemeta.json @@ -7,7 +7,7 @@ "codeRepository": "https://github.com/ATFutures/gtfs-router", "issueTracker": "https://github.com/ATFutures/gtfs-router/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.0.5.129", + "version": "0.0.5.130", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R",