0

I am trying to access the real-time position of a bus from the GTFS data. I have followed the examples from here but the sample is different than the source I am looking to access. The transit provider I am looking to access has their real-time position as a .pb

Here is reprex

    library(readr)
    library(dplyr)
    library(RProtoBuf)
    
    readr::read_csv("https://storage.googleapis.com/storage/v1/b/mdb-csv/o/sources.csv?alt=media") |>
      dplyr::filter(mdb_source_id == 1607) |>
      dplyr::pull(urls.direct_download) |>
      download.file(destfile = "data/hfx_transit_realtime_locations.pd")

    RProtoBuf::read("GTFSv2.Realtime.Position", "data/hfx_transit_realtime_locations.pd")

Which produced the following error:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘read’ for signature ‘"character", "character"’

Following the example link from above I am not sure if this is the correct process as the provider I am looking to access lists out to a .pb, rather than a .proto.

URL = 'http://gtfs.halifax.ca/realtime/Vehicle/VehiclePositions.pb'`

Is there a different process or function calls to used for reading a .pb for GTFS? I made the assumption that using "GTFSv2.Realtime.Position" was standard for all the feeds within GTFS.

My ultimate goal is to be able to access and plot real-time positions from

'http://gtfs.halifax.ca/realtime/Vehicle/VehiclePositions.pb'
Ryan Garnett
  • 231
  • 2
  • 8
  • 1
    tidytransit cannot be used to read GTFS Realtime, only GTFS Schedule. – dhersz May 02 '23 at 16:23
  • 1
    As per the answer you linked to in the question, you first need to use `RProtoBuf::readProtoFiles()` to read the descriptors, and then use them to read the feed content (please note that you need to pass the descriptor names without `"`, which is what the error you posted refers to). – dhersz May 02 '23 at 16:31
  • Thank you @dhersz for your response. If I am understanding correctly, the link to the .proto is standard and not specific to the transit organization? – Ryan Garnett May 03 '23 at 18:19

1 Answers1

1

I tried following the instructions on the answer you posted in the question and it seems to have worked. I haven't worked with GTFS RT and don't know the specifics of the feed you used as an example, so I don't know if the result is actually correct (why 0 fields set?), but here's the code I used:

library(RProtoBuf)

download.file(
  "https://raw.githubusercontent.com/google/transit/master/gtfs-realtime/proto/gtfs-realtime.proto",
  destfile = "translink-gtfs-realtime.proto"
)
readProtoFiles("translink-gtfs-realtime.proto")

ls("RProtoBuf:DescriptorPool")
#>  [1] "rexp.CMPLX"                         "rexp.REXP"                         
#>  [3] "rexp.STRING"                        "rprotobuf.HelloWorldRequest"       
#>  [5] "rprotobuf.HelloWorldResponse"       "transit_realtime.Alert"            
#>  [7] "transit_realtime.EntitySelector"    "transit_realtime.FeedEntity"       
#>  [9] "transit_realtime.FeedHeader"        "transit_realtime.FeedMessage"      
#> [11] "transit_realtime.Position"          "transit_realtime.Shape"            
#> [13] "transit_realtime.TimeRange"         "transit_realtime.TranslatedImage"  
#> [15] "transit_realtime.TranslatedString"  "transit_realtime.TripDescriptor"   
#> [17] "transit_realtime.TripUpdate"        "transit_realtime.VehicleDescriptor"
#> [19] "transit_realtime.VehiclePosition"   "tutorial.AddressBook"              
#> [21] "tutorial.Person"

gtfs_url <- readr::read_csv(
  "https://storage.googleapis.com/storage/v1/b/mdb-csv/o/sources.csv?alt=media"
) |>
  dplyr::filter(mdb_source_id == 1607) |>
  dplyr::pull(urls.direct_download)
#> Rows: 1907 Columns: 23
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (11): data_type, location.country_code, location.subdivision_name, loca...
#> dbl   (5): mdb_source_id, location.bounding_box.minimum_latitude, location.b...
#> lgl   (6): entity_type, note, static_reference, urls.authentication_type, ur...
#> dttm  (1): location.bounding_box.extracted_on
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

download.file(gtfs_url, destfile = "hfx_transit_realtime_locations.pd")

read(transit_realtime.Position, "hfx_transit_realtime_locations.pd")
#> message of type 'transit_realtime.Position' with 0 fields set

Created on 2023-05-04 with reprex v2.0.2

dhersz
  • 525
  • 2
  • 8