
Collect GTFS-RT data from a Protocol Buffers feed at regular intervals
Source:R/rt_collect_protobuf.R
rt_collect_protobuf.RdCollect GTFS-RT data from a Protocol Buffers feed at regular intervals
Usage
rt_collect_protobuf(
gtfs_rt_url,
destination_file,
fields_collect = c("id", "vehicle.trip.trip_id", "vehicle.position.latitude",
"vehicle.position.longitude", "vehicle.position.speed", "vehicle.timestamp",
"vehicle.current_status", "vehicle.current_stop_sequence", "vehicle.stop_id"),
scrape_interval = 60,
log_file = NA,
headers = NULL
)Arguments
- gtfs_rt_url
String. URL of the Protocol Buffers GTFS-RT feed.
- destination_file
String. File to save the downloaded GTFS-RT data. Content is appended in each iteration.
- fields_collect
Character vector. Fields to extract from each entity in the feed.
- scrape_interval
Integer (Default 60). Interval in seconds between each download. Negative to run only once.
- log_file
String (Optional). Path to a log file to save download logs.
- headers
Named list or character vector (Optional). Custom HTTP headers for credentials when accessing the GTFS-RT feed URL.
Details
Downloads GTFS-RT data from the specified URL at regular intervals and saves them to the destination file.
This function will run indefinitely until manually stopped (CTRL + C).
Examples
# Create file
destination_file <- withr::local_tempfile(fileext = ".csv")
# Collect data
GTFShift::rt_collect_protobuf(
gtfs_rt_url = "https://go.tmlmobilidade.pt/hub/api/v1/realtime/vehicles/positions/gtfs.pb",
destination_file = destination_file,
scrape_interval = -1 # Negative to run only once
)
#> [2026-08-27 16:43:56] Starting GTFS-RT data collection from https://go.tmlmobilidade.pt/hub/api/v1/realtime/vehicles/positions/gtfs.pb
#> [20260827_164356] Iteration 1 completed
#> [1] "/tmp/RtmpGgUISp/file1f2163f253a7.csv"
# Read data
collection <- read.csv(destination_file)
names(collection)
#> [1] "id" "vehicle.trip.trip_id"
#> [3] "vehicle.position.latitude" "vehicle.position.longitude"
#> [5] "vehicle.position.speed" "vehicle.timestamp"
#> [7] "vehicle.current_status" "vehicle.current_stop_sequence"
#> [9] "vehicle.stop_id" "feed_timestamp"
#> [11] "feed_incrementality"
head(
collection |>
dplyr::select("vehicle.trip.trip_id", "vehicle.position.latitude", "vehicle.position.longitude")
)
#> vehicle.trip.trip_id vehicle.position.latitude
#> 1 [F1M13][A2L1N]4704_0_2|2700|1735 38.7862
#> 2 [F1M13][A2L1N]4600_0_1|2700|1700 38.6973
#> 3 [0277F][BNA17]2537_0_1|1|3|1740 38.8657
#> 4 [F1M13][A2L1N]4432_0_1|2700|1740 38.5262
#> 5 [F1M13][A2L1N]4704_0_2|2700|1720 38.7069
#> 6 [F1M13][A2L1N]4102_0_2|2700|1740 38.6809
#> vehicle.position.longitude
#> 1 -9.0912
#> 2 -8.9450
#> 3 -9.0603
#> 4 -8.8917
#> 5 -8.9605
#> 6 -8.9856