Tidytransit Linking GTFS Stop Ids and Routes

Tidytransit is a really nice and helpful package in R dealing with transit data, to be specific, GTFS datasets. However, there is one function missing: linking stop ids to route ids. For some GTFS feeds from some of the transit agencies, there are no columns or information about corresponding route(s) for each stop. This can be fixed by using left_join function after we understand the data structure of GTFS feeds:

Then, we know that using the three ids: Route ID, Trip ID, Stop ID, we can link the routes with the stops.

First of all, use read_gtfs function to import the GTFS file:

  gtfs <- read_gtfs("Your_GTFS_Zip_Directory")

Then, we can create a function to take out the tables that we need to use here, and join them accordingly.

route_stop_link <- function(gtfs){routes <- gtfs$routes 
trips <- gtfs$trips 
stop_times <- gtfs$stop_times 
stops <- gtfs$stops 
links <- routes %>% 
  merge(trips %>% select(trip_id, route_id, shape_id), all=TRUE ) %>% 
  merge(stop_times %>% select(trip_id, stop_id), all=TRUE ) %>% 
  merge(stops, all=TRUE ) %>% group_by(stop_id, route_id) %>% slice(1) 
links 
}

Let’s test this out by using Washington D.C. WMATA/Washington Metropolitan Area Transit Authority GTFS from April 16, 2020. (data downloaded from here )

The result table head is like this:

Lastly, I want to recommend this ArcGIS toolbox. If you are using ESRI GIS product, and you simply want to convert GTFS zip files into route shapefile, this tool is the way to go. Just import the tool without installation, and the user interface is the same as all other Arc Tool Box tools.