Git Product home page Git Product logo

metro's Introduction

metro

Lifecycle: experimental CRAN status Downloads Codecov test coverage R build status

The goal of metro is to return data frames from the Washington Metropolitan Area Transit Authority API. Nested lists have been converted to tidy data frames when possible.

Installation

The release version of metro (0.9.1) can be installed from CRAN:

install.packages("metro")

Or install the development version from GitHub:

# install.packages("devtools")
devtools::install_github("k5cents/metro")

Key

Usage of the WMATA API requires a developer API key. Such a key can be obtained by creating a developer account and subscribing to the free default tier.

The WMATA also provides a demo key to try out the various features of the API. This key should never be used in production, it is rate limited and subject to change at any time.

Sys.setenv(WMATA_KEY = "e13626d03d8e4c03ac07f95541b3091b")

Example

library(metro)
packageVersion("metro")
#> [1] '0.9.3'

Functions return data frames for easy analysis.

next_train(StationCodes = "A01")
#> # A tibble: 6 × 9
#>     Car Destination DestinationCode DestinationName Group Line  LocationCode LocationName   Min
#>   <int> <chr>       <chr>           <chr>           <int> <chr> <chr>        <chr>        <int>
#> 1     8 Glenmont    B11             Glenmont            1 RD    A01          Metro Center    -1
#> 2     6 Shady Grove <NA>            Shady Grove         2 RD    A01          Metro Center     1
#> 3     6 Glenmont    B11             Glenmont            1 RD    A01          Metro Center     3
#> 4     6 Shady Grove <NA>            Shady Grove         2 RD    A01          Metro Center     7
#> 5     8 Glenmont    B11             Glenmont            1 RD    A01          Metro Center     9
#> 6     8 Shady Grove <NA>            Shady Grove         2 RD    A01          Metro Center    14

Coordinates

Use coordinates to find station entrances or bus stops near a location. The geodist::geodist() function is used to calculate distance from the supplied coordinates.

# Washington Monument coordinates
rail_entrance(Lat = 38.890, Lon = -77.035, Radius = 750)[, -(3:4)]
#> # A tibble: 6 × 5
#>   Name                                        StationCode   Lat   Lon Distance
#>   <chr>                                       <chr>       <dbl> <dbl>    <dbl>
#> 1 12TH ST NW & JEFERSON DR SW                 D02          38.9 -77.0     582.
#> 2 12TH ST SW & INDEPENDENCE AVE SW (ELEVATOR) D02          38.9 -77.0     612.
#> 3 12TH ST SW & INDEPENDENCE AVE SW            D02          38.9 -77.0     626.
#> 4 12TH ST NW & PENNSYLVANIA AVE NW            D01          38.9 -77.0     672.
#> 5 13TH ST NW & PENNSYLVANIA AVE NW (BUILDING) D01          38.9 -77.0     685.
#> 6 12TH ST NW & PENNSYLVANIA AVE NW (ELEVATOR) D01          38.9 -77.0     714.

Dates and Times

Date columns with class POSIXt have been shifted from Eastern time to the UTC time zone (+5 hours).

bus_position(RouteId = "33")[, 1:8]
#> # A tibble: 8 × 8
#>   VehicleID   Lat   Lon Distance Deviation DateTime            TripID   RouteID
#>   <chr>     <dbl> <dbl>    <dbl>     <dbl> <dttm>              <chr>    <chr>  
#> 1 7150       39.0 -77.1       NA         0 2024-03-12 01:51:23 31544020 33     
#> 2 4604       38.9 -77.0       NA         3 2024-03-12 01:51:17 25897020 33     
#> 3 7142       38.9 -77.1       NA         2 2024-03-12 01:51:37 46893020 33     
#> 4 4788       38.9 -77.1       NA         7 2024-03-12 01:51:39 12908020 33     
#> 5 4567       38.9 -77.0       NA         3 2024-03-12 01:51:39 12746020 33     
#> 6 4579       38.9 -77.1       NA         0 2024-03-12 01:51:24 16822020 33     
#> 7 7108       38.9 -77.0       NA         2 2024-03-12 01:51:38 25354020 33     
#> 8 4610       39.0 -77.1       NA         0 2024-03-12 01:51:19 13917020 33

Time values are left in Eastern time and are represented using the class hms, which counts the seconds since midnight. If the last train on a Saturday leaves at 1:21 AM (past midnight), this would be represented as 25:21.

