A GTFS feed is a collection of csv files that stores a transit system’s scheduled operations as visible to riders. The specification is designed to be sufficient to provide trip planning functionality. An excellent visual explanation by OpenPlans of this system is provided in the figure below:

GTFS Summary

While, working on GTFS files, I realized that since there is no consistency among the kind of information that is provided by trip agencies, you need to do visual analysis of each before using them. Here, I’ve created an automated document creation module, that feeds in a folder containing data files from various agencies, and produces a documentation summary for each agency. It shows how the complete document creation process can be automated. The data can be obtained from GTFS data exchange.

library(ggmap)
library(RgoogleMaps)
library(googleVis)
library(rgdal)
library(sp)
library(ggplot2)
summaryData <- data.frame(Agency = character(), Routes= numeric(),
                          Shapes=numeric(),
                          trips = numeric(), stops = numeric(),
                          shapeFile = logical(),
                          Transportation = character(),
                          stringsAsFactors = FALSE )
GTFSDirs <- dir("~/Personal/blogData/san_fran_noShapeGTFS/",full.names = TRUE)
routeTypeDefs <- data.frame(c(0,1,2,3,4,5,6,7),
                            c("light rail","Subway or Metro",
                              "rail","bus","Ferry","cable car",
                              "gondola","funicular"))
colnames(routeTypeDefs)<- c("rId","transType")
for(dir in GTFSDirs){
  GTFSFileName <- dir
  agencyFile <-paste0(GTFSFileName,"/agency.txt")
  routeFile <- paste0(GTFSFileName,"/routes.txt")
  tripFile <- paste0(GTFSFileName,"/trips.txt")
  stoptimesFile <- paste0(GTFSFileName,"/stop_times.txt")
  stopsFile <- paste0(GTFSFileName,"/stops.txt")
  shapesFile <- paste0(GTFSFileName,"/shapes.txt")

  routesInfo <- read.csv(routeFile, header = TRUE)
  #shapesInfo <- read.csv(shapesFile, header = TRUE)
  tripInfo <- read.csv(tripFile, header = TRUE)
  stopInfo <- read.csv(stoptimesFile, header = TRUE)
  stopInformation <-read.csv(stopsFile, header = TRUE)
  agencyInfo <- read.csv(agencyFile,header = TRUE)
    
  cat("## ",as.character(agencyInfo$agency_name),"\n")
  #,unique(length(shapesInfo$shape_id))
  cat("The agency is serving a total of ",
      length(routesInfo$route_id)," routes.")
if(file.exists(shapesFile)){
  
  shapesInfo <- read.csv(shapesFile, header = TRUE)
  shapeLen <- length(unique(shapesInfo$shape_id))
    cat("The number of shapes in this agency are ",
        length(unique(shapesInfo$shape_id)))
    cat(" The trips that go through these shapes are ",
        length(unique(tripInfo$trip_id)),".")
  shapeFil <- TRUE
 
}else{
  cat(" The gtfs folder of this agency does not contains a shape file.")
  cat(" The trips that go through these routes are ",
      length(unique(tripInfo$trip_id)),".")
  shapeLen <- 0
  shapeFil <- FALSE
}
  cat("The number of stops served by all the routes in this agency are ",
      length(stopInformation$stop_id),".")
  
 routeTypes <- unique(routesInfo$route_type)
 
  cat("The type of transportation served by this agency is ",
      paste0(as.character(routeTypeDefs[routeTypeDefs$rId %in% routeTypes,
                                        "transType"]),collapse =  " and "),".")
cat("\n","Web URL: ",as.character(agencyInfo$agency_url),"\n")
  summaryData <- rbind.data.frame(
      summaryData,data.frame(Agency = as.character(agencyInfo$agency_name),
                             Routes = as.numeric(
                                 length(routesInfo$route_id)),
                             Shapes = shapeLen,
                             trips = as.numeric(
                                 length(unique(tripInfo$trip_id))) ,
                             stops = as.numeric(
                                 length(stopInformation$stop_id)),
                             shapeFile = shapeFil,
                             Transportation = paste0(as.character(
                                 routeTypeDefs[routeTypeDefs$rId %in% routeTypes,
                                               "transType"]),
                             collapse =  ",")))
#stopTemp <- stopInformation
#coordinates(stopInformation) <- ~stop_lon+stop_lat
#sf <- get_map(c(left= bbox(stopInformation)[1]-0.1,bottom=bbox(stopInformation)[2]-0.1, right=bbox(stopInformation)[3]+0.1,top=bbox(stopInformation)[4]+0.1), maptype = "terrain", source = "google")
latLong<-paste(stopInformation$stop_lat,stopInformation$stop_lon,sep = ":")
stopInformation$latLong <- latLong

#stopPlot <- stopInformation[,c("stop_name","stop_code","latLong")]
AhMap <- gvisMap(stopInformation, "latLong" , "stop_name", 
                     options=list(showTip=TRUE,
                                  showLine=TRUE, 
                                  enableScrollWheel=TRUE,
                                  mapType='terrain', 
                                  useMapTypeControl=TRUE))
print(AhMap,"chart")

#sfMap <-ggmap(sf) + geom_point(aes(x=stop_lon, y=stop_lat), color='blue', data = stopTemp,size = 1,alpha = 0.6) + xlab("longitude") #+ ylab("lattitude") + ggtitle(paste0("Stops served by ", as.character(agencyInfo$agency_name)))
#plot(sfMap)

cat("\n")
cat("\n")

 }

