transitlos.stops¶
Load a GTFS feed and build a per-stop table of raw transit level-of-service inputs.
Wraps pyGTFSHandler.feed.Feed loading plus its FeedAnalysisMixin methods
(get_headway_at_stops, get_speed_at_stops, add_stop_coords) into a
single call that produces the raw inputs transitlos.scoring needs:
headway_minutes (from get_headway_at_stops, already returned in minutes)
and avg_speed_kmh (from get_speed_at_stops's speed column, which is
already km/h -- see its implementation: (distance_weight/1000) /
(time_weight/3600)).
Honesty note on reliability: get_headway_at_stops returns only a harmonic
mean headway per stop/route, not a distribution, so there is no headway
coefficient-of-variation (headway_cv) available from pyGTFSHandler today.
This module does not fabricate one -- the returned frame's headway_cv
column is all-null, and stop_scores.compute_stop_scores documents how it
handles that honestly (it does not silently assume perfect reliability
without saying so).
pick_analysis_date(feed, route_types=None, day_type='weekday')
¶
Automatically pick the analysis day for feed (no hard-coded dates needed): a working weekday, a Saturday or a Sunday.
feed.get_representative_dateover the part of the feed's calendar within_REPRESENTATIVE_DATE_HORIZON_DAYSof today (or the whole calendar if that doesn't overlap -- stale/historical feeds).- Sanity check against total scheduled trips per day of that type (for weekdays: Mon-Fri, public holidays
excluded) in that window: if the pick has under 80% of the busiest such day's trips (real case: Taipei's
tdx_bus, a sparse baseline plus a short dense span the station-median score can't tell apart), use the middle date among those days with >= 95% of the maximum trips.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the feed has no dates to pick from. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
A loaded pyGTFSHandler Feed. |
required |
route_types
|
Optional route type filter. |
None
|
|
day_type
|
str
|
"weekday" (default), "saturday" or "sunday". |
'weekday'
|
Returns:
| Type | Description |
|---|---|
date
|
The analysis date. |
download_and_prepare_stops(feed_source, aoi=None, date_range=None, start_time=time.min, end_time=time.max, route_types=None, by='route_id', at='parent_station', how='add', scoring='direction_best', speed_method='straight', window_min=15.0, window_direction='both', **kwargs)
¶
Load a GTFS feed and build a per-stop headway/speed table.
scoring="direction_best" (default) scores every parent station from the MERGED departures of all routes split in the two directions
of travel, searching mode tier x minimum trip speed for the best stop score (scoring.direction_subset); by/how then do not apply
and the table already carries stop_score. scoring="route" is the previous route-level headway (by/how) with the speed max.
feed_source is passed straight through to pyGTFSHandler.feed.Feed's
gtfs_dirs constructor argument -- this function does not implement any
downloading/geocoding-to-feed-source resolution of its own, it only
orchestrates a Feed that has already been (or can already be) loaded
from a path/URL/list understood by pyGTFSHandler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_source
|
Union[str, Path, Sequence[Union[str, Path]]]
|
One or more GTFS directory/zip paths, forwarded to
|
required |
aoi
|
Optional[GeoDataFrame]
|
Optional Area of Interest GeoDataFrame/GeoSeries used to geospatially filter stops while loading the feed. |
None
|
date_range
|
Optional[tuple[Union[date, datetime], Union[date, datetime]]]
|
Optional Performance: passing |
None
|
start_time
|
Union[datetime, time]
|
Start of the daily analysis window, forwarded to both
|
min
|
end_time
|
Union[datetime, time]
|
End of the daily analysis window, forwarded the same way. Defaults to end of day. |
max
|
route_types
|
Optional[Union[list, int, str]]
|
Optional GTFS route type filter, forwarded to both
|
None
|
by
|
str
|
Grouping key forwarded to |
'route_id'
|
at
|
str
|
Spatial stop-identity column forwarded to both analysis calls
( |
'parent_station'
|
how
|
str
|
Aggregation strategy forwarded to both analysis calls. Note
Changed from |
'add'
|
speed_method
|
str
|
How the speed at a stop is measured: |
'straight'
|
window_min
|
float
|
Minutes on each side of the stop the speed is measured over (default 15). |
15.0
|
window_direction
|
str
|
|
'both'
|
**kwargs
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
geopandas.GeoDataFrame with columns: |
GeoDataFrame
|
used), |
GeoDataFrame
|
|
GeoDataFrame
|
docstring), |
GeoDataFrame
|
by the underlying calls. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the feed has no usable date to analyze (no
|
download_and_prepare_stops_per_feed(gtfs_dirs, **kwargs)
¶
Score each GTFS directory in gtfs_dirs independently, then concatenate.
download_and_prepare_stops normally stacks every directory into one
Feed and picks a SINGLE representative date shared across all of them
(see its docstring's "Bug found and fixed 2026-08-24" note) -- correct
and cheaper for the common case where every feed's calendar.txt
covers roughly the same real-world window. That assumption breaks when
feeds are genuinely from different times: real case found 2026-08-24,
Taipei -- its tdx_bus feed's calendar is valid only 2026-05-01 to
2026-06-30 (a narrow real-time TDX export snapshot), while its
trtc_metro feed's calendar spans 2025-08-18 to 2026-12-31. No single
date can be representative of both feeds' real service at once: a
near-term-bounded search barely overlaps tdx_bus's narrow window
(starves it down to a handful of stops), while an unbounded search can
only get both feeds right by coincidence.
This function sidesteps the problem entirely by never stacking feeds in
the first place: each directory in gtfs_dirs gets its own Feed, its
own representative-date pick (independently, via the normal
download_and_prepare_stops call), and its own stop scoring -- then
the resulting per-feed GeoDataFrames are concatenated. Each feed's
parent_station grouping (stop_group_distance, if passed via
**kwargs) is computed WITHIN that feed only, not across feeds -- a
bus stop and a metro entrance from two different feeds within 150m of
each other will NOT be merged into one parent_station by this path
(unlike the single-Feed path, where they would be). This is a
deliberate, documented simplification, not an oversight: the point of
this function is to fix the "no representative date works for every
feed" problem, not to also solve cross-feed station deduplication --
use download_and_prepare_stops (single stacked Feed) whenever every
feed's calendar genuinely overlaps well enough for one shared date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gtfs_dirs
|
Sequence[Union[str, Path]]
|
One GTFS directory/zip path per feed -- each is scored
completely independently (own |
required |
**kwargs
|
Forwarded unchanged to |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Concatenated |
GeoDataFrame
|
|
GeoDataFrame
|
feed stacked on top of the others ( |
GeoDataFrame
|
cross-feed index meaning to preserve). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If every feed in |
build_parent_station_keys(stops_meta_df, distance_m=150.0)
¶
Assign every GTFS stop a station_key grouping it with its physical station.
Prefers the real GTFS parent_station field where present (a station's
platforms/entrances are usually already linked there by the agency).
Stops with no parent_station (null/missing, or the column absent
entirely) are instead grouped by 150m spatial-proximity clustering
(_proximity_cluster_ids) -- this is the fallback the module docstring
and the stop-clustering requirement call for: real-world GTFS feeds
frequently split one physical station into several unlinked stop_ids
(opposite-direction platforms, separate entrances) with no
parent_station set at all.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stops_meta_df
|
DataFrame
|
A polars DataFrame with at least |
required |
distance_m
|
float
|
Proximity threshold in meters for the spatial fallback. Defaults to 150m per the clustering requirement. |
150.0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
DataFrame
|
where present, else |
DataFrame
|
proximity. Each individual |
DataFrame
|
this only adds the grouping key, it does not collapse rows. |