This module analyzes real-time transit vehicle data to determine schedule adherence—how well vehicles are following their scheduled timetables. It compares actual arrival and departure times with scheduled times to calculate adherence metrics.
The Schedule Adherence Module processes real-time GTFS vehicle position data and compares it with scheduled timetables to:
- Determine if vehicles are running on their assigned routes.
- Calculate arrival and departure adherence at stops (early, on-time, or late).
- Track trip completion percentages.
- Generate detailed adherence reports for transit operators.
- Real-time monitoring of vehicle positions via GTFS-RT feeds.
- Comparison of actual vs. scheduled route assignments.
- Calculation of schedule adherence at the start and end of trips.
- Trip completion tracking.
- JSON output for easy integration with other systems.
- Python 3.7+
- Dependencies listed in
requirements.txt
-
Clone the repository:
git clone https://github.com/transport-stack/schedule-adherence.git cd schedule-adherence -
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile:Create a
.envfile in the root directory by copying the example file:cp .env.example .env
Update the
.envfile with your specific API endpoints and configuration:# API Endpoints DEPOT_TOOL_URL=https://api.example.com/depot_tool_duty_master VEHICLE_AGENCY_URL=https://api.example.com/vehicle_agency_data REALTIME_API_URL=https://api.example.com/realtime_gtfs_feed # File Paths ROUTES_FILE=route_comp/routes.txt TREE_DICT_FILE=route_comp/tree_dict.pkl # Timezone Settings TIMEZONE=Asia/Kolkata -
Directory Structure:
The module requires a
route_compdirectory with the following structure:route_comp/ ├── data/ # Directory for storing data files ├── routes.txt # GTFS routes file ├── tree_dict.pkl # Pickle file containing route trees └── vehicle_data.json # Vehicle metadata
The application runs in two main stages:
-
Real-time Data Processing (
realtime_processor.py):- Fetches real-time vehicle positions from the GTFS-RT feed.
- Downloads and updates vehicle and depot data.
- Processes the data and stores it in a local SQLite database (
route_comp/data/route_comp_{YYYY_MM_DD}.db). - Generates a JSON file (
route_comp/actualVsScheduled.json) that compares actual vs. scheduled routes.
-
Schedule Adherence Calculation (
adherence_calculator.py):- Reads the processed data from the SQLite database.
- Calculates schedule adherence metrics, including start/end trip adherence and stop-level adherence.
- Generates a detailed JSON report (
route_comp/SCHEDULE_ADHERENCE.json).
To run the complete pipeline, execute the scripts in the following order:
-
Run the real-time processor:
python realtime_processor.py
-
Run the adherence calculator:
python adherence_calculator.py
The module generates the following output files:
-
route_comp/actualVsScheduled.json: A JSON file containing the comparison of actual vs. scheduled route assignments. -
route_comp/SCHEDULE_ADHERENCE.json: A detailed schedule adherence report for each trip. The format is as follows:{ "vehicle": { "id": "VEHICLE_ID", "is_ac": true/false, "fuel_type": "electric/cng", "depot": { "name": "DEPOT_NAME", "agency": "AGENCY_NAME" } }, "trip_completion": 100.0, "pb_trip_id": "TRIP_ID", "route_id": "ROUTE_ID", "route_short_name": "ROUTE_SHORT_NAME", "route_long_name": "ROUTE_LONG_NAME", "actual": { "start_timestamp": "YYYY-MM-DDThh:mm:ss+05:30", "end_timestamp": "YYYY-MM-DDThh:mm:ss+05:30" }, "scheduled": { "start_timestamp": "YYYY-MM-DDThh:mm:ss+05:30", "end_timestamp": "YYYY-MM-DDThh:mm:ss+05:30" }, "start_adherence_in_seconds": 120, "end_adherence_in_seconds": 180, "stops": [ { "id": "STOP_ID", "name": "STOP_NAME", "actual_arrival": "YYYY-MM-DDThh:mm:ss+05:30", "scheduled_arrival": "YYYY-MM-DDThh:mm:ss+05:30", "actual_departure": "YYYY-MM-DDThh:mm:ss+05:30", "scheduled_departure": "YYYY-MM-DDThh:mm:ss+05:30", "arrival_adherence_in_seconds": 120, "departure_adherence_in_seconds": 120 } ] }
[Your License Here]