knitr::opts_chunk$set(echo = TRUE) library(tidytransit) library(dplyr)
Use tidytransit to:
This package requires a working installation of sf.
# Once sf is installed, you can install from CRAN with: install.packages('tidytransit') # For the development version from Github: # install.packages("devtools") devtools::install_github("r-transit/tidytransit")
The summary page for the GTFS standard is a good resource for more information on the standard.
GTFS feeds contain many linked tables about published transit schedules, service schedules, trips, stops, and routes. Below is a diagram of these relationships and tables:
Source: Wikimedia, user -stk.
GTFS data come packaged as a zip file of comma separated tables in text form. The first thing tidytransit does is consolidate the reading of all those tables into a single R object, which contains a list of the tables in each feed.
Below we use the tidytransit read_gtfs
function in order to read a feed from the NYC MTA
into R.
We use an example feed included in the package in the example below. But note that you can read directly from the New York City Metropolitan Transit Authority, as shown in the commented code below.
You can also read from any other URL. This is useful because there are many sources for GTFS data, and often the best source is transit service providers themselves. See the next section on "Finding More GTFS Feeds" for more sources of feeds.
# nyc <- read_gtfs("http://web.mta.info/developers/data/nyct/subway/google_transit.zip") local_gtfs_path <- system.file("extdata", "nyc_subway.zip", package = "tidytransit") nyc <- read_gtfs(local_gtfs_path)
You can use summary
to get an overview of the feed.
summary(nyc)
Each of the source tables for the GTFS feed is now available in the nyc gtfs
object. For
example, stops:
head(nyc$stops)
The tables available on each feed may vary. Below we can simply print the names of all the tables that were read in for this feed. Each of these is a table.
names(nyc)
When reading a feed, it is checked against the GTFS specification, and an attribute is
added to the resultant object called validation_result
, which is a tibble about the files
and fields in the GTFS feed and how they compare to the specification.
You can get this tibble from the metadata about the feed.
validation_result <- attr(nyc, "validation_result") head(validation_result)
You can find more feeds on the following sites
There might be other feed registries available, depending on your area of interest. Transit agencies might also provide their feeds on their on website.
MobilityData.org provide a csv file with all their feed sources on their GitHub page.
You can load it in R and browse it as a data frame. Tidytransit cannot handle GTFS realtime data, so we can remove those entries.
MobilityData.csv = read.csv("https://storage.googleapis.com/storage/v1/b/mdb-csv/o/sources.csv?alt=media") MobilityData_feedlist = MobilityData.csv %>% as_tibble() %>% filter(data_type == "gtfs") str(MobilityData_feedlist)
There is a URL (column urls.direct_download
) for each feed, which
can be used to read the feed for a given area directly into R. For example:
gtfs_path_goldengate <- MobilityData_feedlist %>% filter(provider == "Golden Gate Transit") %>% pull(urls.direct_download) gtfs_goldengate = read_gtfs(gtfs_path_goldengate)
The MobilityData feedlist contains bounding box coordinates for a feed which can be used to give a basic impression of the location of feeds. Note that some bounding boxes might be wrong.
First, let's create bounding box polygons.
# create a bounding box polygon from min/max corner coordinates suppressPackageStartupMessages(library(sf)) bbox_polygon = function(lon_min, lon_max, lat_min, lat_max) { corner_coords = matrix( c(lon_min, lat_min, lon_min, lat_max, lon_max, lat_max, lon_max, lat_min, lon_min, lat_min), ncol = 2, byrow = T ) polyg = st_polygon(list(corner_coords)) return(st_sfc(polyg, crs = 4326)) } # create bounding box polygon (only for reasonable values) MobilityData_sf = MobilityData_feedlist %>% filter(!is.na(location.bounding_box.minimum_longitude)) %>% filter(location.bounding_box.minimum_latitude > -89) %>% group_by(mdb_source_id) %>% mutate(geometry = bbox_polygon(location.bounding_box.minimum_longitude, location.bounding_box.maximum_longitude, location.bounding_box.minimum_latitude, location.bounding_box.maximum_latitude)) %>% ungroup() %>% st_as_sf()
These boxes could now be shown on an interactive map.
library(leaflet) leaflet() %>% addProviderTiles(provider = providers$CartoDB.Positron) %>% addPolygons(data = MobilityData_sf, weight = 2, fillOpacity = 0.1, label = substr(MobilityData_sf$provider, 0, 60))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.