Skip to content

Filtering API Reference

GtfsFilter

GtfsFilter

Provides filtering and querying capabilities for GTFS feeds.

This class wraps a GtfsFeed or LazyGtfsFeed and provides convenient methods for filtering data by various criteria.

Attributes:

Name Type Description
feed GtfsFeed | LazyGtfsFeed

The underlying GTFS feed to filter.

feed property

feed: GtfsFeed | LazyGtfsFeed

Return the underlying feed.

__init__

__init__(feed: GtfsFeed | LazyGtfsFeed) -> None

Initialize the filter with a GTFS feed.

Parameters:

Name Type Description Default
feed GtfsFeed | LazyGtfsFeed

A GtfsFeed or LazyGtfsFeed instance to filter.

required

get_stop

get_stop(stop_id: str) -> Stop | None

Get a stop by its ID.

Parameters:

Name Type Description Default
stop_id str

The stop ID to look up.

required

Returns:

Type Description
Stop | None

The Stop object, or None if not found.

get_route

get_route(route_id: str) -> Route | None

Get a route by its ID.

Parameters:

Name Type Description Default
route_id str

The route ID to look up.

required

Returns:

Type Description
Route | None

The Route object, or None if not found.

get_trip

get_trip(trip_id: str) -> Trip | None

Get a trip by its ID.

Parameters:

Name Type Description Default
trip_id str

The trip ID to look up.

required

Returns:

Type Description
Trip | None

The Trip object, or None if not found.

get_agency

get_agency(agency_id: str) -> Agency | None

Get an agency by its ID.

Parameters:

Name Type Description Default
agency_id str

The agency ID to look up.

required

Returns:

Type Description
Agency | None

The Agency object, or None if not found.

get_calendar

get_calendar(service_id: str) -> Calendar | None

Get a calendar entry by service ID.

Parameters:

Name Type Description Default
service_id str

The service ID to look up.

required

Returns:

Type Description
Calendar | None

The Calendar object, or None if not found.

trips_for_route

trips_for_route(route_id: str) -> list[Trip]

Get all trips for a specific route.

Parameters:

Name Type Description Default
route_id str

The route ID to filter by.

required

Returns:

Type Description
list[Trip]

List of Trip objects for the specified route.

stop_times_for_route

stop_times_for_route(route_id: str) -> list[StopTime]

Get all stop times for a specific route.

Parameters:

Name Type Description Default
route_id str

The route ID to filter by.

required

Returns:

Type Description
list[StopTime]

List of StopTime objects for trips on the specified route.

stops_for_route

stops_for_route(route_id: str) -> list[Stop]

Get all unique stops served by a specific route.

Parameters:

Name Type Description Default
route_id str

The route ID to filter by.

required

Returns:

Type Description
list[Stop]

List of unique Stop objects served by the route.

routes_for_agency

routes_for_agency(agency_id: str) -> list[Route]

Get all routes operated by a specific agency.

Parameters:

Name Type Description Default
agency_id str

The agency ID to filter by.

required

Returns:

Type Description
list[Route]

List of Route objects for the specified agency.

trips_for_agency

trips_for_agency(agency_id: str) -> list[Trip]

Get all trips for routes operated by a specific agency.

Parameters:

Name Type Description Default
agency_id str

The agency ID to filter by.

required

Returns:

Type Description
list[Trip]

List of Trip objects for routes operated by the agency.

stop_times_for_trip

stop_times_for_trip(trip_id: str) -> list[StopTime]

Get all stop times for a specific trip, ordered by sequence.

Parameters:

Name Type Description Default
trip_id str

The trip ID to filter by.

required

Returns:

Type Description
list[StopTime]

List of StopTime objects for the trip, sorted by stop_sequence.

stops_for_trip

stops_for_trip(trip_id: str) -> list[Stop]

Get all stops for a specific trip, in sequence order.

Parameters:

Name Type Description Default
trip_id str

The trip ID to filter by.

required

Returns:

Type Description
list[Stop]

List of Stop objects for the trip, in stop sequence order.

stop_times_at_stop

stop_times_at_stop(stop_id: str) -> list[StopTime]

Get all stop times at a specific stop.

Parameters:

Name Type Description Default
stop_id str

The stop ID to filter by.

required

Returns:

Type Description
list[StopTime]

List of StopTime objects at the specified stop.

trips_serving_stop

trips_serving_stop(stop_id: str) -> list[Trip]

Get all trips that serve a specific stop.

Parameters:

Name Type Description Default
stop_id str

The stop ID to filter by.

required

Returns:

Type Description
list[Trip]

List of Trip objects that stop at the specified stop.

routes_serving_stop

routes_serving_stop(stop_id: str) -> list[Route]

Get all routes that serve a specific stop.

Parameters:

Name Type Description Default
stop_id str

The stop ID to filter by.

required

Returns:

Type Description
list[Route]

List of Route objects that serve the specified stop.

trips_for_service

trips_for_service(service_id: str) -> list[Trip]

