Skip to content

An R package for constructing and modelling a transit network in real time to obtain vehicle ETAs

Notifications You must be signed in to change notification settings

tmelliott/transitr

Repository files navigation

transitr

Travis build status codecov

The goals of transitr are to make it easy to load GTFS data into a database, construct a transit network of roads and intersections, and model vehicles in real-time from an API feed to update the network and generate ETAs.

install

transitr is not (yet) on CRAN, so for you would need to use devtools:

devtools::install_github('tmelliott/transitr')

usage

Still under development! This here is just for demonstration of what it could be like at some point in the future.

library(transitr)
library(magrittr)

## Create a database, construct network, and connect to a realtime feed
dbname <- "realtime.db"
nw <- create_gtfs("https://cdn01.at.govt.nz/data/gtfs.zip", db = dbname) %>%
    construct() %>%
    realtime_feed("https://api.at.govt.nz/v2/public/realtime/vehiclelocations",
                  with_headers("Ocp-Apim-Subscription-Key" = "mykey"),
                  response = "protobuf")

## Set the parameters and then run the model
nw %>% 
    set_parameters(n_core = 2, 
                   n_particles = 500, 
                   gps_error = 5) %>%
    model()

Once running, you can launch a new R session and view the shiny app:

transitr::view_realtime("realtime.db")

mock data server

In order to facilitate model development and checking, there's also a mock data server in the simulations directory.

To install:

cd simulations
yarn 

## or if you don't use yarn
npm install

To start the server, you need first an archive of vehicle position feeds,

ls archive | grep vehicle | head -n 5
# vehicle_locations_20180911050001.pb
# vehicle_locations_20180911050031.pb
# vehicle_locations_20180911050102.pb
# vehicle_locations_20180911050132.pb
# vehicle_locations_20180911050201.pb

yarn start
# yarn run v1.9.4
# $ node mock_server.js
# Mock GTFS server running on port 3000!

Now you can run the model with the local server, which will automatically serve the next file with each request.

## assumeing you've constructed with simulation flag:
## $ make FLAGS="-DSIMULATION"
## simulation history will be saved in a `history` directory
dir.create("history")

## set some process ID for the server to recognise (allows running multiple simulations simultaneously)
pid <- "test1"
nw <- load_gtfs("fulldata.db") %>%
    realtime_feed(sprintf("http://localhost:3000/%s/vehicle_positions", pid),
                  response = "protobuf") %>%
    set_parameters(n_core = 1,
                   n_particles = 2000,
                   gps_error = 10)

nw %>% model()

About

An R package for constructing and modelling a transit network in real time to obtain vehicle ETAs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published