Skip to content

Commit

Permalink
warn in set_servicepattern() if feed has no dates available
Browse files Browse the repository at this point in the history
instead of erroring without an explicit message later on
  • Loading branch information
polettif committed Oct 17, 2024
1 parent 8c5a71f commit daf446f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions R/dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#' @importFrom stats reshape
#' @keywords internal
set_dates_services <- function(gtfs_obj) {
has_calendar = feed_contains(gtfs_obj, "calendar") && nrow(gtfs_obj[["calendar"]]) > 0
has_calendar_dates = feed_contains(gtfs_obj, "calendar_dates") && nrow(gtfs_obj[["calendar_dates"]]) > 0
has_calendar = feed_has_non_empty_table(gtfs_obj, "calendar")
has_calendar_dates = feed_has_non_empty_table(gtfs_obj, "calendar_dates")

# check date validity
if(!has_calendar && !has_calendar_dates) {
Expand Down
2 changes: 1 addition & 1 deletion R/frequencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ get_route_frequency <- function(gtfs_obj,
service_ids = NULL) {
total_departures <- median_headways <- mean_headways <- NULL
n_departures <- mean_headway <- st_dev_headways <- stop_count <- NULL
if(feed_contains(gtfs_obj, "frequencies") && nrow(gtfs_obj$frequencies) > 0) {
if(feed_has_non_empty_table(gtfs_obj, "frequencies")) {
message("A pre-calculated frequencies dataframe exists for this feed already, consider using that.")
}
departures_per_stop = get_stop_frequency(gtfs_obj, start_time, end_time,
Expand Down
5 changes: 5 additions & 0 deletions R/service.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#' @importFrom rlang .data
#' @export
set_servicepattern <- function(gtfs_obj, id_prefix = "s_", hash_algo = "md5", hash_length = 7) {
if(!feed_has_non_empty_table(gtfs_obj, "calendar") && !feed_has_non_empty_table(gtfs_obj, "calendar_dates")) {
warning("No dates defined in feed")
return(gtfs_obj)
}

get_servicepattern_id <- function(dates) {
hash <- digest(dates, hash_algo)
id <- paste0(id_prefix, substr(hash, 0, hash_length))
Expand Down
16 changes: 15 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ feed_contains <- function(gtfs_obj, table_name) {
(exists(".", where = gtfs_obj) && exists(table_name, where = gtfs_obj$.))
}

feed_has_non_empty_table <- function(gtfs_obj, table_name) {
if(exists(table_name, where = gtfs_obj)) {
if(nrow(gtfs_obj[[table_name]]) > 0) {
return(TRUE)
}
} else if(exists(".", where = gtfs_obj) && exists(table_name, where = gtfs_obj$.)) {
if(nrow(gtfs_obj[["."]][[table_name]]) > 0) {
return(TRUE)
}
}
return(FALSE)
}


#' Convert empty strings ("") to NA values in all gtfs tables
#'
#' @param gtfs_obj gtfs feed (tidygtfs object)
Expand Down Expand Up @@ -55,7 +69,7 @@ gather_dt = function(df_wide, new_key_colname, new_val_colname,
value_colnames) {
dt = as.data.table(df_wide)
dt_melted = data.table::melt(dt, measure.vars = value_colnames,
variable.name = new_key_colname, value.name = new_val_colname)
variable.name = new_key_colname, value.name = new_val_colname)

return(dt_melted)
}
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-service.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ test_that("set_servicepattern", {
)
g <- set_servicepattern(g)
expect_equal(length(unique(g$.$servicepatterns$servicepattern_id)), length(unique(gtfs_orig$calendar$service_id)))
g$calendar <- g$calendar[0,]
g$calendar_dates <- g$calendar_dates[0,]
expect_warning(set_servicepattern(g), "No dates defined in feed")
})

test_that("set_servicepattern w/ more params", {
Expand Down

0 comments on commit daf446f

Please sign in to comment.