Get all trips for a specific service.

Parameters:

Name Type Description Default
service_id str

The service ID to filter by.

required

Returns:

Type Description
list[Trip]

List of Trip objects using the specified service.

active_services_on

active_services_on(
    date_input: str | date,
) -> list[Calendar]

Get all services active on a specific date.

Takes into account both the regular calendar and calendar_dates exceptions.

Parameters:

Name Type Description Default
date_input str | date

The date to check (string "YYYY-MM-DD" or date object).

required

Returns:

Type Description
list[Calendar]

List of Calendar objects for services active on the date.

trips_on_date

trips_on_date(date_input: str | date) -> list[Trip]

Get all trips running on a specific date.

Parameters:

Name Type Description Default
date_input str | date

The date to check (string "YYYY-MM-DD" or date object).

required

Returns:

Type Description
list[Trip]

List of Trip objects running on the specified date.

shape_for_trip

shape_for_trip(trip_id: str) -> Shape | None

Get the shape for a specific trip.

Parameters:

Name Type Description Default
trip_id str

The trip ID to get the shape for.

required

Returns:

Type Description
Shape | None

The Shape object for the trip, or None if no shape assigned.

route_stop_count

route_stop_count(route_id: str) -> int

Get the number of unique stops served by a route.

Parameters:

Name Type Description Default
route_id str

The route ID to count stops for.

required

Returns:

Type Description
int

Number of unique stops on the route.

route_trip_count

route_trip_count(route_id: str) -> int

Get the number of trips for a route.

Parameters:

Name Type Description Default
route_id str

The route ID to count trips for.

required

Returns:

Type Description
int

Number of trips on the route.

stop_trip_count

stop_trip_count(stop_id: str) -> int

Get the number of trips serving a stop.

Parameters:

Name Type Description Default
stop_id str

The stop ID to count trips for.

required

Returns:

Type Description
int

Number of trips serving the stop.

Constructor

GtfsFilter(feed: GtfsFeed | LazyGtfsFeed)

Create a filter for the given GTFS feed.

Lookup Methods

get_stop

f.get_stop(stop_id: str) -> Stop | None
Get a stop by its ID.

get_route

f.get_route(route_id: str) -> Route | None
Get a route by its ID.

get_trip

f.get_trip(trip_id: str) -> Trip | None
Get a trip by its ID.

get_agency

f.get_agency(agency_id: str) -> Agency | None
Get an agency by its ID.

get_calendar

f.get_calendar(service_id: str) -> Calendar | None
Get a calendar entry by service ID.

Route Filtering

trips_for_route

f.trips_for_route(route_id: str) -> List[Trip]
Get all trips for a specific route.

stop_times_for_route

f.stop_times_for_route(route_id: str) -> List[StopTime]
Get all stop times for trips on a route.

stops_for_route

f.stops_for_route(route_id: str) -> List[Stop]
Get all unique stops served by a route.

Trip Filtering

stop_times_for_trip

f.stop_times_for_trip(trip_id: str) -> List[StopTime]
Get all stop times for a trip, sorted by stop_sequence.

stops_for_trip

f.stops_for_trip(trip_id: str) -> List[Stop]
Get all stops for a trip, in sequence order.

Stop Filtering

stop_times_at_stop

f.stop_times_at_stop(stop_id: str) -> List[StopTime]
Get all stop times at a specific stop.

trips_serving_stop

f.trips_serving_stop(stop_id: str) -> List[Trip]
Get all trips that serve a stop.

routes_serving_stop

f.routes_serving_stop(stop_id: str) -> List[Route]
Get all routes that serve a stop.

Agency Filtering

routes_for_agency

f.routes_for_agency(agency_id: str) -> List[Route]
Get all routes operated by an agency.

trips_for_agency

f.trips_for_agency(agency_id: str) -> List[Trip]
Get all trips for routes operated by an agency.

Service/Date Filtering

trips_for_service

f.trips_for_service(service_id: str) -> List[Trip]
Get all trips for a specific service.

active_services_on

f.active_services_on(date_input: str | date) -> List[Calendar]
Get all services active on a specific date.

Takes into account both calendar (day-of-week patterns) and calendar_dates (exceptions).

Parameters: - date_input: Date as string ("YYYY-MM-DD" or "YYYYMMDD") or datetime.date object

Raises: - InvalidDateError: If the date string cannot be parsed

trips_on_date

f.trips_on_date(date_input: str | date) -> List[Trip]
Get all trips running on a specific date.

Shape Queries

shape_for_trip

f.shape_for_trip(trip_id: str) -> Shape | None
Get the shape for a trip, if one is assigned.

Statistics

route_stop_count

f.route_stop_count(route_id: str) -> int
Get the number of unique stops served by a route.

route_trip_count

f.route_trip_count(route_id: str) -> int
Get the number of trips for a route.

stop_trip_count

f.stop_trip_count(stop_id: str) -> int
Get the number of trips serving a stop.