Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if input date is covered in the GTFS feed #326

Open
rafapereirabr opened this issue Mar 14, 2023 · 3 comments
Open

Check if input date is covered in the GTFS feed #326

rafapereirabr opened this issue Mar 14, 2023 · 3 comments
Assignees
Labels

Comments

@rafapereirabr
Copy link
Member

1) Briefly describe your problem and what output you expect

Problem

Every now and then, a user opens a new issue saying that r5r doest not output results accounting for the transit network. Fast forward, we help the user realize they were using a departure date that is outside the GTFS calendar. This has been a common issue in r5py, so they are implementing a simple input check.

Output expected

If user routes with a transit mode, we could test whether the date falls inside the calendar of the GTFS feeds used to build the network. If the input date falls outside the calendar, the function should throw and error with an informative message.

To make this work, it would be ideal if we could extract the start and end dates information of the service calendar cooked into the network built with setup_r5(). Something like this:

start <- r5r_core$service.calendar.start_date

end <- r5r_core$service.calendar.end_date

And then we can use this info to check against the departure_datetime input. @mvpsaraiva , is it possible to expose the r5r_core$service.calendar to R ?

@rafapereirabr
Copy link
Member Author

@mvpsaraiva , is it simple to expose r5r_core$service.calendar.start_date from the Java side ?

@rafapereirabr
Copy link
Member Author

If there are any potential contributors who know Java and who might want to have a look at this, we would appreciate PR from collaborators

@rafapereirabr
Copy link
Member Author

here's a simple R function that should do the trick:

check_transit_availability_on_date <- function(mode, departure_datetime){

  # get date in yyyy-mm-dd
  date_only <- format(departure_datetime, "%Y-%m-%d")
  
  # check services available
  services <- r5r_core$getTransitServicesByDate(date_only)
  services <- r5r:::java_to_dt(services)
  
  # count services available
  data.table::setDT(services)
  services_available <- services[, sum(active_on_date) / .N ] 
  
  if (services_available == 0) {
    cli::cli_abort("There are no transit services available on the selected departure 
               date: {.val {date_only}}. Please ensure your departure date falls 
               within the GTFS data calendar.")
  }
  
  
  if (services_available < 0.2) {
    cli::cli_alert_warning("Less than 20% of the transit services in the GTFS are running
                   on the selected departure.")
    }
}

To implement this in the pcakge, we need to insert the code below in our main routing / accessibility functions.

# check the availability of transit services on the departure date
if (any(c('TRANSIT', 'TRAM', 'SUBWAY', 'RAIL', 'BUS', 'FERRY', 
             'CABLE_CAR', 'GONDOLA', 'FUNICULAR') %in% mode)) {
  
  check_transit_availability_on_date(mode, departure_datetime)
  }
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants