
Collect GTFS-RT data from a JSON feed at regular intervals
Source:R/rt_collect_json.R
rt_collect_json.RdCollect GTFS-RT data from a JSON feed at regular intervals
Usage
rt_collect_json(
gtfs_rt_url,
destination_file,
header_key = "header",
entity_key = "entity",
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 GTFS-RT feed in JSON format.
- destination_file
String. File to save the downloaded GTFS-RT data. Content is appended in each iteration.
- header_key
String (Default "header"). Key in the JSON corresponding to the feed header. Set to NA if not present.
- entity_key
String (Default "entity"). Key in the JSON corresponding to the feed entities. Set to NA if response is a flat list. Use "." for nested keys.
- fields_collect
Character vector. Fields to extract from each entity in the feed. Use "." for nested keys.
- 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_json(
gtfs_rt_url = "https://go.tmlmobilidade.pt/hub/api/v1/realtime/vehicles/positions/gtfs",
entity_key = "data.entity",
destination_file = destination_file,
scrape_interval = -1 # Negative to run only once
)
#> [2026-10-09 13:35:41] Starting GTFS-RT data collection from https://go.tmlmobilidade.pt/hub/api/v1/realtime/vehicles/positions/gtfs
#> [20261009_133541] Iteration 1 completed
#> [1] "/tmp/RtmpkECFtw/file1e782da4859e.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"
head(
collection |>
dplyr::select("vehicle.trip.trip_id", "vehicle.position.latitude", "vehicle.position.longitude")
)
#> vehicle.trip.trip_id vehicle.position.latitude
#> 1 [F34NX][YA15B]3521_0_1_1400_1429_0_ESC_DU 38.58020
#> 2 [F34NX][YA15B]3710_0_2_1400_1429_0_ESC_DU 38.71818
#> 3 [VNWG3][LA77N]1208_0_2_1400_1429_0_1 38.80648
#> 4 [VNWG3][LA77N]1241_0_1_1400_1429_0_1 38.80792
#> 5 [VNWG3][LA77N]1726_0_2_1430_1459_0_1 38.71920
#> 6 [VNWG3][LA77N]1232_0_3_1400_1429_1_1 38.79764
#> vehicle.position.longitude
#> 1 -9.153893
#> 2 -9.175030
#> 3 -9.316980
#> 4 -9.349970
#> 5 -9.263113
#> 6 -9.340896