## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  message = FALSE,
  warning = FALSE,
  eval = FALSE,
  comment = "#>"
)

## ----gtfs-rt-example----------------------------------------------------------
# # TransLink Vancouver GTFS-RT
# gtfs_rt_yvr <- feeds(provider = "TransLink Vancouver", data_type = "gtfs_rt")
# 
# # read URL for GTFS-RT vehicle position
# mobdb_feed_url(gtfs_rt_yvr$id[1])
# 
# # TransLink, like many agencies, requires authentication for their RT API
# # Get authentication information from Mobility Database
# yvr_rt_auth <- gtfs_rt_yvr$source_info[1, ]
# 
# # Get column names
# names(yvr_rt_auth)
# 
# # Get registration URL
# yvr_rt_auth$authentication_info_url
# 
# # Get API key parameter
# yvr_rt_auth$api_key_parameter_name

## ----gtfs-rt-protobuf, eval = FALSE-------------------------------------------
# library(RProtoBuf)
# 
# gtfs_rt_example <- feeds(provider = "sample-agency", data_type = "gtfs_rt")
# 
# # read URL for GTFS-RT vehicle position
# example_url <- mobdb_feed_url(gtfs_rt_example$id[1])
# 
# # Download and read the GTFS-RT .proto definition
# # Available at: https://gtfs.org/documentation/realtime/gtfs-realtime.proto
# proto_url <- "https://raw.githubusercontent.com/google/transit/master/gtfs-realtime/proto/gtfs-realtime.proto"
# proto_file <- tempfile(fileext = ".proto")
# download.file(proto_url, proto_file, quiet = TRUE)
# readProtoFiles(proto_file)
# 
# # Fetch and parse the feed
# con <- url(gtfs_rt_example, "rb")
# feed <- read(transit_realtime.FeedMessage, con)
# close(con)
# 
# # Access vehicle positions
# for (entity in feed$entity) {
#   if (entity$has("vehicle")) {
#     vehicle <- entity$vehicle
#     cat("Vehicle ID:", vehicle$vehicle$id, "\n")
#     cat("Position:", vehicle$position$latitude, ",", vehicle$position$longitude, "\n")
#     cat("Timestamp:", vehicle$timestamp, "\n\n")
#   }
# }

## ----gbfs-example-------------------------------------------------------------
# # Search GBFS feeds in Vancouver
# gbfs_yvr <- mobdb_search("vancouver", data_type = "gbfs")
# 
# yvr_feed <- mobdb_feed_url(gbfs_yvr$id[1])
# 
# yvr_feed

## ----gbfs-package-example, eval=FALSE-----------------------------------------
# library(gbfs)
# 
# gbfs_yvr <- mobdb_search("vancouver", data_type = "gbfs")
# yvr_feed <- mobdb_feed_url(gbfs_yvr$id[1])
# yvr_station_info <- get_station_information(yvr_feed, output = "return")

