Skip to content

Commit

Permalink
started filtering transfers from trip_id and route_id as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dhersz committed Nov 3, 2023
1 parent e13c1fc commit 4c79b6e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
6 changes: 4 additions & 2 deletions R/filter_by_agency_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -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%`)

Expand All @@ -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%`)

Expand Down
6 changes: 4 additions & 2 deletions R/filter_by_shape_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ 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 ->
# fare_attributes -> agency_id.
# 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)
Expand All @@ -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%`)

Expand Down
14 changes: 10 additions & 4 deletions R/filter_by_trip_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -61,14 +62,15 @@ 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 ->
# fare_attributes -> agency_id.
# 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)
Expand Down Expand Up @@ -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)
}
38 changes: 36 additions & 2 deletions R/filter_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
}

Expand Down
6 changes: 5 additions & 1 deletion R/gtfstools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)

0 comments on commit 4c79b6e

Please sign in to comment.