3D

The agency is serving a total of 17 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 564 .The number of stops served by all the routes in this agency are 622 .The type of transportation served by this agency is bus . Web URL: http://www.trideltatransit.com

AB

The agency is serving a total of 1 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 614 .The number of stops served by all the routes in this agency are 2 .The type of transportation served by this agency is bus . Web URL: http://www.bart.gov/guide/airport

AM

The agency is serving a total of 1 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 52 .The number of stops served by all the routes in this agency are 12 .The type of transportation served by this agency is rail . Web URL: http://www.capitolcorridor.org

AT

The agency is serving a total of 1 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 27 .The number of stops served by all the routes in this agency are 4 .The type of transportation served by this agency is Ferry . Web URL: http://www.angelislandferry.com

AY

The agency is serving a total of 1 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 26 .The number of stops served by all the routes in this agency are 19 .The type of transportation served by this agency is bus . Web URL: http://www.ridethevine.com/american-canyon-transit

BART

The agency is serving a total of 10 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 1452 .The number of stops served by all the routes in this agency are 44 .The type of transportation served by this agency is rail . Web URL: http://www.bart.gov

BG

The agency is serving a total of 3 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 80 .The number of stops served by all the routes in this agency are 7 .The type of transportation served by this agency is Ferry . Web URL: http://www.blueandgoldfleet.com/

CC

The agency is serving a total of 61 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 1645 .The number of stops served by all the routes in this agency are 1433 .The type of transportation served by this agency is bus . Web URL: http://www.cccta.org/

CE

The agency is serving a total of 2 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 10 .The number of stops served by all the routes in this agency are 7 .The type of transportation served by this agency is rail . Web URL: http://www.acerail.com/

Caltrain

The agency is serving a total of 4 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 188 .The number of stops served by all the routes in this agency are 64 .The type of transportation served by this agency is rail and bus . Web URL: http://www.caltrain.com/

Dumbarton Express

The agency is serving a total of 2 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 91 .The number of stops served by all the routes in this agency are 86 .The type of transportation served by this agency is bus . Web URL: http://www.dumbartonexpress.com

EM

The agency is serving a total of 4 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 288 .The number of stops served by all the routes in this agency are 37 .The type of transportation served by this agency is bus . Web URL: http://www.emerygoround.com

FS

The agency is serving a total of 14 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 635 .The number of stops served by all the routes in this agency are 73 .The type of transportation served by this agency is bus . Web URL: http://www.fasttransit.org

GF

The agency is serving a total of 2 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 108 .The number of stops served by all the routes in this agency are 3 .The type of transportation served by this agency is Ferry . Web URL: http://www.goldengateferry.org

GG

The agency is serving a total of 39 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 1657 .The number of stops served by all the routes in this agency are 766 .The type of transportation served by this agency is bus . Web URL: http://www.goldengatetransit.org

HF

