The Delaware Valley Regional Planning Commission (DVRPC) partnered with the City of Philadelphia’s Office of Transportation and Infrastructure Systems (OTIS) to create a guide to identify and diagnose sources of bus delay on a roadway segment or corridor and recommend a design intervention to remedy the delay. This guide is designed to be used by OTIS's internal and external partners.
Part 3, step 4 of the guide, instructs highlighting abnormal speed patterns. This repository includes the code and instructions for this step.
Speed patterns refer to code-based simplified/abstracted reading of the AVL data available in Swift.ly and are calculated separately for each predefined time-interval.
The high-resolution AVL data is provided in segments that are the same in length so that between each two stops on a route, the overall number of segments can differ. Each segment includes a numeric median speed value. Using Swift.ly-based speed buckets, the code first translates these buckets into text form. Then, it simplified each between-stop overall path into a pattern regardless of the count of segments.
Here is a (simplified) example:
graph LR;
A[ stop 001_to_002: high-med-med-low-high-high ]-->B[Pattern_A: high-med-low-high];
C[ stop 002_to_003: high-high-med-med-low-low-high ]-->B;
D[ stop 003_to_004: high-med-med-low-med-med ]-->E[Pattern_B: high-med-low-med];
Note
The current speed thresholds in the code are taken from Swift.ly definitions. To modify the thresholds or the overall amount of buckets, edit part 1 R code, lines 18-25 add_speed_med_def function.
Abnormal patterns are decided at the planning team's discretion while accounting for the specific corridor reviewed. Generally, buses gain speed after each stop and reduce speed near the next one. Abnormal patterns would be ones where there is one (or more) decline in speed midway. For example:
To read more about abnormal patterns, please review the full how-to guide.
-
Clone the repository
-
Download and install R from https://www.r-project.org/
-
Download and install R Studio from https://www.rstudio.com/products/rstudio/#Desktop
-
Open the code files directly in R studio. If needed, push commits through GitHub desktop.
-
Create the .Renviron files
The .Renviron file includes these two secret paths in this format:import_folder="path/to/import_folder/with_files" export_folder="path/to/export_folder"
The .Renviron file can be created in R studio through Files -> New Files -> Text File (and set up the .Renviron as type when saving.)
Alternatively, download the .Renviron.Example file to the folder from the repository, edit the secrets, and rename the file to .Renviron.The beginning of each code file calls on the Renviron within its folder, then calls the relevant secrets:
readRenviron(paste0(dirname(rstudioapi::getActiveDocumentContext()$path), "/.Renviron")) import_folder <- Sys.getenv("import_folder") export_folder <- Sys.getenv("export_folder")
-
Install all libraries before running them for the first time:
install.packages("name_of_package") library(name_of_package)
These two code files utilize these three R packages:
- dplyr
- sf
- tidyverse
- Set up (create) the import and export folders for each specific bus route and direction reviewed
- Populate the import folder with these files:
- Five CSV files
- Download five CSV files (per direction, per route) from the Swift.ly speed map page. Each file should align with the preferred time-intervals buckets. This step requires an active Swift.ly user. For further instructions regarding manual Swift.ly downloads and/or time intervals, please review Appendix 1 of the full how-to guide.
- Name the files early_am, am_rush, midday, pm_rush & evening.
- Download five CSV files (per direction, per route) from the Swift.ly speed map page. Each file should align with the preferred time-intervals buckets. This step requires an active Swift.ly user. For further instructions regarding manual Swift.ly downloads and/or time intervals, please review Appendix 1 of the full how-to guide.
- Septa routes points shapefile
Can be downloaded from the DVRPC data catalog at: https://catalog.dvrpc.org/dataset/septa-transit-stops
- Five CSV files
- Follow the setup steps above, including cloning the repository and creating the .Renviron file.
- Run ‘part 1’ R code:
- Open part1_patterns_time_matrix_and_dict.r from the cloned repository directly in Rstudio
- Run the code
- Expected output to export folder: pattern_time_matrix.csv, pattern_dictionary.csv
- Manually flag abnormal patterns in the exported pattern dictionary CSV file.
Write 'T' under the flagged_pattern column to flag a pattern, then save the file. Flagging is to be carried by the planning/reviewing team's discretion. See the overall how-to guide by DVRPC's OTBP team for recommendations and a detailed explanation. - Run ‘part 2’ R code:
- Open part2_routes_and_matrix.r from the cloned repository directly in Rstudio
- Run the code
- Expected output to export folder: matrix_mapped.shp, flagged_matrix.csv
Note
If a segment had missing (NA) values for at least one of the five time-intervals buckets, it will be dropped and will not appear in either of the outputted files
The second code produces a shapefile that allows mapping the abnormal pattern results. To do so, open the file using GIS software, such as ArcGIS Pro. In ArcGIS Pro, you can then add a definition query to the layer to present only the abnormal segments (all or from a specific time of day; modify the statement as needed).
It is useful to add SEPTA's stop points and SEPTA's route line shapefiles to this map to see (and possibly label) the specific route's stops. This map can be used to select points for direct observations for the corridor study.
Note
- The code connects each two stops with a linear line. Where there are curves (for example, the bus is turning around a square) the lines will not reflect it. So, it is helpful to add the overall SEPTA's route line, that is not divided into segments, as a background layer to the map
- Some segments that could be flagged as abnormal are with irregular road shape (a curve the bus needs to slow down in), or with a visible intersecting road that might cause the slow down middway. Review the outputted shapefile on a map to possibly eliminate these as direct observation locations.
In addition to the shapefile, you can review the flagged chart without GIS software using this file.
stops_id | early_am | am_rush | midday | pm_rush | evening | early_am_t | am_rush_t | midday_t | pm_rush_t | evening_t |
---|---|---|---|---|---|---|---|---|---|---|
001_to_002 | pattern | pattern | pattern | pattern | pattern | FALSE | TRUE | TRUE | FALSE | FALSE |
Columns two to six of the output table provide the speed pattern of a specific segment throughout the day. The last five columns indicate if the team flagged one or more of the time intervals as abnormal. The file allows for filtering abnormal segments and conditional formatting of results if needed.