Travel time with GTFS

352 views Asked by At

I have a GTFS file for Mexico City downloaded from transitland. Here is a link to the file I am using in my code.

I want to use the GTFS to calculate travel times. Nevertheless, the GTFS does not seem to work with any of the R-based libraries (gtfsrouter and tidytransit). I have not been able to get travel times between two locations, between two stations, or between a transit station to any other station.

With gtfsrouter:

library(gtfsrouter)
gtfs <- extract_gtfs ("MexicoCity_TransitLand.zip") # The GTFS loads ok without problem

gtfs <- gtfs_timetable (gtfs, day = "Wed")

gtfs_route (gtfs,
            from = "Las Torres",
            to = "Plaza Ermita",
            start_time = 120)

I get the error "Error in rcpp_csa(gtfs$timetable, gtfs$transfers, nrow(gtfs$stop_ids), : Index out of bounds: [index='from_stop_id']." I have tried trips for different locations, different days, and different times. Also, I have verified that the trips are doable.

I also tried:

from <- "Las Torres"
start_times <- 12 * 3600 + c (0, 60) * 60 # 8:00-9:00
res <- gtfs_traveltimes (gtfs, from, start_times)

But I get the following error: "Error: gtfs must have a transfers table; please use 'gtfs_transfer_table()' to construct one". So I tried:

gtfs_tt <- gtfs
gtfs_tt$transfers <- gtfs_transfer_table(gtfs_tt)

res <- gtfs_traveltimes (gtfs_tt, from, start_times)

But I got "Error in gtfs_traveltimes(gtfs_tt, from, start_times) : There are no scheduled services after that time." Again, I have tested different stations and different star times but I keep getting the same result.

With tidytransit

library(tidytransit)

gtfs <- read_gtfs("MexicoCity_TransitLand.zip")

gtfs <- cluster_stops(gtfs, 
                      group_col = "stop_name", 
                      cluster_colname = "stop_name")

stop_times <- filter_stop_times(gtfs, "2021-05-01")
tts <- travel_times(stop_times, "Las Torres", return_coords = TRUE)

I get "Error in travel_times(stop_times, "Las Torres", return_coords = TRUE) : Stops and transfers not found in filtered_stop_times attributes. Use filter_stop_times() to prepare data or use raptor() for lower level access." Again, I have tested different attributes.

An issue with the GFTS file?

I have been looking the GTFS files, and it might be an issue with the stop_times file. If you do

View(gtfs$stop_times)

You will notice that every trip starts at "00:00:00", so the information stored in this file is basically the travel time from the first station to any other station in the same trip. I think this is not consistent with the standard. In other GTFS I have seen that time reported in this file is the time at which the trip arrives at the station.

Am I doing something wrong? is the GTFS file wrong? is there any workaround?

1

There are 1 answers

0
Drew Dara-Abrams On

This feed includes a frequencies.txt file. This is used to specify a "headway" frequency at which a route runs between designated start and end times: https://gtfs.org/schedule/reference/#frequenciestxt

I am not familiar with those R libraries. However, it does look like gtfsrouter has a frequencies_to_stop_times function that may be worth a try: https://cran.r-project.org/web/packages/gtfsrouter/gtfsrouter.pdf#Rfn.frequencies.Rul.to.Rul.stop.Rul.times.1