Python package for processing GTFS feeds to assemble bus blocks and relevant data
Project description
GTFS Blocks
gtfsblocks is a Python package that pieces together GTFS feed data to assemble individual vehicle blocks and compile relevant data from various tables. The code was originally developed to analyze transit bus electrification, but the functionality can be helpful to other applications as well. The package was predominantly built off of the GTFS processing code from ebusopt.
Some core functions include:
- Reading static GTFS tables into Pandas
DataFrameobjects and performing a bit of basic validation that necessary columns are populated while dropping what isn't needed. - Parsing the
calendar.txtandcalendar_dates.txtfiles to identify the activeservice_idvalues on each day of service. - Merging together trip-level data from different GTFS tables for easy manipulation and analysis. For example:
- Adding trip start and end times from
stop_times.txt - Adding trip start and end locations from
shapes.txt- Esimating deadhead distances between consecutive trips based on these coordinates
- Adding trip distances calculated from the lat/lon coordinates in
shapes.txt
- Adding trip start and end times from
See this gist for an overview of core functionality as well as the example usage below. Documentation is a work in progress.
Installation
gtfsblocks is installable via pip:
pip install gtfsblocks
Example Usage
It's easy to read in a GTFS feed with gtfsblocks. Just supply the path to the directory where unzipped GTFS files are housed:
from gtfsblocks import Feed
gtfs = Feed.from_dir('/path/to/your/data')
This will load all relevant files into memory as Pandas DataFrames. Future releases may take advantage of partridge for better memory management.
From here, you can access predictably named tables like gtfs.trips_df or gtfs.stop_times_df, or call various methods on Feed to perform some transformations and aggregations for you.
Getting active trips on a particular day
# Get a Pandas Series of the number of trips per day in the scope of these files
trips_per_day = gtfs.get_n_trips_per_day()
# Filter down trips.txt to just those happening on a particular day
test_date = '2/25/25'
day_trips = gtfs.get_trips_from_date(test_date)
Only include blocks serving a specific set of routes
from gtfsblocks import filter_blocks_by_route
routes = ['D Line', 'E Line']
route_trips = filter_blocks_by_route(
trips=day_trips,
routes=routes,
route_method=route_method,
route_column='route_short_name'
)
Add data from other GTFS tables to trips DataFrame
# Add all trip data columns (e.g. locations and distances)
route_trips = gtfs.add_trip_data(route_trips, test_date)
Estimate deadhead distance between trips
from gtfsblocks import add_deadhead
trips_with_dh = add_deadhead(route_trips)
Plot the trips on an interactive Plotly map
from gtfsblocks import plot_trips_and_terminals
fig = plot_trips_and_terminals(
trips_df=route_trips,
shapes_df=gtfs.shapes_df
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gtfsblocks-0.1.0.tar.gz.
File metadata
- Download URL: gtfsblocks-0.1.0.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5b78ed72c9914291b0db7af4bc0a04a3324828f8354401596add90a05c1d231
|
|
| MD5 |
163b7efd101ed7d9a320e4282a49388f
|
|
| BLAKE2b-256 |
8e2db8158d7d8a72a9cc1e0623f3b8b97690b56d2176ec59799ed212c43d6c16
|
Provenance
The following attestation bundles were made for gtfsblocks-0.1.0.tar.gz:
Publisher:
python-publish.yml on dan-mccabe/gtfsblocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gtfsblocks-0.1.0.tar.gz -
Subject digest:
e5b78ed72c9914291b0db7af4bc0a04a3324828f8354401596add90a05c1d231 - Sigstore transparency entry: 175504492
- Sigstore integration time:
-
Permalink:
dan-mccabe/gtfsblocks@8ae2f87a8d4bd099842db61bd2a75e229c7559d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dan-mccabe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8ae2f87a8d4bd099842db61bd2a75e229c7559d4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gtfsblocks-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gtfsblocks-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60ab1674d26a3fd279474e2850e4e342bbb0596553c632b7a166f201d5248ccf
|
|
| MD5 |
2531747d9d90887ecb792fe0a38a836b
|
|
| BLAKE2b-256 |
e42db12f777d9fbdfc4e6868627d2b439684de9a1a6fecac15ba8ad493fea323
|
Provenance
The following attestation bundles were made for gtfsblocks-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on dan-mccabe/gtfsblocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gtfsblocks-0.1.0-py3-none-any.whl -
Subject digest:
60ab1674d26a3fd279474e2850e4e342bbb0596553c632b7a166f201d5248ccf - Sigstore transparency entry: 175504494
- Sigstore integration time:
-
Permalink:
dan-mccabe/gtfsblocks@8ae2f87a8d4bd099842db61bd2a75e229c7559d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dan-mccabe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8ae2f87a8d4bd099842db61bd2a75e229c7559d4 -
Trigger Event:
release
-
Statement type: