Transit ¶
Tables ¶
Data models for various GTFS tables using pandera library.
The module includes the following classes:
- AgencyTable: Optional. Represents the Agency table in the GTFS dataset.
- WranglerStopsTable: Represents the Stops table in the GTFS dataset.
- RoutesTable: Represents the Routes table in the GTFS dataset.
- WranglerShapesTable: Represents the Shapes table in the GTFS dataset.
- WranglerStopTimesTable: Represents the Stop Times table in the GTFS dataset.
- WranglerTripsTable: Represents the Trips table in the GTFS dataset.
Each table model leverages the Pydantic data models defined in the records module to define the data model for the corresponding table. The classes also include additional configurations for, such as uniqueness constraints.
Validating a table to the WranglerStopsTable
network_wrangler.models.gtfs.tables.AgenciesTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.AgenciesTable[AgenciesTable]
click network_wrangler.models.gtfs.tables.AgenciesTable href "" "network_wrangler.models.gtfs.tables.AgenciesTable"
Represents the Agency table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#agencytxt
Attributes:
| Name | Type | Description |
|---|---|---|
agency_id |
str
|
The agency_id. Primary key. Required to be unique. |
agency_name |
str
|
The agency name. |
agency_url |
str
|
The agency URL. |
agency_timezone |
str
|
The agency timezone. |
agency_lang |
str
|
The agency language. |
agency_phone |
str
|
The agency phone number. |
agency_fare_url |
str
|
The agency fare URL. |
agency_email |
str
|
The agency email. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.FrequenciesTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.FrequenciesTable[FrequenciesTable]
click network_wrangler.models.gtfs.tables.FrequenciesTable href "" "network_wrangler.models.gtfs.tables.FrequenciesTable"
Represents the Frequency table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#frequenciestxt
The primary key of this table is a composite key of trip_id and start_time.
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Foreign key to |
start_time |
TimeString
|
The start time in HH:MM:SS format. |
end_time |
TimeString
|
The end time in HH:MM:SS format. |
headway_secs |
int
|
The headway in seconds. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.RoutesTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.RoutesTable[RoutesTable]
click network_wrangler.models.gtfs.tables.RoutesTable href "" "network_wrangler.models.gtfs.tables.RoutesTable"
Represents the Routes table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#routestxt
Attributes:
| Name | Type | Description |
|---|---|---|
route_id |
str
|
The route_id. Primary key. Required to be unique. |
route_short_name |
str | None
|
The route short name. |
route_long_name |
str | None
|
The route long name. |
route_type |
RouteType
|
The route type. Required. Values can be: - 0: Tram, Streetcar, Light rail - 1: Subway, Metro - 2: Rail - 3: Bus |
agency_id |
str | None
|
The agency_id. Foreign key to agency_id in the agencies table. |
route_desc |
str | None
|
The route description. |
route_url |
str | None
|
The route URL. |
route_color |
str | None
|
The route color. |
route_text_color |
str | None
|
The route text color. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.ShapesTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.ShapesTable[ShapesTable]
click network_wrangler.models.gtfs.tables.ShapesTable href "" "network_wrangler.models.gtfs.tables.ShapesTable"
Represents the Shapes table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#shapestxt
Attributes:
| Name | Type | Description |
|---|---|---|
shape_id |
str
|
The shape_id. Primary key. Required to be unique. |
shape_pt_lat |
float
|
The shape point latitude. |
shape_pt_lon |
float
|
The shape point longitude. |
shape_pt_sequence |
int
|
The shape point sequence. |
shape_dist_traveled |
float | None
|
The shape distance traveled. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.StopTimesTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.StopTimesTable[StopTimesTable]
click network_wrangler.models.gtfs.tables.StopTimesTable href "" "network_wrangler.models.gtfs.tables.StopTimesTable"
Represents the Stop Times table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#stop_timestxt
The primary key of this table is a composite key of trip_id and stop_sequence.
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Foreign key to |
stop_id |
str
|
Foreign key to |
stop_sequence |
int
|
The stop sequence. |
pickup_type |
PickupDropoffType
|
The pickup type. Values can be: - 0: Regularly scheduled pickup - 1: No pickup available - 2: Must phone agency to arrange pickup - 3: Must coordinate with driver to arrange pickup |
drop_off_type |
PickupDropoffType
|
The drop off type. Values can be: - 0: Regularly scheduled drop off - 1: No drop off available - 2: Must phone agency to arrange drop off - 3: Must coordinate with driver to arrange drop off |
arrival_time |
TimeString
|
The arrival time in HH:MM:SS format. |
departure_time |
TimeString
|
The departure time in HH:MM:SS format. |
shape_dist_traveled |
float | None
|
The shape distance traveled. |
timepoint |
TimepointType | None
|
The timepoint type. Values can be: - 0: The stop is not a timepoint - 1: The stop is a timepoint |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.StopTimesTable.parse_times ¶
Parse time strings to timestamps.
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.StopsTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.StopsTable[StopsTable]
click network_wrangler.models.gtfs.tables.StopsTable href "" "network_wrangler.models.gtfs.tables.StopsTable"
Represents the Stops table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#stopstxt
Attributes:
| Name | Type | Description |
|---|---|---|
stop_id |
str
|
The stop_id. Primary key. Required to be unique. |
stop_lat |
float
|
The stop latitude. |
stop_lon |
float
|
The stop longitude. |
wheelchair_boarding |
int | None
|
The wheelchair boarding. |
stop_code |
str | None
|
The stop code. |
stop_name |
str | None
|
The stop name. |
tts_stop_name |
str | None
|
The text-to-speech stop name. |
stop_desc |
str | None
|
The stop description. |
zone_id |
str | None
|
The zone id. |
stop_url |
str | None
|
The stop URL. |
location_type |
LocationType | None
|
The location type. Values can be: - 0: stop platform - 1: station - 2: entrance/exit - 3: generic node - 4: boarding area Default of blank assumes a stop platform. |
parent_station |
str | None
|
The |
stop_timezone |
str | None
|
The stop timezone. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.TripsTable ¶
Bases: DataFrameModel
flowchart TD
network_wrangler.models.gtfs.tables.TripsTable[TripsTable]
click network_wrangler.models.gtfs.tables.TripsTable href "" "network_wrangler.models.gtfs.tables.TripsTable"
Represents the Trips table in the GTFS dataset.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#tripstxt
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Primary key. Required to be unique. |
shape_id |
str
|
Foreign key to |
direction_id |
DirectionID
|
The direction id. Required. Values can be: - 0: Outbound - 1: Inbound |
service_id |
str
|
The service id. |
route_id |
str
|
The route id. Foreign key to |
trip_short_name |
str | None
|
The trip short name. |
trip_headsign |
str | None
|
The trip headsign. |
block_id |
str | None
|
The block id. |
wheelchair_accessible |
int | None
|
The wheelchair accessible. Values can be: - 0: No information - 1: Allowed - 2: Not allowed |
bikes_allowed |
int | None
|
The bikes allowed. Values can be: - 0: No information - 1: Allowed - 2: Not allowed |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerFrequenciesTable ¶
Bases: FrequenciesTable
flowchart TD
network_wrangler.models.gtfs.tables.WranglerFrequenciesTable[WranglerFrequenciesTable]
network_wrangler.models.gtfs.tables.FrequenciesTable[FrequenciesTable]
network_wrangler.models.gtfs.tables.FrequenciesTable --> network_wrangler.models.gtfs.tables.WranglerFrequenciesTable
click network_wrangler.models.gtfs.tables.WranglerFrequenciesTable href "" "network_wrangler.models.gtfs.tables.WranglerFrequenciesTable"
click network_wrangler.models.gtfs.tables.FrequenciesTable href "" "network_wrangler.models.gtfs.tables.FrequenciesTable"
Wrangler flavor of GTFS FrequenciesTable.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#frequenciestxt
The primary key of this table is a composite key of trip_id and start_time.
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Foreign key to |
start_time |
datetime
|
The start time in datetime format. |
end_time |
datetime
|
The end time in datetime format. |
headway_secs |
int
|
The headway in seconds. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerFrequenciesTable.et_to_timestamp ¶
Check that start time is timestamp.
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerFrequenciesTable.st_to_timestamp ¶
Check that start time is timestamp.
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerShapesTable ¶
Bases: ShapesTable
flowchart TD
network_wrangler.models.gtfs.tables.WranglerShapesTable[WranglerShapesTable]
network_wrangler.models.gtfs.tables.ShapesTable[ShapesTable]
network_wrangler.models.gtfs.tables.ShapesTable --> network_wrangler.models.gtfs.tables.WranglerShapesTable
click network_wrangler.models.gtfs.tables.WranglerShapesTable href "" "network_wrangler.models.gtfs.tables.WranglerShapesTable"
click network_wrangler.models.gtfs.tables.ShapesTable href "" "network_wrangler.models.gtfs.tables.ShapesTable"
Wrangler flavor of GTFS ShapesTable.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#shapestxt
Attributes:
| Name | Type | Description |
|---|---|---|
shape_id |
str
|
The shape_id. Primary key. Required to be unique. |
shape_pt_lat |
float
|
The shape point latitude. |
shape_pt_lon |
float
|
The shape point longitude. |
shape_pt_sequence |
int
|
The shape point sequence. |
shape_dist_traveled |
float | None
|
The shape distance traveled. |
shape_model_node_id |
int
|
The model_node_id of the shape point. Foreign key to the model_node_id in the nodes table. |
projects |
str
|
A comma-separated string value for projects that have been applied to this shape. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerStopTimesTable ¶
Bases: StopTimesTable
flowchart TD
network_wrangler.models.gtfs.tables.WranglerStopTimesTable[WranglerStopTimesTable]
network_wrangler.models.gtfs.tables.StopTimesTable[StopTimesTable]
network_wrangler.models.gtfs.tables.StopTimesTable --> network_wrangler.models.gtfs.tables.WranglerStopTimesTable
click network_wrangler.models.gtfs.tables.WranglerStopTimesTable href "" "network_wrangler.models.gtfs.tables.WranglerStopTimesTable"
click network_wrangler.models.gtfs.tables.StopTimesTable href "" "network_wrangler.models.gtfs.tables.StopTimesTable"
Wrangler flavor of GTFS StopTimesTable.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#stop_timestxt
The primary key of this table is a composite key of trip_id and stop_sequence.
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Foreign key to |
stop_id |
int
|
Foreign key to |
stop_sequence |
int
|
The stop sequence. |
pickup_type |
PickupDropoffType
|
The pickup type. Values can be: - 0: Regularly scheduled pickup - 1: No pickup available - 2: Must phone agency to arrange pickup - 3: Must coordinate with driver to arrange pickup |
drop_off_type |
PickupDropoffType
|
The drop off type. Values can be: - 0: Regularly scheduled drop off - 1: No drop off available - 2: Must phone agency to arrange drop off - 3: Must coordinate with driver to arrange drop off |
shape_dist_traveled |
float | None
|
The shape distance traveled. |
timepoint |
TimepointType | None
|
The timepoint type. Values can be: - 0: The stop is not a timepoint - 1: The stop is a timepoint |
projects |
str
|
A comma-separated string value for projects that have been applied to this stop. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerStopTimesTable.parse_times ¶
Parse time strings to timestamps.
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerStopsTable ¶
Bases: StopsTable
flowchart TD
network_wrangler.models.gtfs.tables.WranglerStopsTable[WranglerStopsTable]
network_wrangler.models.gtfs.tables.StopsTable[StopsTable]
network_wrangler.models.gtfs.tables.StopsTable --> network_wrangler.models.gtfs.tables.WranglerStopsTable
click network_wrangler.models.gtfs.tables.WranglerStopsTable href "" "network_wrangler.models.gtfs.tables.WranglerStopsTable"
click network_wrangler.models.gtfs.tables.StopsTable href "" "network_wrangler.models.gtfs.tables.StopsTable"
Wrangler flavor of GTFS StopsTable.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#stopstxt
Attributes:
| Name | Type | Description |
|---|---|---|
stop_id |
int
|
The stop_id. Primary key. Required to be unique. Wrangler assumes that this is a reference to a roadway node and as such must be an integer |
stop_lat |
float
|
The stop latitude. |
stop_lon |
float
|
The stop longitude. |
wheelchair_boarding |
int | None
|
The wheelchair boarding. |
stop_code |
str | None
|
The stop code. |
stop_name |
str | None
|
The stop name. |
tts_stop_name |
str | None
|
The text-to-speech stop name. |
stop_desc |
str | None
|
The stop description. |
zone_id |
str | None
|
The zone id. |
stop_url |
str | None
|
The stop URL. |
location_type |
LocationType | None
|
The location type. Values can be: - 0: stop platform - 1: station - 2: entrance/exit - 3: generic node - 4: boarding area Default of blank assumes a stop platform. |
parent_station |
int | None
|
The |
stop_timezone |
str | None
|
The stop timezone. |
stop_id_GTFS |
str | None
|
The stop_id from the GTFS data. |
projects |
str
|
A comma-separated string value for projects that have been applied to this stop. |
Source code in network_wrangler/models/gtfs/tables.py
network_wrangler.models.gtfs.tables.WranglerTripsTable ¶
Bases: TripsTable
flowchart TD
network_wrangler.models.gtfs.tables.WranglerTripsTable[WranglerTripsTable]
network_wrangler.models.gtfs.tables.TripsTable[TripsTable]
network_wrangler.models.gtfs.tables.TripsTable --> network_wrangler.models.gtfs.tables.WranglerTripsTable
click network_wrangler.models.gtfs.tables.WranglerTripsTable href "" "network_wrangler.models.gtfs.tables.WranglerTripsTable"
click network_wrangler.models.gtfs.tables.TripsTable href "" "network_wrangler.models.gtfs.tables.TripsTable"
Represents the Trips table in the Wrangler feed, adding projects list.
For field definitions, see the GTFS reference: https://gtfs.org/documentation/schedule/reference/#tripstxt
Attributes:
| Name | Type | Description |
|---|---|---|
trip_id |
str
|
Primary key. Required to be unique. |
shape_id |
str
|
Foreign key to |
direction_id |
DirectionID
|
The direction id. Required. Values can be: - 0: Outbound - 1: Inbound |
service_id |
str
|
The service id. |
route_id |
str
|
The route id. Foreign key to |
trip_short_name |
str | None
|
The trip short name. |
trip_headsign |
str | None
|
The trip headsign. |
block_id |
str | None
|
The block id. |
wheelchair_accessible |
int | None
|
The wheelchair accessible. Values can be: - 0: No information - 1: Allowed - 2: Not allowed |
bikes_allowed |
int | None
|
The bikes allowed. Values can be: - 0: No information - 1: Allowed - 2: Not allowed |
projects |
str
|
A comma-separated string value for projects that have been applied to this trip. |
Source code in network_wrangler/models/gtfs/tables.py
Data Model for Pure GTFS Feed (not wrangler-flavored).
network_wrangler.models.gtfs.gtfs.FERRY_ROUTE_TYPES
module-attribute
¶
GTFS route types which trigger ‘ferry_only’ link creation in add_stations_and_links_to_roadway_network()
network_wrangler.models.gtfs.gtfs.MIXED_TRAFFIC_ROUTE_TYPES
module-attribute
¶
GTFS route types that operate in mixed traffic so stops are nodes that are drive-accessible.
See GTFS routes.txt
- TRAM = Tram, Streetcar, Light rail, operates in mixed traffic AND at stations
- CABLE_TRAM = street-level rail with underground cable
- TROLLEYBUS = electric buses with overhead wires
network_wrangler.models.gtfs.gtfs.RAIL_ROUTE_TYPES
module-attribute
¶
GTFS route types which trigger ‘rail_only’ link creation in add_stations_and_links_to_roadway_network()
network_wrangler.models.gtfs.gtfs.STATION_ROUTE_TYPES
module-attribute
¶
GTFS route types that operate at stations.
network_wrangler.models.gtfs.gtfs.GtfsModel ¶
Bases: DBModelMixin
flowchart TD
network_wrangler.models.gtfs.gtfs.GtfsModel[GtfsModel]
network_wrangler.models._base.db.DBModelMixin[DBModelMixin]
network_wrangler.models._base.db.DBModelMixin --> network_wrangler.models.gtfs.gtfs.GtfsModel
click network_wrangler.models.gtfs.gtfs.GtfsModel href "" "network_wrangler.models.gtfs.gtfs.GtfsModel"
click network_wrangler.models._base.db.DBModelMixin href "" "network_wrangler.models._base.db.DBModelMixin"
Wrapper class around GTFS feed.
This is the pure GTFS model version of Feed
Most functionality derives from mixin class
DBModelMixin which provides:
- validation of tables to schemas when setting a table attribute (e.g. self.trips = trips_df)
- validation of fks when setting a table attribute (e.g. self.trips = trips_df)
- hashing and deep copy functionality
- overload of eq to apply only to tables in table_names.
- convenience methods for accessing tables
Attributes:
| Name | Type | Description |
|---|---|---|
table_names |
list[str]
|
list of table names in GTFS feed. |
tables |
list[DataFrame]
|
list tables as dataframes. |
agency |
DataFrame[AgenciesTable]
|
agency dataframe |
stop_times |
DataFrame[StopTimesTable]
|
stop_times dataframe |
stops |
DataFrame[WranglerStopsTable]
|
stops dataframe |
shapes |
DataFrame[ShapesTable]
|
shapes dataframe |
trips |
DataFrame[TripsTable]
|
trips dataframe |
frequencies |
Optional[DataFrame[FrequenciesTable]]
|
frequencies dataframe |
routes |
DataFrame[RoutesTable]
|
route dataframe |
net |
Optional[TransitNetwork]
|
TransitNetwork object |
Source code in network_wrangler/models/gtfs/gtfs.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
network_wrangler.models.gtfs.gtfs.GtfsModel.summary
property
¶
A high level summary of the GTFS model object and public attributes.
network_wrangler.models.gtfs.gtfs.GtfsModel.__init__ ¶
Initialize GTFS model.
Source code in network_wrangler/models/gtfs/gtfs.py
network_wrangler.models.gtfs.gtfs.GtfsModel.__repr__ ¶
Return a string representation of the GtfsModel with table summaries.
network_wrangler.models.gtfs.gtfs.GtfsValidationError ¶
Bases: Exception
flowchart TD
network_wrangler.models.gtfs.gtfs.GtfsValidationError[GtfsValidationError]
click network_wrangler.models.gtfs.gtfs.GtfsValidationError href "" "network_wrangler.models.gtfs.gtfs.GtfsValidationError"
Exception raised for errors in the GTFS feed.
Feed ¶
Main functionality for GTFS tables including Feed object.
network_wrangler.transit.feed.feed.Feed ¶
Bases: DBModelMixin
flowchart TD
network_wrangler.transit.feed.feed.Feed[Feed]
network_wrangler.models._base.db.DBModelMixin[DBModelMixin]
network_wrangler.models._base.db.DBModelMixin --> network_wrangler.transit.feed.feed.Feed
click network_wrangler.transit.feed.feed.Feed href "" "network_wrangler.transit.feed.feed.Feed"
click network_wrangler.models._base.db.DBModelMixin href "" "network_wrangler.models._base.db.DBModelMixin"
Wrapper class around Wrangler flavored GTFS feed.
Most functionality derives from mixin class
DBModelMixin which provides:
- validation of tables to schemas when setting a table attribute (e.g. self.trips = trips_df)
- validation of fks when setting a table attribute (e.g. self.trips = trips_df)
- hashing and deep copy functionality
- overload of eq to apply only to tables in table_names.
- convenience methods for accessing tables
What is Wrangler-flavored GTFS?
A Wrangler-flavored GTFS feed differs from a GTFS feed in the following ways:
frequencies.txtis requiredshapes.txtrequires additional field,shape_model_node_id, corresponding tomodel_node_idin theRoadwayNetworkstops.txt-stop_idis required to be an int
Attributes:
| Name | Type | Description |
|---|---|---|
table_names |
list[str]
|
list of table names in GTFS feed. |
tables |
list[DataFrame]
|
list tables as dataframes. |
stop_times |
DataFrame[WranglerStopTimesTable]
|
stop_times dataframe with roadway node_ids |
stops |
DataFrame[WranglerStopsTable]
|
stops dataframe |
shapes |
DataFrame[WranglerShapesTable]
|
shapes dataframe |
trips |
DataFrame[WranglerTripsTable]
|
trips dataframe |
frequencies |
DataFrame[WranglerFrequenciesTable]
|
frequencies dataframe |
routes |
DataFrame[RoutesTable]
|
route dataframe |
agencies |
Optional[DataFrame[AgenciesTable]]
|
agencies dataframe |
net |
Optional[TransitNetwork]
|
TransitNetwork object |
Source code in network_wrangler/transit/feed/feed.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
network_wrangler.transit.feed.feed.Feed.summary
property
¶
A high level summary of the GTFS model object and public attributes.
network_wrangler.transit.feed.feed.Feed.__init__ ¶
Create a Feed object from a dictionary of DataFrames representing a GTFS feed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
A dictionary containing DataFrames representing the tables of a GTFS feed. |
{}
|
Source code in network_wrangler/transit/feed/feed.py
network_wrangler.transit.feed.feed.Feed.__repr__ ¶
Return a string representation of the Feed with table summaries.
Source code in network_wrangler/transit/feed/feed.py
network_wrangler.transit.feed.feed.Feed.set_by_id ¶
Set one or more property values based on an ID property for a given table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table to modify. |
required |
set_df
|
DataFrame
|
DataFrame with columns |
required |
id_property
|
str
|
Property to use as ID to set by. Defaults to “index”. |
'index'
|
properties
|
list[str] | None
|
List of properties to set which are in set_df. If not specified, will set all properties. |
None
|
Source code in network_wrangler/transit/feed/feed.py
network_wrangler.transit.feed.feed.merge_shapes_to_stop_times ¶
Add shape_id and shape_pt_sequence to stop_times dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
stop_times dataframe to add shape_id and shape_pt_sequence to. |
required |
shapes
|
DataFrame[WranglerShapesTable]
|
shapes dataframe to add to stop_times. |
required |
trips
|
DataFrame[WranglerTripsTable]
|
trips dataframe to link stop_times to shapes |
required |
Returns:
| Type | Description |
|---|---|
DataFrame[WranglerStopTimesTable]
|
stop_times dataframe with shape_id and shape_pt_sequence added. |
Source code in network_wrangler/transit/feed/feed.py
network_wrangler.transit.feed.feed.stop_count_by_trip ¶
Returns dataframe with trip_id and stop_count from stop_times.
Source code in network_wrangler/transit/feed/feed.py
Filters and queries of a gtfs frequencies table.
network_wrangler.transit.feed.frequencies.frequencies_for_trips ¶
Filter frequenceis dataframe to records associated with trips table.
Source code in network_wrangler/transit/feed/frequencies.py
Filters and queries of a gtfs routes table and route_ids.
network_wrangler.transit.feed.routes.route_ids_for_trip_ids ¶
Returns route ids for given list of trip_ids.
network_wrangler.transit.feed.routes.routes_for_trip_ids ¶
Returns route records for given list of trip_ids.
Source code in network_wrangler/transit/feed/routes.py
network_wrangler.transit.feed.routes.routes_for_trips ¶
Filter routes dataframe to records associated with trip records.
Source code in network_wrangler/transit/feed/routes.py
Filters, queries of a gtfs shapes table and node patterns.
network_wrangler.transit.feed.shapes.find_nearest_stops ¶
Returns node_ids (before and after) of nearest node_ids that are stops for a given trip_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
WranglerShapesTable
|
WranglerShapesTable |
required |
trips
|
WranglerTripsTable
|
WranglerTripsTable |
required |
stop_times
|
WranglerStopTimesTable
|
WranglerStopTimesTable |
required |
trip_id
|
str
|
trip id to find nearest stops for |
required |
node_id
|
int
|
node_id to find nearest stops for |
required |
pickup_dropoff
|
PickupDropoffAvailability
|
str indicating logic for selecting stops based on piackup and dropoff availability at stop. Defaults to “either”. “either”: either pickup_type or dropoff_type > 0 “both”: both pickup_type or dropoff_type > 0 “pickup_only”: only pickup > 0 “dropoff_only”: only dropoff > 0 |
'either'
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int]
|
node_ids for stop before and stop after |
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.node_pattern_for_shape_id ¶
Returns node pattern of a shape.
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shape_id_for_trip_id ¶
network_wrangler.transit.feed.shapes.shape_ids_for_trip_ids ¶
Returns a list of shape_ids for a given list of trip_ids.
network_wrangler.transit.feed.shapes.shapes_for_road_links ¶
Filter shapes dataframe to records associated with links dataframe.
EX:
shapes = pd.DataFrame({ “shape_id”: [“1”, “1”, “1”, “1”, “2”, “2”, “2”, “2”, “2”], “shape_pt_sequence”: [1, 2, 3, 4, 1, 2, 3, 4, 5], “shape_model_node_id”: [1, 2, 3, 4, 2, 3, 1, 5, 4] })
links_df = pd.DataFrame({ “A”: [1, 2, 3], “B”: [2, 3, 4] })
shapes
shape_id shape_pt_sequence shape_model_node_id should retain 1 1 1 TRUE 1 2 2 TRUE 1 3 3 TRUE 1 4 4 TRUE 1 5 5 FALSE 2 1 1 TRUE 2 2 2 TRUE 2 3 3 TRUE 2 4 1 FALSE 2 5 5 FALSE 2 6 4 FALSE 2 7 1 FALSE - not largest segment 2 8 2 FALSE - not largest segment
links_df
A B 1 2 2 3 3 4
Source code in network_wrangler/transit/feed/shapes.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
network_wrangler.transit.feed.shapes.shapes_for_shape_id ¶
Returns shape records for a given shape_id.
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shapes_for_trip_id ¶
Returns shape records for a single given trip_id.
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shapes_for_trip_ids ¶
Returns shape records for list of trip_ids.
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shapes_for_trips ¶
Filter shapes dataframe to records associated with trips table.
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shapes_with_stop_id_for_trip_id ¶
Returns shapes.txt for a given trip_id with the stop_id added based on pickup_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
WranglerShapesTable |
required |
trips
|
DataFrame[WranglerTripsTable]
|
WranglerTripsTable |
required |
stop_times
|
DataFrame[WranglerStopTimesTable]
|
WranglerStopTimesTable |
required |
trip_id
|
str
|
trip id to select |
required |
pickup_dropoff
|
PickupDropoffAvailability
|
str indicating logic for selecting stops based on piackup and dropoff availability at stop. Defaults to “either”. “either”: either pickup_type or dropoff_type > 0 “both”: both pickup_type or dropoff_type > 0 “pickup_only”: only pickup > 0 “dropoff_only”: only dropoff > 0 |
'either'
|
Source code in network_wrangler/transit/feed/shapes.py
network_wrangler.transit.feed.shapes.shapes_with_stops_for_shape_id ¶
Returns a DataFrame containing shapes with associated stops for a given shape_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
DataFrame containing shape data. |
required |
trips
|
DataFrame[WranglerTripsTable]
|
DataFrame containing trip data. |
required |
stop_times
|
DataFrame[WranglerStopTimesTable]
|
DataFrame containing stop times data. |
required |
shape_id
|
str
|
The shape_id for which to retrieve shapes with stops. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame[WranglerShapesTable]
|
DataFrame[WranglerShapesTable]: DataFrame containing shapes with associated stops. |
Source code in network_wrangler/transit/feed/shapes.py
Filters and queries of a gtfs stop_times table.
network_wrangler.transit.feed.stop_times.stop_times_for_longest_segments ¶
Find the longest segment of each trip_id that is in the stop_times.
Segment ends defined based on interruptions in stop_sequence.
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_min_stops ¶
Filter stop_times dataframe to only the records which have >= min_stops for the trip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
stoptimestable to filter |
required |
min_stops
|
int
|
minimum stops to require to keep trip in stoptimes |
required |
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_pickup_dropoff_trip_id ¶
Filters stop_times for a given trip_id based on pickup type.
GTFS values for pickup_type and drop_off_type” 0 or empty - Regularly scheduled pickup/dropoff. 1 - No pickup/dropoff available. 2 - Must phone agency to arrange pickup/dropoff. 3 - Must coordinate with driver to arrange pickup/dropoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
A WranglerStopTimesTable to query. |
required |
trip_id
|
str
|
trip_id to get stop pattern for |
required |
pickup_dropoff
|
PickupDropoffAvailability
|
str indicating logic for selecting stops based on pickup and dropoff availability at stop. Defaults to “either”. “any”: all stoptime records “either”: either pickup_type or dropoff_type != 1 “both”: both pickup_type and dropoff_type != 1 “pickup_only”: dropoff = 1; pickup != 1 “dropoff_only”: pickup = 1; dropoff != 1 |
'either'
|
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_route_ids ¶
Returns a stop_time records for a list of route_ids.
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_shapes ¶
Filter stop_times dataframe to records associated with shapes dataframe.
Where multiple segments of stop_times are found to match shapes, retain only the longest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
stop_times dataframe to filter |
required |
shapes
|
DataFrame[WranglerShapesTable]
|
shapes dataframe to stop_times to. |
required |
trips
|
DataFrame[WranglerTripsTable]
|
trips to link stop_times to shapess |
required |
Returns:
| Type | Description |
|---|---|
DataFrame[WranglerStopTimesTable]
|
filtered stop_times dataframe |
- should be retained
stop_times
trip_id stop_sequence stop_id t1 1 1 t1 2 2 t1 3 3 t1 4 5 t2 1 1 *t2 2 3 t2 3 7
shapes
shape_id shape_pt_sequence shape_model_node_id s1 1 1 s1 2 2 s1 3 3 s1 4 4 s2 1 1 s2 2 2 s2 3 3
trips
trip_id shape_id t1 s1 t2 s2
Source code in network_wrangler/transit/feed/stop_times.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
network_wrangler.transit.feed.stop_times.stop_times_for_stops ¶
Filter stop_times dataframe to only have stop_times associated with stops records.
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_trip_id ¶
Returns a stop_time records for a given trip_id.
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_trip_ids ¶
Returns a stop_time records for a given list of trip_ids.
Source code in network_wrangler/transit/feed/stop_times.py
network_wrangler.transit.feed.stop_times.stop_times_for_trip_node_segment ¶
stop_times_for_trip_node_segment(
stop_times,
trip_id,
node_id_start,
node_id_end,
include_start=True,
include_end=True,
)
Returns stop_times for a given trip_id between two nodes or with those nodes included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
WranglerStopTimesTable |
required |
trip_id
|
str
|
trip id to select |
required |
node_id_start
|
int
|
int of the starting node |
required |
node_id_end
|
int
|
int of the ending node |
required |
include_start
|
bool
|
bool indicating if the start node should be included in the segment. Defaults to True. |
True
|
include_end
|
bool
|
bool indicating if the end node should be included in the segment. Defaults to True. |
True
|
Source code in network_wrangler/transit/feed/stop_times.py
Filters and queries of a gtfs stops table and stop_ids.
network_wrangler.transit.feed.stops.node_is_stop ¶
Returns boolean indicating if a (or list of) node(s)) is (are) stops for a given trip_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stops
|
DataFrame[WranglerStopsTable]
|
WranglerStopsTable |
required |
stop_times
|
DataFrame[WranglerStopTimesTable]
|
WranglerStopTimesTable |
required |
node_id
|
int | list[int]
|
node ID for roadway |
required |
trip_id
|
str
|
trip_id to get stop pattern for |
required |
pickup_dropoff
|
PickupDropoffAvailability
|
str indicating logic for selecting stops based on piackup and dropoff availability at stop. Defaults to “either”. “either”: either pickup_type or dropoff_type > 0 “both”: both pickup_type or dropoff_type > 0 “pickup_only”: only pickup > 0 “dropoff_only”: only dropoff > 0 |
'either'
|
Source code in network_wrangler/transit/feed/stops.py
network_wrangler.transit.feed.stops.stop_id_pattern_for_trip ¶
Returns a stop pattern for a given trip_id given by a list of stop_ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
WranglerStopTimesTable |
required |
trip_id
|
str
|
trip_id to get stop pattern for |
required |
pickup_dropoff
|
PickupDropoffAvailability
|
str indicating logic for selecting stops based on piackup and dropoff availability at stop. Defaults to “either”. “either”: either pickup_type or dropoff_type > 0 “both”: both pickup_type or dropoff_type > 0 “pickup_only”: only pickup > 0 “dropoff_only”: only dropoff > 0 |
'either'
|
Source code in network_wrangler/transit/feed/stops.py
network_wrangler.transit.feed.stops.stops_for_stop_times ¶
Filter stops dataframe to only have stops associated with stop_times records.
Source code in network_wrangler/transit/feed/stops.py
network_wrangler.transit.feed.stops.stops_for_trip_id ¶
Returns stops.txt which are used for a given trip_id.
Source code in network_wrangler/transit/feed/stops.py
Filters and queries of a gtfs trips table and trip_ids.
network_wrangler.transit.feed.trips.trip_ids_for_shape_id ¶
Returns a list of trip_ids for a given shape_id.
network_wrangler.transit.feed.trips.trips_for_shape_id ¶
Returns a trips records for a given shape_id.
network_wrangler.transit.feed.trips.trips_for_stop_times ¶
Filter trips dataframe to records associated with stop_time records.
Source code in network_wrangler/transit/feed/trips.py
Functions for translating transit tables into visualizable links relatable to roadway network.
network_wrangler.transit.feed.transit_links.shapes_to_shape_links ¶
Converts shapes DataFrame to shape links DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
The input shapes DataFrame. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The resulting shape links DataFrame. |
Source code in network_wrangler/transit/feed/transit_links.py
network_wrangler.transit.feed.transit_links.stop_times_to_stop_times_links ¶
Converts stop times to stop times links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
The stop times data. |
required |
from_field
|
str
|
The name of the field representing the ‘from’ stop. Defaults to “A”. |
'A'
|
to_field
|
str
|
The name of the field representing the ‘to’ stop. Defaults to “B”. |
'B'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The resulting stop times links. |
Source code in network_wrangler/transit/feed/transit_links.py
network_wrangler.transit.feed.transit_links.unique_shape_links ¶
Returns a DataFrame containing unique shape links based on the provided shapes DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
The input DataFrame containing shape information. |
required |
from_field
|
str
|
The name of the column representing the ‘from’ field. Defaults to “A”. |
'A'
|
to_field
|
str
|
The name of the column representing the ‘to’ field. Defaults to “B”. |
'B'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing unique shape links based on the provided shapes df. |
Source code in network_wrangler/transit/feed/transit_links.py
network_wrangler.transit.feed.transit_links.unique_stop_time_links ¶
Returns a DataFrame containing unique stop time links based on the given stop times DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
DataFrame[WranglerStopTimesTable]
|
The DataFrame containing stop times data. |
required |
from_field
|
str
|
The name of the column representing the ‘from’ field in the stop times DataFrame. Defaults to “A”. |
'A'
|
to_field
|
str
|
The name of the column representing the ‘to’ field in the stop times DataFrame. Defaults to “B”. |
'B'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing unique stop time links with columns ‘from_field’, ‘to_field’, and ‘trip_id’. |
Source code in network_wrangler/transit/feed/transit_links.py
Functions to create segments from shapes and shape_links.
network_wrangler.transit.feed.transit_segments.filter_shapes_to_segments ¶
Filter shapes dataframe to records associated with segments dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
shapes dataframe to filter |
required |
segments
|
DataFrame
|
segments dataframe to filter by with shape_id, segment_start_shape_pt_seq, segment_end_shape_pt_seq . Should have one record per shape_id. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame[WranglerShapesTable]
|
filtered shapes dataframe |
Source code in network_wrangler/transit/feed/transit_segments.py
network_wrangler.transit.feed.transit_segments.shape_links_to_longest_shape_segments ¶
Find the longest segment of each shape_id that is in the links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape_links
|
DataFrame with shape_id, shape_pt_sequence_A, shape_pt_sequence_B |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with shape_id, segment_id, segment_start_shape_pt_seq, segment_end_shape_pt_seq |
Source code in network_wrangler/transit/feed/transit_segments.py
network_wrangler.transit.feed.transit_segments.shape_links_to_segments ¶
Convert shape_links to segments by shape_id with segments of continuous shape_pt_sequence.
DataFrame with shape_id, segment_id, segment_start_shape_pt_seq,
| Type | Description |
|---|---|
DataFrame
|
segment_end_shape_pt_seq |
Source code in network_wrangler/transit/feed/transit_segments.py
Transit Projects ¶
Functions for adding a transit route to a TransitNetwork.
network_wrangler.transit.projects.add_route.apply_transit_route_addition ¶
Add transit route to TransitNetwork.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
Network to modify. |
required |
transit_route_addition
|
dict
|
route dictionary to add to the feed. |
required |
reference_road_net
|
RoadwayNetwork | None
|
(RoadwayNetwork, optional): Reference roadway network to use for adding shapes and stops. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TransitNetwork |
TransitNetwork
|
Modified network. |
Source code in network_wrangler/transit/projects/add_route.py
Module for applying calculated transit projects to a transit network object.
These projects are stored in project card pycode property as python code strings which are
executed to change the transit network object.
network_wrangler.transit.projects.calculate.apply_calculated_transit ¶
Changes transit network object by executing pycode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
transit network to manipulate |
required |
pycode
|
str
|
python code which changes values in the transit network object |
required |
Source code in network_wrangler/transit/projects/calculate.py
Functions for adding a transit route to a TransitNetwork.
network_wrangler.transit.projects.delete_service.apply_transit_service_deletion ¶
Delete transit service to TransitNetwork.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
Network to modify. |
required |
selection
|
TransitSelection
|
TransitSelection object, created from a selection dictionary. |
required |
clean_shapes
|
bool
|
If True, remove shapes not used by any trips. Defaults to False. |
False
|
clean_routes
|
bool
|
If True, remove routes not used by any trips. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
TransitNetwork |
TransitNetwork
|
Modified network. |
Source code in network_wrangler/transit/projects/delete_service.py
Functions for editing transit properties in a TransitNetwork.
network_wrangler.transit.projects.edit_property.apply_transit_property_change ¶
Apply changes to transit properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
Network to modify. |
required |
selection
|
TransitSelection
|
Selection of trips to modify. |
required |
property_changes
|
dict
|
Dictionary of properties to change. |
required |
project_name
|
str
|
Name of the project. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TransitNetwork |
TransitNetwork
|
Modified network. |
Source code in network_wrangler/transit/projects/edit_property.py
Functions for editing the transit route shapes and stop patterns.
network_wrangler.transit.projects.edit_routing.apply_transit_routing_change ¶
apply_transit_routing_change(
net,
selection,
routing_change,
reference_road_net=None,
project_name=None,
)
Apply a routing change to the transit network, including stop updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
TransitNetwork object to apply routing change to. |
required |
selection
|
Selection
|
TransitSelection object, created from a selection dictionary. |
required |
routing_change
|
dict
|
required | |
shape_id_scalar
|
int
|
Initial scalar value to add to duplicated shape_ids to create a new shape_id. Defaults to SHAPE_ID_SCALAR. |
required |
reference_road_net
|
RoadwayNetwork
|
Reference roadway network to use for updating shapes and stops. Defaults to None. |
None
|
project_name
|
str
|
Name of the project. Defaults to None. |
None
|
Source code in network_wrangler/transit/projects/edit_routing.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
Transit Helper Modules ¶
Utilities for getting GTFS into wrangler.
network_wrangler.utils.transit.add_additional_data_to_shapes ¶
Updates feed_tables[‘shapes’] with route/trip metadata and snaps shape points to stops.
Enriches shape points with information from trips, routes, and agencies tables, then matches shape points to nearby stops and updates their locations. Processes stops in sequence order, always searching forward to handle routes that double back.
Process Steps:
- Converts shapes to GeoDataFrame if needed (using shape_pt_lon/lat)
- Joins with trips, routes, and agencies to add metadata
- Projects to local CRS for distance calculations
- For each shape, calls _align_shape_with_stops() to:
- Match stops to existing shape points (via _match_stop_to_shape_points())
- Insert unmatched stops as new shape points (via _insert_stop_into_shape())
- Verify stop_sequence is monotonically increasing
- Writes debug GeoJSON output (via _write_debug_shapes())
Assumes create_feed_frequencies() has already run, so each shape corresponds to one consolidated trip_id.
Modifies feed_tables in place:
feed_tables[‘shapes’] - Adds/modifies columns: Route/Trip Metadata: - trip_id (str): Associated trip ID - direction_id (int): Direction of travel (0 or 1) - route_id (str): Route identifier - agency_id (str): Agency identifier - agency_name (str): Agency name - route_short_name (str): Route short name - route_type (int): GTFS route type
1 2 3 4 5 6 7 | |
feed_tables[‘stop_times’] - Converted to GeoDataFrame with: - geometry: Stop location added from stops table
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict[str, DataFrame]
|
dictionary with required tables: - ‘shapes’: Shape points to update - ‘trips’: Trip information - ‘routes’: Route information - ‘agencies’: Agency information - ‘stops’: Stop locations - ‘stop_times’: Stop sequences |
required |
local_crs
|
str
|
Coordinate reference system for projections |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
trace_shape_ids
|
list[str] | None
|
Optional shape IDs for debug logging |
None
|
Helper Functions
_align_shape_with_stops(): Process all stops for one shape _match_stop_to_shape_points(): Find nearest shape point for a stop _insert_stop_into_shape(): Insert unmatched stop as new shape point _write_debug_shapes(): Write debug GeoJSON output
Notes
- Searches forward from previous matched position to handle routes that double back
- Inserts stops immediately when unmatched (not batched) for accurate positioning
- Handles circular routes (first/last stop same) with constrained search ranges
- Writes debug_shapes.geojson with all shapes, stops, and shape points
Source code in network_wrangler/utils/transit.py
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 | |
network_wrangler.utils.transit.add_additional_data_to_stops ¶
Updates feed_tables[‘stops’] with additional metadata about routes and agencies.
Aggregates information from stop_times, trips, routes, and agencies tables to add comprehensive metadata about which routes and agencies serve each stop.
Process Steps:
- Joins stop_times with trips to get route and shape information
- Joins with routes and agencies to get route types and agency names
- Groups by stop_id to aggregate all serving routes/agencies
- Identifies mixed-traffic stops based on route types
- Handles parent stations by checking child stop characteristics
Modifies feed_tables[‘stops’] in place, adding columns:
Route/Agency Information: - agency_ids (list of str): All agencies serving this stop - agency_names (list of str): Names of agencies serving this stop - route_ids (list of str): All routes serving this stop - route_names (list of str): Short names of routes serving this stop - route_types (list of int): Types of routes serving this stop - shape_ids (list of str): All shapes associated with this stop
Stop Type Flags: - is_parent (bool): True if other stops reference this as parent_station
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict[str, DataFrame]
|
dictionary with required tables: - ‘stop_times’: Links stops to trips - ‘trips’: Links trips to routes and shapes - ‘routes’: Route information including type - ‘agencies’: Agency names - ‘stops’: Table to be updated |
required |
Notes
- Parent stations may not appear in trips but are retained if referenced
- Empty lists are used for stops with no associated routes
- Handles missing parent_station column gracefully
Source code in network_wrangler/utils/transit.py
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 | |
network_wrangler.utils.transit.add_stations_and_links_to_roadway_network ¶
add_stations_and_links_to_roadway_network(
feed_tables,
roadway_net,
local_crs,
crs_units,
trace_shape_ids=None,
default_node_attribute_dict=None,
default_link_attribute_dict=None,
)
Add transit station nodes and dedicated transit links to the roadway network.
Creates new roadway nodes for transit stations and adds dedicated transit links between stations for fixed-guideway transit (rail, subway, ferry, etc.). Bus stops use existing roadway nodes from match_bus_stops_to_roadway_nodes().
Process Steps: 1. Creates stop link pairs from consecutive stops in stop_times 2. Aggregates intermediate shape points between stops into multi-point lines 3. Filters links to STATION_ROUTE_TYPES for network addition 4. Creates new roadway nodes for stations not already in network 5. Creates dedicated transit links with appropriate access restrictions 6. Updates feed_tables[‘stops’] with model_node_id for all stops 7. Updates feed_tables[‘shapes’] with shape_model_node_id for all stations 8. Returns bus stop links separately (not added to network)
Modifies in place:
roadway_net - Adds
- New nodes for transit stations with model_node_id
- New links between stations with:
- rail_only=True for rail types
- ferry_only=True for ferry types
- drive/bike/walk/truck_access=False
- Geometry following shape points if available
feed_tables[‘stops’] - Adds/updates: - model_node_id (int): Roadway node ID for the stop - Updates existing bus stop model_node_ids - Adds new station model_node_ids
feed_tables[‘shapes’] - Adds/updates: - shape_model_node_id (int): Roadway node ID for the shape point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict[str, DataFrame]
|
dictionary with required tables: - ‘stops’: Stop information with geometry - ‘stop_times’: Stop sequences for trips - ‘shapes’: Shape points between stops - ‘routes’: Route types |
required |
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork to modify with new nodes/links |
required |
local_crs
|
str
|
Coordinate reference system for projections |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
trace_shape_ids
|
list[str] | None
|
Optional shape IDs for debug logging |
None
|
default_node_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new nodes. |
None
|
default_link_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new links. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, int], GeoDataFrame]
|
tuple[dict[str,int], gpd.GeoDataFrame]: - dictionary mapping new station stop_ids to model_node_ids - GeoDataFrame of bus stop links (not added to network) with columns: shape_id, stop_sequence, stop_id, stop_name, next_stop_id, next_stop_name, A, B, geometry |
Notes
- Stations are new nodes; bus stops use existing road nodes
- Self-loops (stop appearing twice consecutively) are filtered out
- Links follow actual shape geometry when available
- Parent stations without trips are handled correctly
Source code in network_wrangler/utils/transit.py
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 | |
network_wrangler.utils.transit.add_unmatched_bus_stops_to_network ¶
add_unmatched_bus_stops_to_network(
feed_tables,
roadway_net,
local_crs,
max_distance,
trace_shape_ids=None,
default_node_attribute_dict=None,
)
Add unmatched bus stops as new nodes in the roadway network.
Creates new roadway nodes for bus stops that couldn’t be matched to the bus-accessible network. Clusters nearby unmatched stops (e.g., at transit stations) and creates one node per cluster at the centroid location.
Process Steps: 1. Identifies unmatched bus stops (poor_match=True) 2. Clusters stops using max_distance threshold with DBSCAN 3. Calculates centroid for each cluster 4. Creates new roadway nodes at cluster centroids 5. Updates stops table with new node IDs and locations 6. Adds nodes to roadway network
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict[str, DataFrame]
|
dictionary of GTFS feed tables with ‘stops’ containing: - poor_match (bool): True for unmatched stops - model_node_id (int): Nearest bus-accessible node (for creating connector links) - Other stop attributes |
required |
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork to add nodes to |
required |
local_crs
|
str
|
Coordinate reference system for projections (e.g., “EPSG:2227”) |
required |
max_distance
|
float
|
Distance threshold in crs_units for clustering |
required |
trace_shape_ids
|
list[str] | None
|
Optional list of shape_ids for debug logging |
None
|
default_node_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new nodes. |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame of added nodes with columns: - model_node_id (int): New node ID - X, Y (float): Node coordinates in lat/lon - geometry (Point): Node geometry - cluster_id (int): Cluster assignment - stop_ids (list): List of stop_ids in this cluster - stop_names (list): List of stop names in this cluster - nearest_bus_node (int): Nearest bus-accessible node for connectivity - is_transit_stop_node (bool): True (marks these as special transit nodes) |
Notes
- Only processes bus stops (not rail/ferry stations)
- Clusters stops within max_distance of each other
- One node created per cluster at centroid
- Original GTFS stop locations preserved before updating
- Modifies feed_tables[‘stops’] in place
- Modifies roadway_net.nodes_df in place
Source code in network_wrangler/utils/transit.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 | |
network_wrangler.utils.transit.assess_stop_name_roadway_compatibility ¶
assess_stop_name_roadway_compatibility(
stop_name,
node_link_names,
threshold=0.5,
config=DefaultConfig,
)
Assess if a transit stop name is compatible with a roadway node’s link names.
Checks if street names in the stop name match any of the node’s connected link names. Handles common patterns like “Street1 & Street2” or “Street1 at Street2”.
Exact name matches receive a special high score of 10.0 to enable users to force specific stop-to-node matches by ensuring the stop name exactly matches a link name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_name
|
str
|
Name of the transit stop (e.g., “Van Ness Ave & Market St”) |
required |
node_link_names
|
list[str]
|
List of link names connected to the roadway node |
required |
threshold
|
float
|
Minimum fraction of stop streets that must match node links (default 0.5) |
0.5
|
config
|
WranglerConfig
|
WranglerConfig with TRANSIT.MIN_SUBSTRING_MATCH_LENGTH setting. |
DefaultConfig
|
Returns:
| Type | Description |
|---|---|
tuple[bool, float, list[str]]
|
Tuple of (is_compatible, match_score, matched_streets) where: - is_compatible: True if stop name is compatible with node - match_score: 10.0 for exact match (to force selection), otherwise fraction of stop streets found in node links (0.0 to 1.0) - matched_streets: List of street names from stop that matched node links |
Source code in network_wrangler/utils/transit.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
network_wrangler.utils.transit.calculate_path_deviation_from_shape ¶
Calculate total deviation of a path from original shape points.
Creates a LineString from the path nodes and calculates the distance from each shape point to the nearest point on the line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_nodes
|
list
|
List of roadway node IDs in the path |
required |
original_shape_points
|
DataFrame
|
DataFrame of original GTFS shape points |
required |
roadway_net
|
RoadwayNetwork to get node coordinates |
required | |
trace
|
bool
|
If True, enable trace logging for debugging |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Total deviation distance (sum of distances from shape points to path line) |
Source code in network_wrangler/utils/transit.py
3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 | |
network_wrangler.utils.transit.create_connector_links_for_poor_match_stops ¶
create_connector_links_for_poor_match_stops(
roadway_net,
unmatched_stops_gdf,
local_crs,
crs_units,
trace_shape_ids=None,
default_link_attribute_dict=None,
)
Create connector links between poor match bus stop nodes and nearest bus-accessible nodes.
Creates bidirectional bus-only connector links in the roadway network to enable routing through bus stops that couldn’t be matched directly to existing roadway nodes. These are typically stops that are too far from the road network (poor_match stops added as new nodes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork to add connector links to |
required |
unmatched_stops_gdf
|
GeoDataFrame
|
GeoDataFrame of unmatched stops with columns: - model_node_id (int): New transit stop node ID - nearest_bus_node (int): Nearest bus-accessible node for connectivity - stop_ids, stop_names: Stop information - geometry (Point): Stop location |
required |
local_crs
|
str
|
Coordinate reference system for distance calculations |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
trace_shape_ids
|
list[str] | None
|
Optional list of shape_ids for debug logging |
None
|
default_link_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new links. |
None
|
Notes
- Creates bidirectional links (forward and reverse) for each stop
- Links are marked with ref=”unmatched_bus_stop” for identification
- All links are bus_only=True, no other mode access
- Modifies roadway_net.links_df and roadway_net.shapes in place
Source code in network_wrangler/utils/transit.py
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
network_wrangler.utils.transit.create_feed_from_gtfs_model ¶
create_feed_from_gtfs_model(
gtfs_model,
roadway_net,
local_crs,
crs_units,
timeperiods,
frequency_method,
default_frequency_for_onetime_route=180,
add_stations_and_links=True,
max_stop_distance=None,
trace_shape_ids=None,
errors="raise",
default_node_attribute_dict=None,
default_link_attribute_dict=None,
)
Convert GTFS model to Wrangler Feed with stops mapped to roadway network.
Comprehensive conversion that transforms GTFS schedule data into a frequency-based Feed representation compatible with travel modeling. Maps transit stops to roadway nodes and optionally adds station infrastructure to the network.
Process Steps: 1. Prepare roadway network: - Convert roadway_net.nodes_df to GeoDataFrame if needed - Create Point geometries from X, Y coordinates - Set CRS to LAT_LON_CRS (EPSG:4326) - Modifies roadway_net.nodes_df in place
- Copy GTFS tables to feed_tables dictionary:
- Copy routes, trips, agencies, stops, stop_times, shapes from gtfs_model
- Convert stops to GeoDataFrame with Point geometries from stop_lon/stop_lat
-
Creates feed_tables dict for all subsequent operations
-
Enrich stops with route/agency metadata:
- Calls:
add_additional_data_to_stops() - Joins route and agency information to each stop via stop_times and trips
- Adds columns: agency_ids, agency_names, route_ids, route_names, route_types, shape_ids, is_parent, is_bus_stop
-
Modifies feed_tables[‘stops’] in place
-
Create frequency-based schedules from timetables:
- Calls: [
create_feed_frequencies()][network_wrangler.models.gtfs.converters.create_feed_frequencies] - Converts GTFS trip-based schedules to frequency-based representation
- Groups trips by stop pattern (shape_id) and time period
- Calculates headways using specified method (uniform/mean/median)
- Creates one representative trip per shape_id
- Creates feed_tables[‘frequencies’] table
-
Modifies: feed_tables[‘stop_times’] (adds departure_minutes), feed_tables[‘trips’] (one row per shape_id)
-
Match stops to shape points and enrich shapes:
- Calls:
add_additional_data_to_shapes() - For each shape_id, processes stops in sequence order
- For each stop:
- Match: Find nearest existing shape point within threshold (forward-only search)
- Insert: If no match, create new shape point at stop location
- Uses local minimum matching to handle routes that double back
- Renumbers shape_pt_sequence if duplicates or non-integers detected
- Writes debug_shapes.geojson with stop matching information
- Calls helpers:
_match_stop_to_shape_points(),_insert_stop_into_shape(),_align_shape_with_stops(),_write_debug_shapes() -
Modifies feed_tables[‘shapes’]: adds stop_id, stop_name, stop_sequence, match_distance_{crs_units}, poor_match
-
Match bus stops to roadway nodes:
- Calls:
match_bus_stops_to_roadway_nodes() - Gets bus modal graph from roadway network (bus-accessible nodes only)
- For each bus stop:
- Finds K nearest bus-accessible nodes using BallTree spatial index
- If use_name_matching=True: scores by distance + name compatibility (combined_score = 0.1 * normalized_dist + 0.9 * (1 - name_score))
- Selects best match within max_distance threshold
- If name matching enabled: marks stops with combined_score > 0.9 as poor_match:
- Sets poor_match = True (does not update location yet)
- Keeps model_node_id as nearest bus-accessible node (for connector links)
- These stops will be added to network in step 6a
- If name matching disabled: poor_match = False for all stops
- Updates stop locations to matched node positions (except poor_match stops)
- Modifies feed_tables[‘stops’]: adds model_node_id, match_distance_{crs_units}, close_match, poor_match (always added, but only True when name matching enabled), node_link_names, name_match_score, normalized_dist, combined_score
- Modifies feed_tables[‘shapes’]: updates bus stop shape points to node locations, adds poor_match flag
- Modifies roadway_net.nodes_df: adds bus_access column
6a. Add unmatched bus stops to network:
- Calls: add_unmatched_bus_stops_to_network()
- Identifies unmatched bus stops (poor_match=True)
- Clusters nearby stops using DBSCAN (max_distance threshold)
- Creates new roadway nodes at cluster centroids
- One node per cluster for grouped transit stations
- Updates feed_tables[‘stops’] with new node IDs
- Modifies roadway_net.nodes_df: adds new transit stop nodes with is_transit_stop_node flag
- Add rail/ferry stations and links to roadway network:
- Calls:
add_stations_and_links_to_roadway_network() - For STATION_ROUTE_TYPES (rail, light rail, subway, etc.):
- Creates new nodes for each station
- Adds dedicated transit links between consecutive stations
- Links follow original GTFS shape geometry
- For BUS/TROLLEYBUS routes:
- Creates bus_stop_links_gdf with consecutive stop pairs (A->B)
- Includes route metadata (route_id, trip_id, direction_id, etc.)
- No new nodes/links added yet (handled in next step)
- Returns station_id_to_model_node_id_dict and bus_stop_links_gdf
- Modifies roadway_net.nodes_df: adds station nodes
- Modifies roadway_net.links_df: adds station-to-station links
- Modifies feed_tables[‘stops’]: updates station stops with new model_node_ids
- Modifies feed_tables[‘shapes’]: removes intermediate shape points for station routes, keeps only station stop points
7a. Create connector links for unmatched bus stops:
- Calls: create_connector_links_for_poor_match_stops()
- Creates bidirectional bus-only links between:
- New transit stop nodes (from step 6a) to their nearest bus-accessible node
- Links marked with ref=”unmatched_bus_stop”
- Enables routing through these previously unmatched stops
- Modifies roadway_net.links_df: adds connector links
- Modifies roadway_net.shapes: adds link geometries
- Route bus services through road network:
- Calls:
route_shapes_between_stops() - Gets bus modal graph (DiGraph for pathfinding)
- For each consecutive bus stop pair (A->B):
- Check: If either node not in bus graph (e.g., poor_match):
- Add direct A->B connection to bus_node_sequence
- Add to no_path_sequence for special handling
- Skip pathfinding
- Find path: Use NetworkX shortest path through bus network
- Optional: Shape-aware routing (prefers paths close to original shape)
- Create shape points: Add all intermediate nodes in path as shape points
- Check: If either node not in bus graph (e.g., poor_match):
- Handles exceptions (NetworkXNoPath, NodeNotFound): adds to no_path_sequence
- If errors=’raise’ and no_path_sequence not empty: raises TransitValidationError
- If errors=’ignore’ and no_path_sequence not empty: calls create_links_for_failed_bus_paths() to create special bus-only links (marked ref=”bad_bus_path”) for failed routing segments
- Calls helpers:
get_original_shape_points_between_stops(),find_shape_aware_shortest_path() -
Modifies feed_tables[‘shapes’]: replaces bus route shapes with routed paths through road network, adds shape_model_node_id from roadway
-
Consolidate duplicate stops mapped to same node:
- Renames stop_id -> stop_id_GTFS (original GTFS IDs)
- Renames model_node_id -> stop_id (now uses network node IDs as stop IDs)
- Multiple GTFS stops may map to same network node
- Groups by stop_id (model_node_id) and aggregates:
- Converts singular fields to lists (stop_id_GTFS, stop_name, etc.)
- Takes first geometry/location
- Merges route/agency lists (flattens and deduplicates)
- Creates stop_id_to_model_node_id_dict mapping GTFS stop_id -> model_node_id
-
Modifies feed_tables[‘stops’]: consolidated rows, one per unique network node
-
Update stop references and create Feed object:
- Updates feed_tables[‘stop_times’]: maps stop_id_GTFS -> stop_id (model_node_id)
- Converts stop_times to Wrangler format
- Creates Feed object with all processed tables:
- routes, trips, agencies: from GTFS
- stops: consolidated by model_node_id with metadata
- stop_times: with updated stop_id references
- shapes: routed through road network with shape_model_node_id
- frequencies: frequency-based schedules by time period
- Returns Feed object ready for network modeling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gtfs_model
|
GtfsModel
|
Source GTFS data model |
required |
roadway_net
|
RoadwayNetwork
|
Target roadway network for stop mapping |
required |
local_crs
|
str
|
Coordinate system for distance calculations |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
timeperiods
|
dict[str, tuple[str, str]]
|
Time period definitions for frequencies Example: {‘EA’: (‘03:00’,‘06:00’), ‘AM’: (‘06:00’,‘10:00’)} |
required |
frequency_method
|
Literal['uniform_headway', 'mean_headway', 'median_headway']
|
How to calculate headways (‘uniform_headway’, ‘mean_headway’, or ‘median_headway’) |
required |
default_frequency_for_onetime_route
|
int
|
Default headway in minutes for routes with one trip per period (default: 180) |
180
|
add_stations_and_links
|
bool
|
If True, add stations to roadway network (recommended, False not implemented) |
True
|
max_stop_distance
|
float | None
|
Maximum distance in crs_units for matching bus stops to roadway nodes. If None, uses default MAX_DISTANCE_STOP values |
None
|
trace_shape_ids
|
list[str] | None
|
Shape IDs for detailed debug logging |
None
|
errors
|
Literal['raise', 'ignore']
|
How to handle routing errors (‘raise’ or ‘ignore’) |
'raise'
|
default_node_attribute_dict
|
dict[str, any] | None
|
node attributes to set for new transit nodes. Defaults to None. |
None
|
default_link_attribute_dict
|
dict[str, any] | None
|
link attributes to set for new transit links. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Feed |
Feed
|
Wrangler Feed object with: - Stops mapped to roadway nodes - Frequency-based trip representation - Routes following road network paths |
Raises:
| Type | Description |
|---|---|
TransitValidationError
|
If bus stops can’t be matched to roadway |
NodeNotFoundError
|
If required nodes aren’t found |
Notes
- Bus routes are re-routed through actual road network
- Station routes keep original alignment with new nodes/links
Source code in network_wrangler/utils/transit.py
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 | |
network_wrangler.utils.transit.create_links_for_failed_bus_paths ¶
create_links_for_failed_bus_paths(
roadway_net,
no_bus_path_gdf,
local_crs,
crs_units,
trace_shape_ids=None,
default_link_attribute_dict=None,
)
Create direct transit-only links for bus stop pairs that couldn’t be routed.
When pathfinding through the bus network fails for consecutive bus stop pairs, this function creates direct transit-only links connecting them. These links enable the transit route to continue even when the underlying road network doesn’t provide a valid bus path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork to add links to |
required |
no_bus_path_gdf
|
GeoDataFrame
|
GeoDataFrame of failed bus path segments with columns: - A, B (int): Stop node IDs that couldn’t be connected via pathfinding - trip_id, stop_sequence: Trip and sequence information - stop_id, stop_name: Stop identifiers - next_stop_id, next_stop_name: Next stop identifiers - geometry (LineString): Direct connection geometry between stops - route_type, route_id, direction_id, shape_id: Route metadata |
required |
local_crs
|
str
|
Coordinate reference system for distance calculations |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
trace_shape_ids
|
list[str] | None
|
Optional list of shape_ids for debug logging |
None
|
default_link_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new links. |
None
|
Notes
- Links are marked with ref=”bad_bus_path” for identification
- Links allow all transit modes (bus_only, rail_only, ferry_only = True)
- If a link already exists for an A->B pair, transit access is added to existing link
- Modifies roadway_net.links_df and roadway_net.shapes in place
Source code in network_wrangler/utils/transit.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 | |
network_wrangler.utils.transit.drop_transit_agency ¶
Remove all routes, trips, stops, etc. for a specific agency or agencies.
Filters out all data associated with the specified agency_id(s), ensuring the resulting transit data remains valid by removing orphaned stops and maintaining referential integrity. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to filter. Modified in place. |
required |
agency_id
|
str | list[str]
|
Single agency_id string or list of agency_ids to remove |
required |
Example
Remove a single agency ¶
drop_transit_agency(gtfs_model, “SFMTA”)
Remove multiple agencies ¶
drop_transit_agency(gtfs_model, [“SFMTA”, “AC”])
Source code in network_wrangler/transit/filter.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
network_wrangler.utils.transit.filter_transit_by_boundary ¶
Filter transit routes based on whether they have stops within a boundary.
Removes routes that are entirely outside the boundary shapefile. Routes that are partially within the boundary are kept by default, but can be configured per route type to be truncated at the boundary. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to filter. Modified in place. |
required |
boundary
|
str | Path | GeoDataFrame
|
Path to boundary shapefile or a GeoDataFrame with boundary polygon(s) |
required |
partially_include_route_type_action
|
dict[RouteType, str] | None
|
Optional dictionary mapping RouteType enum to action for routes partially within boundary: - “truncate”: Truncate route to only include stops within boundary Route types not specified in this dictionary will be kept entirely (default). |
None
|
Example
from network_wrangler.models.gtfs.types import RouteType
Remove routes entirely outside the Bay Area ¶
filtered_gtfs = filter_transit_by_boundary(gtfs_model, “bay_area_boundary.shp”)
Truncate rail routes at boundary, keep other route types unchanged ¶
filtered_gtfs = filter_transit_by_boundary( … gtfs_model, … “bay_area_boundary.shp”, … partially_include_route_type_action={ … RouteType.RAIL: “truncate”, # Rail - will be truncated at boundary … # Other route types not listed will be kept entirely … }, … )
Todo
This is similar to clip_feed_to_boundary – consolidate?
Source code in network_wrangler/transit/filter.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
network_wrangler.utils.transit.find_shape_aware_shortest_path ¶
find_shape_aware_shortest_path(
G_bus,
start_node,
end_node,
original_shape_points,
roadway_net,
tolerance=1.1,
trace=False,
)
Find shortest path that considers original shape points.
Uses constrained shortest path approach: 1. Find shortest distance 2. Get all paths within tolerance of shortest distance 3. Among those, select path with minimum deviation from original shape
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_bus
|
DiGraph
|
NetworkX DiGraph of bus network |
required |
start_node
|
int
|
Starting node ID |
required |
end_node
|
int
|
Ending node ID |
required |
original_shape_points
|
DataFrame
|
DataFrame of original GTFS shape points |
required |
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork to get node coordinates |
required |
tolerance
|
float
|
Maximum ratio of path distance to shortest distance (default 1.10 = 110%) |
1.1
|
trace
|
bool
|
Whether to log trace information |
False
|
Returns:
| Type | Description |
|---|---|
list
|
List of node IDs representing the best path |
Source code in network_wrangler/utils/transit.py
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 | |
network_wrangler.utils.transit.get_original_shape_points_between_stops ¶
get_original_shape_points_between_stops(
feed_tables,
shape_id,
from_stop_seq,
to_stop_seq,
trace=False,
)
Get original GTFS shape points between two consecutive stops.
Uses stop_sequence information already added by add_additional_data_to_shapes().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict
|
GTFS feed tables dictionary |
required |
shape_id
|
str
|
Shape identifier |
required |
from_stop_seq
|
int
|
Starting stop sequence number |
required |
to_stop_seq
|
int
|
Ending stop sequence number (should be from_stop_seq + 1) |
required |
trace
|
bool
|
If True, enable trace logging for debugging |
False
|
Returns:
| Type | Description |
|---|---|
|
DataFrame of shape points between stops, or empty DataFrame if not found |
Source code in network_wrangler/utils/transit.py
3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 | |
network_wrangler.utils.transit.match_bus_stops_to_roadway_nodes ¶
match_bus_stops_to_roadway_nodes(
feed_tables,
roadway_net,
local_crs,
crs_units,
max_distance,
trace_shape_ids=None,
use_name_matching=True,
name_match_weight=None,
config=DefaultConfig,
)
Match bus stops to bus-accessible nodes in the roadway network.
Matches bus and trolleybus stops to the nearest bus-accessible nodes in the roadway network using spatial proximity and optionally street name compatibility. Updates stop and shape locations to snap to road nodes.
Process Steps:
- Identifies bus stops (route_types BUS or TROLLEYBUS) in feed_tables[‘stops’]
- Builds bus network graph from roadway to find accessible nodes
- Projects geometries to local CRS for accurate distance calculations
- Uses BallTree spatial index to find candidate nodes within max_distance
- If name matching is enabled and link_names exist, scores candidates by both distance and name compatibility, selecting best match within max_distance
- Marks stops with combined_score > 0.9 as poor_match=True (only when name matching enabled)
- Excludes stops that serve station route types (rail, ferry, etc.) - these are handled separately
- For poor_match stops, their model_node_id is the nearest bus-accessible node (to use for creating connector links later)
- Updates stop locations to matched road node locations (except poor_match stops)
- Updates shape point locations for matched bus stops
Modifies feed_tables in place:
-
feed_tables[‘stops’] - Adds/modifies columns:
- is_bus_stop (bool): True if stop serves BUS or TROLLEYBUS routes
- model_node_id (int): Matched roadway node ID (None if no close match)
- match_distance_{crs_units} (float): Distance to matched node
- close_match (bool): True if match found within max_distance
- poor_match (bool): True if combined_score > 0.9 AND stop doesn’t serve station routes (only when name matching enabled)
- stop_lon, stop_lat, geometry: Updated to road node location if close_match and not poor_match
-
feed_tables[‘shapes’] - Adds/modifies columns:
- shape_model_node_id (int): Matched roadway node ID for bus stops
- match_distance_{crs_units} (float): Distance to matched node
- shape_pt_lon, shape_pt_lat, geometry: Updated to road node location if valid match
-
feed_tables[‘stop_times’] - If GeoDataFrame, updates:
- geometry: Updated to matched road node location for bus stops
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_tables
|
dict[str, DataFrame]
|
dictionary of GTFS feed tables. Expects: - ‘stops’: Must have route_types column (list of RouteType enums) - ‘shapes’: Shape points to update - ‘stop_times’: Optional, updated if present as GeoDataFrame |
required |
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork with nodes to match against. Will be converted to GeoDataFrame if needed. |
required |
local_crs
|
str
|
Coordinate reference system for projections (e.g., “EPSG:2227”) |
required |
crs_units
|
str
|
Distance units for local_crs (‘feet’ or ‘meters’) |
required |
max_distance
|
float
|
Maximum matching distance in crs_units |
required |
trace_shape_ids
|
list[str] | None
|
Optional list of shape_ids for debug logging |
None
|
use_name_matching
|
bool
|
If True and nodes have ‘link_names’, will consider name compatibility when selecting best match within max_distance. Default is True. |
True
|
name_match_weight
|
float | None
|
Weight for name match score in combined scoring (0.0 to 1.0). Final score = (1 - name_match_weight) * normalized_distance + name_match_weight * name_score Defaults to NAME_MATCH_WEIGHT constant. |
None
|
config
|
WranglerConfig
|
WranglerConfig with TRANSIT settings for name matching thresholds. |
DefaultConfig
|
Raises:
| Type | Description |
|---|---|
TransitValidationError
|
If no bus-accessible nodes found near any bus stops |
Notes
- Only matches stops that serve BUS or TROLLEYBUS routes
- Uses bus modal graph to ensure matched nodes are bus-accessible
- Preserves original locations for non-bus stops
Source code in network_wrangler/utils/transit.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
network_wrangler.utils.transit.route_shapes_between_stops ¶
route_shapes_between_stops(
bus_stop_links_gdf,
feed_tables,
roadway_net,
local_crs,
crs_units,
trace_shape_ids=None,
errors="raise",
default_link_attribute_dict=None,
)
Find shortest paths through the bus network between consecutive bus stops.
Replaces original bus route shapes with new shapes that follow the actual bus network by finding shortest paths between consecutive stops through bus-accessible roads.
Process Steps: 1. Sorts bus stop links by shape_id and stop_sequence 2. Gets bus modal graph from roadway network 3. For each consecutive stop pair in each shape: - Finds shortest path through bus network using NetworkX - Creates shape points for all nodes in the path - Preserves stop information at stop nodes 4. Replaces bus shapes in feed_tables[‘shapes’] with new routed shapes
Modifies feed_tables[‘shapes’] in place: - Removes existing bus/trolleybus shapes - Adds new shapes with points following road network paths - Each shape point has shape_model_node_id from roadway network - Stop points retain stop_id, stop_name, stop_sequence
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bus_stop_links_gdf
|
GeoDataFrame
|
GeoDataFrame of bus stop pairs, required columns: - shape_id (str): Shape identifier - stop_sequence (int): Stop order in route - stop_id (str): Current stop ID - stop_name (str): Current stop name - next_stop_id (str): Next stop ID - next_stop_name (str): Next stop name - A (int): Current stop’s model_node_id - B (int): Next stop’s model_node_id - route_id, route_type, trip_id, direction_id: Route metadata |
required |
feed_tables
|
dict[str, DataFrame]
|
dictionary with required tables: - ‘stops’: Must have is_bus_stop column - ‘shapes’: Will be modified to replace bus shapes |
required |
roadway_net
|
RoadwayNetwork
|
RoadwayNetwork with bus modal graph |
required |
local_crs
|
str
|
Coordinate reference system for projections |
required |
crs_units
|
str
|
Distance units (‘feet’ or ‘meters’) |
required |
trace_shape_ids
|
list[str] | None
|
Optional shape IDs for debug logging |
None
|
errors
|
Literal['raise', 'ignore']
|
‘raise’ or ‘ignore’ |
'raise'
|
default_link_attribute_dict
|
dict[str, any] | None
|
Optional dict of column-name to default value to set on new links. |
None
|
Raises:
| Type | Description |
|---|---|
TransitValidationError
|
If no path exists between any consecutive stops. Exception includes no_bus_path_gdf with failed stop sequences. |
Notes
- Uses NetworkX shortest_path for routing
- Intermediate nodes between stops are added as shape points
- Original shape geometry is replaced with routed paths
Source code in network_wrangler/utils/transit.py
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 | |
network_wrangler.utils.transit.truncate_route_at_stop ¶
Truncate all trips of a route at a specific stop.
Removes stops before or after the specified stop for all trips matching the given route_id and direction_id. This is useful for shortening routes at terminal stations or service boundaries. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to modify. Modified in place. |
required |
route_id
|
str
|
The route_id to truncate |
required |
direction_id
|
int
|
The direction_id of trips to truncate (0 or 1) |
required |
stop_id
|
str | int
|
The stop where truncation occurs. For GtfsModel, this should be a string stop_id. For Feed, this should be an integer model_node_id. |
required |
truncate
|
Literal['before', 'after']
|
Either “before” to remove stops before stop_id, or “after” to remove stops after stop_id |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If truncate is not “before” or “after” |
ValueError
|
If stop_id is not found in any trips of the route/direction |
Example
Truncate outbound BART trips to end at Embarcadero (GtfsModel) ¶
truncate_route_at_stop( … gtfs_model, … route_id=”BART-01”, … direction_id=0, … stop_id=”EMBR”, # string stop_id … truncate=”after”, … )
Truncate outbound BART trips to end at node 12345 (Feed) ¶
truncate_route_at_stop( … feed, … route_id=”BART-01”, … direction_id=0, … stop_id=12345, # integer model_node_id … truncate=”after”, … )
Source code in network_wrangler/transit/filter.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | |
Functions to clip a TransitNetwork object to a boundary.
Clipped transit is an independent transit network that is a subset of the original transit network.
Example usage:
from network_wrangler.transit load_transit, write_transit
from network_wrangler.transit.clip import clip_transit
stpaul_transit = load_transit(example_dir / "stpaul")
boundary_file = test_dir / "data" / "ecolab.geojson"
clipped_network = clip_transit(stpaul_transit, boundary_file=boundary_file)
write_transit(clipped_network, out_dir, prefix="ecolab", format="geojson", true_shape=True)
network_wrangler.transit.clip.clip_feed_to_boundary ¶
clip_feed_to_boundary(
feed,
ref_nodes_df,
boundary_gdf=None,
boundary_geocode=None,
boundary_file=None,
min_stops=DEFAULT_MIN_STOPS,
)
Clips a transit Feed object to a boundary and returns the resulting GeoDataFrames.
Retains only the stops within the boundary and trips that traverse them subject to a minimum
number of stops per trip as defined by min_stops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
Feed object to be clipped. |
required |
ref_nodes_df
|
GeoDataFrame
|
geodataframe with node geometry to reference |
required |
boundary_geocode
|
Union[str, dict]
|
A geocode string or dictionary representing the boundary. Defaults to None. |
None
|
boundary_file
|
Union[str, Path]
|
A path to the boundary file. Only used if boundary_geocode is None. Defaults to None. |
None
|
boundary_gdf
|
GeoDataFrame
|
A GeoDataFrame representing the boundary. Only used if boundary_geocode and boundary_file are None. Defaults to None. |
None
|
min_stops
|
int
|
minimum number of stops needed to retain a transit trip within clipped area. Defaults to DEFAULT_MIN_STOPS which is set to 2. |
DEFAULT_MIN_STOPS
|
Source code in network_wrangler/transit/clip.py
network_wrangler.transit.clip.clip_feed_to_roadway ¶
Returns a copy of transit feed clipped to the roadway network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
Transit Feed to clip. |
required |
roadway_net
|
RoadwayNetwork
|
Roadway network to clip to. |
required |
min_stops
|
int
|
minimum number of stops needed to retain a transit trip within clipped area. Defaults to DEFAULT_MIN_STOPS which is set to 2. |
DEFAULT_MIN_STOPS
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no stops found within the roadway network. |
Returns:
| Name | Type | Description |
|---|---|---|
Feed |
Feed
|
Clipped deep copy of feed limited to the roadway network. |
Source code in network_wrangler/transit/clip.py
network_wrangler.transit.clip.clip_transit ¶
clip_transit(
network,
node_ids=None,
boundary_geocode=None,
boundary_file=None,
boundary_gdf=None,
ref_nodes_df=None,
roadway_net=None,
min_stops=DEFAULT_MIN_STOPS,
)
Returns a new TransitNetwork clipped to a boundary as determined by arguments.
Will clip based on which arguments are provided as prioritized below:
- If
node_idsprovided, will clip based onnode_ids - If
boundary_geocodeprovided, will clip based on on search in OSM for that jurisdiction boundary using reference geometry fromref_nodes_df,roadway_net, orroadway_path - If
boundary_fileprovided, will clip based on that polygon using reference geometry fromref_nodes_df,roadway_net, orroadway_path - If
boundary_gdfprovided, will clip based on that geodataframe using reference geometry fromref_nodes_df,roadway_net, orroadway_path - If
roadway_netprovided, will clip based on that roadway network
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
TransitNetwork
|
TransitNetwork to clip. |
required |
node_ids
|
list[str]
|
A list of node_ids to clip to. Defaults to None. |
None
|
boundary_geocode
|
Union[str, dict]
|
A geocode string or dictionary representing the boundary. Only used if node_ids are None. Defaults to None. |
None
|
boundary_file
|
Union[str, Path]
|
A path to the boundary file. Only used if node_ids and boundary_geocode are None. Defaults to None. |
None
|
boundary_gdf
|
GeoDataFrame
|
A GeoDataFrame representing the boundary. Only used if node_ids, boundary_geocode and boundary_file are None. Defaults to None. |
None
|
ref_nodes_df
|
None | GeoDataFrame
|
GeoDataFrame of geographic references for node_ids. Only used if node_ids is None and one of boundary_* is not None. |
None
|
roadway_net
|
None | RoadwayNetwork
|
Roadway Network instance to clip transit network to. Only used if node_ids is None and allof boundary_* are None |
None
|
min_stops
|
int
|
minimum number of stops needed to retain a transit trip within clipped area. Defaults to DEFAULT_MIN_STOPS which is set to 2. |
DEFAULT_MIN_STOPS
|
Source code in network_wrangler/transit/clip.py
Functions to filter transit feeds by various criteria.
Filtered transit feeds are subsets of the original feed based on selection criteria like service_ids, route_types, etc.
network_wrangler.transit.filter.MAX_TRUNCATION_WARNING_STOPS
module-attribute
¶
Maximum number of removed stops to list individually in truncation warnings.
Used in truncate_route_at_stop() to control verbosity of warning messages. If more stops are removed, only the count is shown instead of listing each stop.
network_wrangler.transit.filter.MIN_ROUTE_SEGMENTS
module-attribute
¶
Minimum number of boundary segments before warning about complex route patterns.
Used in filter_transit_by_boundary() to detect routes that exit and re-enter the boundary.
network_wrangler.transit.filter.drop_transit_agency ¶
Remove all routes, trips, stops, etc. for a specific agency or agencies.
Filters out all data associated with the specified agency_id(s), ensuring the resulting transit data remains valid by removing orphaned stops and maintaining referential integrity. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to filter. Modified in place. |
required |
agency_id
|
str | list[str]
|
Single agency_id string or list of agency_ids to remove |
required |
Example
Remove a single agency ¶
drop_transit_agency(gtfs_model, “SFMTA”)
Remove multiple agencies ¶
drop_transit_agency(gtfs_model, [“SFMTA”, “AC”])
Source code in network_wrangler/transit/filter.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
network_wrangler.transit.filter.filter_feed_by_service_ids ¶
Filter a transit feed to only include trips for specified service_ids.
Filters trips, stop_times, and stops to only include data related to the specified service_ids. Also ensures parent stations are retained if referenced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed | GtfsModel
|
Feed or GtfsModel object to filter |
required |
service_ids
|
list[str]
|
List of service_ids to retain |
required |
Returns:
| Type | Description |
|---|---|
Feed | GtfsModel
|
Union[Feed, GtfsModel]: Filtered copy of feed with only trips/stops/stop_times for specified service_ids. Returns same type as input. |
Source code in network_wrangler/transit/filter.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
network_wrangler.transit.filter.filter_transit_by_boundary ¶
Filter transit routes based on whether they have stops within a boundary.
Removes routes that are entirely outside the boundary shapefile. Routes that are partially within the boundary are kept by default, but can be configured per route type to be truncated at the boundary. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to filter. Modified in place. |
required |
boundary
|
str | Path | GeoDataFrame
|
Path to boundary shapefile or a GeoDataFrame with boundary polygon(s) |
required |
partially_include_route_type_action
|
dict[RouteType, str] | None
|
Optional dictionary mapping RouteType enum to action for routes partially within boundary: - “truncate”: Truncate route to only include stops within boundary Route types not specified in this dictionary will be kept entirely (default). |
None
|
Example
from network_wrangler.models.gtfs.types import RouteType
Remove routes entirely outside the Bay Area ¶
filtered_gtfs = filter_transit_by_boundary(gtfs_model, “bay_area_boundary.shp”)
Truncate rail routes at boundary, keep other route types unchanged ¶
filtered_gtfs = filter_transit_by_boundary( … gtfs_model, … “bay_area_boundary.shp”, … partially_include_route_type_action={ … RouteType.RAIL: “truncate”, # Rail - will be truncated at boundary … # Other route types not listed will be kept entirely … }, … )
Todo
This is similar to clip_feed_to_boundary – consolidate?
Source code in network_wrangler/transit/filter.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
network_wrangler.transit.filter.truncate_route_at_stop ¶
Truncate all trips of a route at a specific stop.
Removes stops before or after the specified stop for all trips matching the given route_id and direction_id. This is useful for shortening routes at terminal stations or service boundaries. Modifies transit_data in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_data
|
GtfsModel | Feed
|
Either a GtfsModel or Feed object to modify. Modified in place. |
required |
route_id
|
str
|
The route_id to truncate |
required |
direction_id
|
int
|
The direction_id of trips to truncate (0 or 1) |
required |
stop_id
|
str | int
|
The stop where truncation occurs. For GtfsModel, this should be a string stop_id. For Feed, this should be an integer model_node_id. |
required |
truncate
|
Literal['before', 'after']
|
Either “before” to remove stops before stop_id, or “after” to remove stops after stop_id |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If truncate is not “before” or “after” |
ValueError
|
If stop_id is not found in any trips of the route/direction |
Example
Truncate outbound BART trips to end at Embarcadero (GtfsModel) ¶
truncate_route_at_stop( … gtfs_model, … route_id=”BART-01”, … direction_id=0, … stop_id=”EMBR”, # string stop_id … truncate=”after”, … )
Truncate outbound BART trips to end at node 12345 (Feed) ¶
truncate_route_at_stop( … feed, … route_id=”BART-01”, … direction_id=0, … stop_id=12345, # integer model_node_id … truncate=”after”, … )
Source code in network_wrangler/transit/filter.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | |
Utilities for working with transit geodataframes.
network_wrangler.transit.geo.shapes_to_shape_links_gdf ¶
shapes_to_shape_links_gdf(
shapes,
ref_nodes_df=None,
from_field="A",
to_field="B",
crs=LAT_LON_CRS,
)
Translates shapes to shape links geodataframe using geometry from ref_nodes_df if provided.
TODO: Add join to links and then shapes to get true geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
Feed shapes table |
required |
ref_nodes_df
|
DataFrame[RoadNodesTable] | None
|
If specified, will use geometry from these nodes. Otherwise, will use geometry in shapes file. Defaults to None. |
None
|
from_field
|
str
|
Field used for the link’s from node |
'A'
|
to_field
|
str
|
Field used for the link’s to node |
'B'
|
crs
|
int
|
Coordinate reference system. SHouldn’t be changed unless you know what you are doing. Defaults to LAT_LON_CRS which is WGS84 lat/long. |
LAT_LON_CRS
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
gpd.GeoDataFrame: description |
Source code in network_wrangler/transit/geo.py
network_wrangler.transit.geo.shapes_to_trip_shapes_gdf ¶
Geodataframe with one polyline shape per shape_id.
TODO: add information about the route and trips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
DataFrame[WranglerShapesTable]
|
WranglerShapesTable |
required |
trips
|
WranglerTripsTable |
required | |
ref_nodes_df
|
DataFrame[RoadNodesTable] | None
|
If specified, will use geometry from these nodes. Otherwise, will use geometry in shapes file. Defaults to None. |
None
|
crs
|
int
|
int, optional, default 4326 |
LAT_LON_CRS
|
Source code in network_wrangler/transit/geo.py
network_wrangler.transit.geo.stop_times_to_stop_time_links_gdf ¶
stop_times_to_stop_time_links_gdf(
stop_times,
stops,
ref_nodes_df=None,
from_field="A",
to_field="B",
)
Stop times geodataframe as links using geometry from stops.txt or optionally another df.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
WranglerStopTimesTable
|
Feed stop times table. |
required |
stops
|
WranglerStopsTable
|
Feed stops table. |
required |
ref_nodes_df
|
DataFrame
|
If specified, will use geometry from these nodes. Otherwise, will use geometry in shapes file. Defaults to None. |
None
|
from_field
|
str
|
Field used for the link’s from node |
'A'
|
to_field
|
str
|
Field used for the link’s to node |
'B'
|
Source code in network_wrangler/transit/geo.py
network_wrangler.transit.geo.stop_times_to_stop_time_points_gdf ¶
Stoptimes geodataframe as points using geometry from stops.txt or optionally another df.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_times
|
WranglerStopTimesTable
|
Feed stop times table. |
required |
stops
|
WranglerStopsTable
|
Feed stops table. |
required |
ref_nodes_df
|
DataFrame
|
If specified, will use geometry from these nodes. Otherwise, will use geometry in shapes file. Defaults to None. |
None
|
Source code in network_wrangler/transit/geo.py
network_wrangler.transit.geo.update_shapes_geometry ¶
Returns shapes table with geometry updated from ref_nodes_df.
NOTE: does not update “geometry” field if it exists.
Source code in network_wrangler/transit/geo.py
network_wrangler.transit.geo.update_stops_geometry ¶
Returns stops table with geometry updated from ref_nodes_df.
NOTE: does not update “geometry” field if it exists.
Source code in network_wrangler/transit/geo.py
Functions for reading and writing transit feeds and networks.
network_wrangler.transit.io.convert_transit_serialization ¶
convert_transit_serialization(
input_path,
output_format,
out_dir=".",
input_file_format="csv",
out_prefix="",
overwrite=True,
)
Converts a transit network from one serialization to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str | Path
|
path to the input network |
required |
output_format
|
TransitFileTypes
|
the format of the output files. Should be txt, csv, or parquet. |
required |
out_dir
|
Path | str
|
directory to write the network to. Defaults to current directory. |
'.'
|
input_file_format
|
TransitFileTypes
|
the file_format of the files to read. Should be txt, csv, or parquet. Defaults to “txt” |
'csv'
|
out_prefix
|
str
|
prefix to add to the file name. Defaults to “” |
''
|
overwrite
|
bool
|
if True, will overwrite the files if they already exist. Defaults to True |
True
|
Source code in network_wrangler/transit/io.py
network_wrangler.transit.io.load_feed_from_dfs ¶
Create a Feed or GtfsModel object from a dictionary of DataFrames representing a GTFS feed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_dfs
|
dict
|
A dictionary containing DataFrames representing the tables of a GTFS feed. |
required |
wrangler_flavored
|
bool
|
If True, creates a Wrangler-enhanced Feed] object. If False, creates a pure GtfsModel object. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Feed | GtfsModel
|
Union[Feed, GtfsModel]: A Feed or GtfsModel object representing the transit network. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the feed_dfs dictionary does not contain all the required tables. |
Example usage:
feed_dfs = {
"agency": agency_df,
"routes": routes_df,
"stops": stops_df,
"trips": trips_df,
"stop_times": stop_times_df,
}
feed = load_feed_from_dfs(feed_dfs) # Creates Feed by default
gtfs_model = load_feed_from_dfs(feed_dfs, wrangler_flavored=False) # Creates GtfsModel
Source code in network_wrangler/transit/io.py
network_wrangler.transit.io.load_feed_from_path ¶
load_feed_from_path(
feed_path,
file_format="txt",
wrangler_flavored=True,
service_ids_filter=None,
**read_kwargs,
)
Create a Feed or GtfsModel object from the path to a GTFS transit feed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed_path
|
Union[Path, str]
|
The path to the GTFS transit feed. |
required |
file_format
|
TransitFileTypes
|
the format of the files to read. Defaults to “txt” |
'txt'
|
wrangler_flavored
|
bool
|
If True, creates a Wrangler-enhanced Feed object. If False, creates a pure GtfsModel object. Defaults to True. |
True
|
service_ids_filter
|
Optional[list[str]]
|
If not None, filter to these service_ids. Assumes service_id is a str. |
None
|
**read_kwargs
|
Additional keyword arguments to pass to the file reader (e.g., low_memory, dtype) |
{}
|
Returns:
| Type | Description |
|---|---|
Feed | GtfsModel
|
Union[Feed, GtfsModel]: The Feed or GtfsModel object created from the GTFS transit feed. |
Source code in network_wrangler/transit/io.py
network_wrangler.transit.io.load_transit ¶
Create a TransitNetwork object.
This function takes in a feed parameter, which can be one of the following types:
Feed: A Feed object representing a transit feed.dict[str, pd.DataFrame]: A dictionary of DataFrames representing transit data.strorPath: A string or a Path object representing the path to a transit feed file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed | GtfsModel | dict[str, DataFrame] | str | Path
|
Feed boject, dict of transit data frames, or path to transit feed data |
required |
file_format
|
TransitFileTypes
|
the format of the files to read. Defaults to “txt” |
'txt'
|
config
|
WranglerConfig
|
WranglerConfig object. Defaults to DefaultConfig. |
DefaultConfig
|
Returns:
| Type | Description |
|---|---|
TransitNetwork
|
object representing the loaded transit network. |
Example usage:
transit_network_from_zip = load_transit("path/to/gtfs.zip")
transit_network_from_unzipped_dir = load_transit("path/to/files")
transit_network_from_parquet = load_transit("path/to/files", file_format="parquet")
dfs_of_transit_data = {"routes": routes_df, "stops": stops_df, "trips": trips_df...}
transit_network_from_dfs = load_transit(dfs_of_transit_data)
Source code in network_wrangler/transit/io.py
network_wrangler.transit.io.write_feed_geo ¶
write_feed_geo(
feed,
ref_nodes_df,
out_dir,
file_format="geojson",
out_prefix=None,
overwrite=True,
)
Write a Feed object to a directory in a geospatial format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
Feed object to write |
required |
ref_nodes_df
|
GeoDataFrame
|
Reference nodes dataframe to use for geometry |
required |
out_dir
|
str | Path
|
directory to write the network to |
required |
file_format
|
Literal['geojson', 'shp', 'parquet']
|
the format of the output files. Defaults to “geojson” |
'geojson'
|
out_prefix
|
prefix to add to the file name |
None
|
|
overwrite
|
bool
|
if True, will overwrite the files if they already exist. Defaults to True |
True
|
Source code in network_wrangler/transit/io.py
network_wrangler.transit.io.write_transit ¶
Writes a transit network, feed, or GTFS model to files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transit_obj
|
TransitNetwork | Feed | GtfsModel
|
a TransitNetwork, Feed, or GtfsModel instance |
required |
out_dir
|
Path | str
|
directory to write the network to |
'.'
|
file_format
|
Literal['txt', 'csv', 'parquet']
|
the format of the output files. Defaults to “txt” which is csv with txt file format. |
'txt'
|
prefix
|
Path | str | None
|
prefix to add to the file name |
None
|
overwrite
|
bool
|
if True, will overwrite the files if they already exist. Defaults to True |
True
|
Source code in network_wrangler/transit/io.py
ModelTransit class and functions for managing consistency between roadway and transit networks.
NOTE: this is not thoroughly tested and may not be fully functional.
network_wrangler.transit.model_transit.ModelTransit ¶
ModelTransit class for managing consistency between roadway and transit networks.
Source code in network_wrangler/transit/model_transit.py
network_wrangler.transit.model_transit.ModelTransit.consistent_nets
property
¶
Indicate if roadway and transit networks have changed since self.m_feed updated.
network_wrangler.transit.model_transit.ModelTransit.m_feed
property
¶
TransitNetwork.feed with updates for consistency with associated ModelRoadwayNetwork.
network_wrangler.transit.model_transit.ModelTransit.model_roadway_net
property
¶
ModelRoadwayNetwork associated with this ModelTransit.
network_wrangler.transit.model_transit.ModelTransit.__init__ ¶
ModelTransit class for managing consistency between roadway and transit networks.
Source code in network_wrangler/transit/model_transit.py
Classes and functions for selecting transit trips from a transit network.
Usage:
Create a TransitSelection object by providing a TransitNetwork object and a selection dictionary:
1 2 3 4 5 6 7 8 9 10 | |
Access the selected trip ids or dataframe as follows:
1 2 3 4 | |
Note: The selection dictionary should conform to the SelectTransitTrips model defined in the models.projects.transit_selection module.
network_wrangler.transit.selection.TransitSelection ¶
Object to perform and store information about a selection from a project card “facility”.
Attributes:
| Name | Type | Description |
|---|---|---|
selection_dict |
|
|
selected_trips |
list
|
|
selected_trips_df |
DataFrame[WranglerTripsTable]
|
pd.DataFrame: DataFrame of selected trips |
sel_key |
|
|
net |
|
Source code in network_wrangler/transit/selection.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
network_wrangler.transit.selection.TransitSelection.selected_frequencies_df
property
¶
DataFrame of selected frequencies.
network_wrangler.transit.selection.TransitSelection.selected_shapes_df
property
¶
network_wrangler.transit.selection.TransitSelection.selected_trips
property
¶
List of selected trip_ids.
network_wrangler.transit.selection.TransitSelection.selected_trips_df
property
¶
Lazily evaluates selection for trips or returns stored value in self._selected_trips_df.
Will re-evaluate if the current feed modification version is different than the stored one from the last selection.
Returns:
| Type | Description |
|---|---|
DataFrame[WranglerTripsTable]
|
DataFrame[WranglerTripsTable] of selected trips |
network_wrangler.transit.selection.TransitSelection.selection_dict
property
writable
¶
Getter for selection_dict.
network_wrangler.transit.selection.TransitSelection.__init__ ¶
Constructor for TransitSelection object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
TransitNetwork
|
Transit network object to select from. |
required |
selection_dict
|
dict | SelectTransitTrips
|
Selection dictionary conforming to SelectTransitTrips |
required |
Source code in network_wrangler/transit/selection.py
network_wrangler.transit.selection.TransitSelection.__nonzero__ ¶
network_wrangler.transit.selection.TransitSelection.validate_selection_dict ¶
Check that selection dictionary has valid and used properties consistent with network.
Checks that selection_dict is a valid TransitSelectionDict
- query vars exist in respective Feed tables
Raises:
| Type | Description |
|---|---|
TransitSelectionNetworkConsistencyError
|
If not consistent with transit network |
ValidationError
|
if format not consistent with SelectTransitTrips |
Source code in network_wrangler/transit/selection.py
Functions to check for transit network validity and consistency with roadway network.
network_wrangler.transit.validate.shape_links_without_road_links ¶
Validate that links in transit shapes exist in referenced roadway links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tr_shapes
|
DataFrame[WranglerShapesTable]
|
transit shapes from shapes.txt to validate foreign key to. |
required |
rd_links_df
|
DataFrame[RoadLinksTable]
|
Links dataframe from roadway network to validate |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
df with shape_id and A, B, as well as whatever other columns were in tr_shapes |
Source code in network_wrangler/transit/validate.py
network_wrangler.transit.validate.stop_times_without_road_links ¶
Validate that links in transit shapes exist in referenced roadway links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tr_stop_times
|
DataFrame[WranglerStopTimesTable]
|
transit stop_times from stop_times.txt to validate foreign key to. |
required |
rd_links_df
|
DataFrame[RoadLinksTable]
|
Links dataframe from roadway network to validate |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
df with shape_id and A, B |
Source code in network_wrangler/transit/validate.py
network_wrangler.transit.validate.transit_nodes_without_road_nodes ¶
Validate all of a transit feeds node foreign keys exist in referenced roadway nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
Transit Feed to query. |
required |
nodes_df
|
DataFrame
|
Nodes dataframe from roadway network to validate foreign key to. Defaults to self.roadway_net.nodes_df |
required |
rd_field
|
str
|
field in roadway nodes to check against. Defaults to “model_node_id” |
'model_node_id'
|
Returns:
| Type | Description |
|---|---|
list[int]
|
boolean indicating if relationships are all valid |
Source code in network_wrangler/transit/validate.py
network_wrangler.transit.validate.transit_road_net_consistency ¶
Checks foreign key and network link relationships between transit feed and a road_net.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
Feed
|
Transit Feed. |
required |
road_net
|
RoadwayNetwork
|
Roadway network to check relationship with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
boolean indicating if road_net is consistent with transit network. |
Source code in network_wrangler/transit/validate.py
network_wrangler.transit.validate.validate_transit_in_dir ¶
Validates a roadway network in a directory to the wrangler data model specifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir
|
Path
|
The transit network file directory. |
required |
file_format
|
str
|
The format of roadway network file name. Defaults to “txt”. |
'txt'
|
road_dir
|
Path
|
The roadway network file directory. Defaults to None. |
None
|
road_file_format
|
str
|
The format of roadway network file name. Defaults to “geojson”. |
'geojson'
|
output_dir
|
str
|
The output directory for the validation report. Defaults to “.”. |
required |