The agency is serving a total of 3 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 36 .The number of stops served by all the routes in this agency are 2 .The type of transportation served by this agency is Ferry . Web URL: http://www.alcatrazcruises.com/index.aspx

Marin Transit

The agency is serving a total of 19 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 853 .The number of stops served by all the routes in this agency are 541 .The type of transportation served by this agency is bus . Web URL: http://www.marintransit.org/

MS

The agency is serving a total of 19 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 1308 .The number of stops served by all the routes in this agency are 133 .The type of transportation served by this agency is bus . Web URL: http://transportation.stanford.edu/marguerite/

PE

The agency is serving a total of 7 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 253 .The number of stops served by all the routes in this agency are 121 .The type of transportation served by this agency is bus . Web URL: http://cityofpetaluma.net/pubworks/transit-sub.html

RV

The agency is serving a total of 2 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 10 .The number of stops served by all the routes in this agency are 62 .The type of transportation served by this agency is bus . Web URL: http://www.riovistacity.com/transit/

SB

The agency is serving a total of 6 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 107 .The number of stops served by all the routes in this agency are 15 .The type of transportation served by this agency is bus and Ferry . Web URL: http://www.sanfranciscobayferry.com

VTA

The agency is serving a total of 92 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 8561 .The number of stops served by all the routes in this agency are 3872 .The type of transportation served by this agency is light rail and bus . Web URL: http://www.vta.org

SamTrans

The agency is serving a total of 75 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 3561 .The number of stops served by all the routes in this agency are 1972 .The type of transportation served by this agency is bus . Web URL: http://www.samtrans.com

SO

The agency is serving a total of 26 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 404 .The number of stops served by all the routes in this agency are 884 .The type of transportation served by this agency is bus . Web URL: http://www.sctransit.com

SR

The agency is serving a total of 17 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 683 .The number of stops served by all the routes in this agency are 456 .The type of transportation served by this agency is bus . Web URL: http://ci.santa-rosa.ca.us/departments/transit/CityBus/Pages/default.aspx

ST

The agency is serving a total of 15 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 688 .The number of stops served by all the routes in this agency are 374 .The type of transportation served by this agency is bus . Web URL: http://www.soltransride.com

UC

The agency is serving a total of 9 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 413 .The number of stops served by all the routes in this agency are 159 .The type of transportation served by this agency is bus . Web URL: http://www.uctransit.org

VC

The agency is serving a total of 6 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 277 .The number of stops served by all the routes in this agency are 107 .The type of transportation served by this agency is bus . Web URL: http://www.citycoach.com/index.cfm?page=cat&caid=16&cid=159

Vine (Napa County)

The agency is serving a total of 13 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 562 .The number of stops served by all the routes in this agency are 272 .The type of transportation served by this agency is bus . Web URL: http://www.ridethevine.com/vine

WestCAT

The agency is serving a total of 15 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 765 .The number of stops served by all the routes in this agency are 192 .The type of transportation served by this agency is bus . Web URL: http://www.westcat.org

WH

The agency is serving a total of 35 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 831 .The number of stops served by all the routes in this agency are 772 .The type of transportation served by this agency is bus . Web URL: http://www.wheelsbus.com

YV

The agency is serving a total of 1 routes. The gtfs folder of this agency does not contains a shape file. The trips that go through these routes are 32 .The number of stops served by all the routes in this agency are 7 .The type of transportation served by this agency is bus . Web URL: http://www.ridethevine.com/yountville-trolley

MTA New York City Transit

The agency is serving a total of 28 routes.The number of shapes in this agency are 224 The trips that go through these shapes are 19379 .The number of stops served by all the routes in this agency are 1482 .The type of transportation served by this agency is Subway or Metro and rail . Web URL: http://www.mta.info

San Francisco Municipal Transportation Agency

The agency is serving a total of 83 routes.The number of shapes in this agency are 2108 The trips that go through these shapes are 27285 .The number of stops served by all the routes in this agency are 3599 .The type of transportation served by this agency is light rail and bus and cable car . Web URL: http://www.sfmta.com

#rsave(summaryData,file="summaryData.Rda")
cat("### Tabular Summary of Agencies")

Tabular Summary of Agencies

AgenTable <- gvisTable(summaryData, options=list(page='enable'))
print(AgenTable,"chart")