diff --git a/R/filter_by_agency_id.R b/R/filter_by_agency_id.R index b462b76..c6ecbaa 100644 --- a/R/filter_by_agency_id.R +++ b/R/filter_by_agency_id.R @@ -63,8 +63,9 @@ filter_by_agency_id <- function(gtfs, agency_id, keep = TRUE) { relevant_routes <- unique(gtfs$routes$route_id) - # 'fare_rules' and 'trips' (route_id) + # 'fare_rules', 'trips' and 'transfers' (route_id) + gtfs <- filter_transfers_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_fare_rules_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_trips_from_route_id(gtfs, relevant_routes, `%chin%`) @@ -83,8 +84,9 @@ filter_by_agency_id <- function(gtfs, agency_id, keep = TRUE) { gtfs <- filter_calendar_from_service_id(gtfs, relevant_services, `%chin%`) gtfs <- filter_calend_dates_from_service_id(gtfs, relevant_services, `%chin%`) - # 'stop_times' and 'frequencies' (trip_id) + # 'stop_times', 'frequencies' and 'transfers' (trip_id) + gtfs <- filter_transfers_from_trip_id(gtfs, relevant_trips, `%chin%`) gtfs <- filter_frequencies_from_trip_id(gtfs, relevant_trips, `%chin%`) gtfs <- filter_stop_times_from_trip_id(gtfs, relevant_trips, `%chin%`) diff --git a/R/filter_by_shape_id.R b/R/filter_by_shape_id.R index a363027..b2382ec 100644 --- a/R/filter_by_shape_id.R +++ b/R/filter_by_shape_id.R @@ -61,7 +61,7 @@ filter_by_shape_id <- function(gtfs, shape_id, keep = TRUE) { gtfs <- filter_calendar_from_service_id(gtfs, relevant_services, `%chin%`) gtfs <- filter_calend_dates_from_service_id(gtfs, relevant_services, `%chin%`) - # 'routes' and 'fare_rules' (route_id) + # 'routes', 'fare_rules' and 'transfers' (route_id) # # we can use 'routes' to filter agency via routes -> agency_id, but we can # also use 'fare_rules' to filter it via fare_rules -> fare_id -> @@ -69,6 +69,7 @@ filter_by_shape_id <- function(gtfs, shape_id, keep = TRUE) { # so we create a 'relevant_agencies' vector that holds the relevant # agency_ids from both paths and use all of them to filter agency later. + gtfs <- filter_transfers_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_routes_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_fare_rules_from_route_id(gtfs, relevant_routes, `%chin%`) relevant_agencies_from_routes <- unique(gtfs$routes$agency_id) @@ -92,8 +93,9 @@ filter_by_shape_id <- function(gtfs, shape_id, keep = TRUE) { gtfs <- filter_agency_from_agency_id(gtfs, relevant_agencies, `%chin%`) - # 'stop_times' and 'frequencies' (trip_id) + # 'stop_times', 'frequencies' and 'transfers' (trip_id) + gtfs <- filter_transfers_from_trip_id(gtfs, relevant_trips, `%chin%`) gtfs <- filter_frequencies_from_trip_id(gtfs, relevant_trips, `%chin%`) gtfs <- filter_stop_times_from_trip_id(gtfs, relevant_trips, `%chin%`) diff --git a/R/filter_by_trip_id.R b/R/filter_by_trip_id.R index 263b892..38a7183 100644 --- a/R/filter_by_trip_id.R +++ b/R/filter_by_trip_id.R @@ -38,9 +38,10 @@ filter_by_trip_id <- function(gtfs, trip_id, keep = TRUE) { `%ffilter%` <- `%chin%` if (!keep) `%ffilter%` <- Negate(`%chin%`) - # 'trips', 'stop_times' and 'frequencies' can be filtered using 'trip_id' - # itself, so `%ffilter%` is used. the other files depend on relational - # associations with 'trip_id' that come from these 3 tables, so we use %chin% + # 'trips', 'stop_times', 'frequencies' and 'transfers' can be filtered using + # 'trip_id' itself, so `%ffilter%` is used. the other files depend on + # relational associations with 'trip_id' that come from these 3 tables, so we + # use %chin% # 'trips' (trip_id) @@ -61,7 +62,7 @@ filter_by_trip_id <- function(gtfs, trip_id, keep = TRUE) { gtfs <- filter_calendar_from_service_id(gtfs, relevant_services, `%chin%`) gtfs <- filter_calend_dates_from_service_id(gtfs, relevant_services, `%chin%`) - # 'routes' and 'fare_rules' (route_id) + # 'routes', 'fare_rules' and 'transfers' (route_id) # # we can use 'routes' to filter agency via routes -> agency_id, but we can # also use 'fare_rules' to filter it via fare_rules -> fare_id -> @@ -69,6 +70,7 @@ filter_by_trip_id <- function(gtfs, trip_id, keep = TRUE) { # so we create a 'relevant_agencies' vector that holds the relevant # agency_ids from both paths and use all of them to filter agency later. + gtfs <- filter_transfers_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_routes_from_route_id(gtfs, relevant_routes, `%chin%`) gtfs <- filter_fare_rules_from_route_id(gtfs, relevant_routes, `%chin%`) relevant_agencies_from_routes <- unique(gtfs$routes$agency_id) @@ -121,5 +123,9 @@ filter_by_trip_id <- function(gtfs, trip_id, keep = TRUE) { gtfs <- filter_frequencies_from_trip_id(gtfs, trip_id, `%ffilter%`) + # 'transfers' (trip_id) + + gtfs <- filter_transfers_from_trip_id(gtfs, trip_id, `%ffilter%`) + return(gtfs) } diff --git a/R/filter_helpers.R b/R/filter_helpers.R index e722163..d032243 100644 --- a/R/filter_helpers.R +++ b/R/filter_helpers.R @@ -168,9 +168,43 @@ filter_transfers_from_stop_id <- function(gtfs, relevant_stops, `%ffilter%`) { from_to_stop_id, rep("character", 2) ) + gtfs$transfers <- gtfs$transfers[from_stop_id %ffilter% relevant_stops] + gtfs$transfers <- gtfs$transfers[to_stop_id %ffilter% relevant_stops] + } + + return(gtfs) +} + +filter_transfers_from_route_id <- function(gtfs, relevant_routes, `%ffilter%`) { + if (gtfsio::check_field_exists(gtfs, "transfers", "from_route_id")) { + gtfsio::assert_field_class(gtfs, "transfers", "from_route_id", "character") + gtfs$transfers <- gtfs$transfers[ + from_route_id %chin% "" | from_route_id %ffilter% relevant_routes + ] + } + + if (gtfsio::check_field_exists(gtfs, "transfers", "to_route_id")) { + gtfsio::assert_field_class(gtfs, "transfers", "to_route_id", "character") + gtfs$transfers <- gtfs$transfers[ + to_route_id %chin% "" | to_route_id %ffilter% relevant_routes + ] + } + + return(gtfs) +} + +filter_transfers_from_trip_id <- function(gtfs, relevant_trips, `%ffilter%`) { + if (gtfsio::check_field_exists(gtfs, "transfers", "from_trip_id")) { + gtfsio::assert_field_class(gtfs, "transfers", "from_trip_id", "character") + gtfs$transfers <- gtfs$transfers[ + from_trip_id %chin% "" | from_trip_id %ffilter% relevant_trips + ] + } + + if (gtfsio::check_field_exists(gtfs, "transfers", "to_trip_id")) { + gtfsio::assert_field_class(gtfs, "transfers", "to_trip_id", "character") gtfs$transfers <- gtfs$transfers[ - from_stop_id %ffilter% relevant_stops & - to_stop_id %ffilter% relevant_stops + to_trip_id %chin% "" | to_trip_id %ffilter% relevant_trips ] } diff --git a/R/gtfstools.R b/R/gtfstools.R index 7398235..99cea02 100644 --- a/R/gtfstools.R +++ b/R/gtfstools.R @@ -97,6 +97,10 @@ utils::globalVariables( "feed_start_date", "feed_end_date", "dist_to_prev_point", - "shape_dist_traveled" + "shape_dist_traveled", + "from_route_id", + "to_route_id", + "from_trip_id", + "to_trip_id" ) )