tail(rail_times(StationCode = "E10"))
#> # A tibble: 6 × 7
#>   StationCode StationName DestinationStation Weekday OpeningTime FirstTime LastTime
#>   <chr>       <chr>       <chr>              <chr>   <time>      <time>    <time>  
#> 1 E10         Greenbelt   F11                Tue     04:50       05:00     23:30   
#> 2 E10         Greenbelt   F11                Wed     04:50       05:00     23:30   
#> 3 E10         Greenbelt   F11                Thu     04:50       05:00     23:30   
#> 4 E10         Greenbelt   F11                Fri     04:50       05:00     26:30   
#> 5 E10         Greenbelt   F11                Sat     06:50       07:00     26:30   
#> 6 E10         Greenbelt   F11                Sun     06:50       07:00     23:30

Data

Some data frames are includes as objects if their functions typically return the same thing every time.

metro_lines # rail_lines() for live
#> # A tibble: 6 × 5
#>   LineCode DisplayName StartStationCode EndStationCode InternalDestination
#>   <chr>    <chr>       <chr>            <chr>          <list>             
#> 1 BL       Blue        J03              G05            <chr [0]>          
#> 2 GR       Green       F11              E10            <chr [0]>          
#> 3 OR       Orange      K08              D13            <chr [0]>          
#> 4 RD       Red         A15              B11            <chr [2]>          
#> 5 SV       Silver      N06              G05            <chr [0]>          
#> 6 YL       Yellow      C15              E06            <chr [1]>

metro's People

Contributors

k5cents avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

metro's Issues

Cover rail parking endpoint

The multiple data frames returned (parking spots, costs, all day vs short term) make this endpoint hard to cover.

Maybe split into multiple functions? See #1.

API does not return station times

Docs: https://developer.wmata.com/docs/services/5476364f031f590f38092507/operations/5476364f031f5909e4fe3312

Code:

dat <- wmata_api(
  path = "Rail.svc/json/jStationTimes",
  query = list(StationCode = "A01"),
  level = 1,
  api_key = api_key
)

The FirstTrains and LastTrains values are empty.

{
  "StationTimes": [
    {
      "Code": "E10",
      "StationName": "Greenbelt",
      "Monday": {
        "OpeningTime": "04:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Tuesday": {
        "OpeningTime": "04:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Wednesday": {
        "OpeningTime": "04:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Thursday": {
        "OpeningTime": "04:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Friday": {
        "OpeningTime": "04:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Saturday": {
        "OpeningTime": "06:50",
        "FirstTrains": [],
        "LastTrains": []
      },
      "Sunday": {
        "OpeningTime": "07:50",
        "FirstTrains": [],
        "LastTrains": []
      }
    }
  ]
}

Add fee calculating function

Per page 7 of this document.

Metrorail Fare Structure (Effective July 1, 2017) Peak Off-Peak All Senior & DisabledFares are ½ Peak Fare
First 3 composite miles $2.25 $2.00 $1.10-3.00
Each additional composite mile more than 3 and less than or equal to 6 $0.33 $0.24
Each additional composite mile greater than 6 $0.29 $0.22
Maximum peak fare (Exclusive of Surcharge and Differentials) $6.00 $3.85 $3.00

API does not return train positions

curl -v -X GET "https://api.wmata.com/TrainPositions/TrainPositions?contentType={json}" -H "api_key: {subscription key}"
{
  "TrainPositions": []
}

Cover all rail and bus JSON endpoints

Cover all the JSON type services:

Bus Route and Stop Methods

  • Bus Position
  • Path details
  • Routes
  • Schedule
  • Schedule at stop
  • Stop search

GTFS

Not JSON:

The General Transit Feed Specification (GTFS), also known as GTFS static or static transit to differentiate it from the GTFS realtime extension, defines a common format for public transportation schedules and associated geographic information. GTFS "feeds" let public transit agencies publish their transit data and developers write applications that consume that data in an interoperable way.

Incidents

  • Bus Incidents
  • Elevator/Escalator Outages
  • Rail Incidents

Misc Methods

  • Validate API Key

Rail Station Information

  • Lines
  • Parking Information
  • Path Between Stations
  • Station Entrances
  • Station Information
  • Station List
  • Station Timings
  • Station to Station Information

Real-Time Bus Predictions

  • Next Buses

Real-Time Rail Predictions

  • Next Trains

Train Positions

Different API needed from the typical bus and trail JSON.

  • Live Train Positions
  • Standard Routes
  • Track Circuits

Develop Maintenance Functions for Rail & Bus

Great R package! It is believed to be critical to include a few functions that capture the maintenance schedules in real time for both rail and buses.

If machine learning is to be applied to rail and bus incidents, it would add a wonderful dimension to the accuracy of the prediction if maintenance schedules were also included.

For example, bus_maint and rail_maint functions could capture the work that is being done.

Perhaps a set of functions related to the future of planned maintenance work could also be included. Functions such as bus_maint_fut and rail_maint_fut come to mind.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.