Functions realted to GTFS Wrapper

  • Due to the irregularities in the GTFS set, several preprocessing filters were applied. E.g.
    • Route Ids are integer and start from 1000.

    • Trip Ids string of format a_b, where a is route Id and b is the sequence number of the trip when arranged in ascending order (according to departure time from the first stop).

    • Stop Ids are integer and start from 1.

    • Stop sequence in stoptimes.txt file is made continuous.

    • Overlapping trips along a route are removed

    • Disjoint routes (i.e., routes which cannot be reached from any other route in the network) are removed. These generally include waterways, airways.

    • The timetable provided is for a day. All the timestamps are converted into pandas.datetime format.

    • calendar_dates can be used in two sense. In the first case, it acts as a supplement to calendar.txt by defining listing the service id removed or added on a particular day (recommended usage).In the second case, it acts independently by listing all the service active on the particular day. See GTFS reference for more details. Currently only first type of functionality is supported.

For implementation details, see GTFS_wrapper.py.

Description

Apply necessary filters to GTFS set. Note that this file is GTFS-specific.

GTFS_wrapper.check_trip_len(stop_times)None[source]

Ensures that number of stops in all trips should be >2.

Parameters

stop_times – GTFS stoptimes.txt file

Returns

None

GTFS_wrapper.filter_stopsfile(stops_map, stops)[source]

Apply filter to stops file

Parameters
  • stops_map – stop id mapping

  • stops – GTFS stops.txt file

Returns

Filtered stops file

GTFS_wrapper.filter_stoptimes(valid_trips: set, trips, DATE_TOFILTER_ON: int, stop_times)tuple[source]

Filter stoptimes file

Parameters
  • valid_trips (set) – GTFS set containing trips

  • trips – GTFS trips.txt file

  • DATE_TOFILTER_ON (int) – date on which GTFS set is filtered

  • stop_times – GTFS stoptimes.txt file

Returns

Filtered stops mapping and stoptimes file

GTFS_wrapper.filter_trips(trips, stop_times, stops)[source]

Filter trips file. Trip Id are renamed as a_b where a is the route id and b is the sequence of trip (arranged according to departure time)

Parameters
  • trips – GTFS trips.txt file

  • stop_times – GTFS stoptimes.txt file

  • stops – GTFS stops.txt file

Returns

Filtered trips, stoptimes and stops file

GTFS_wrapper.filter_trips_routes_ondates(valid_routes_set: set, calendar_dates, calendar, trips, DATE_TOFILTER_ON: int)tuple[source]

Filter the trips file based on calendar. Only One-days data is assumed here.

Parameters
  • valid_routes_set (set) – set containing valid route ids

  • calendar_dates – GTFS Calendar_dates.txt file

  • calendar – GTFS calendar.txt file

  • trips – GTFS trips.txt file

  • DATE_TOFILTER_ON (int) – date on which GTFS set is filtered

Returns

Filtered trips file and a set of valid trips and routes.

Note

calendar_dates can be used in two sense. In the first case, it acts as a supplement to calendar.txt by defining listing the service id removed or added on a particular day (recommended usage).In the second case, it acts independently by listing all the service active on the particular day. See GTFS reference for more details.

GTFS_wrapper.main()None[source]

Main function

Returns

None

#TODO: Call build_transfer_file if the parameter is 1

GTFS_wrapper.read_gtfs(READ_PATH: str, NETWORK_NAME: str)[source]

Reads the GTFS set

Parameters
  • READ_PATH (str) – Path to read GTFS

  • NETWORK_NAME (str) – Network name

Returns

GTFS files

Examples

>>> calendar_dates, route, trips, stop_times, stops, calendar, transfer = read_gtfs(READ_PATH, 'anaheim')
GTFS_wrapper.remove_overlapping_trips(stop_times, trips)[source]

Remove overlapping trips, i.e., all trips should follow first-in-first-out (FIFO) property.

Parameters

stop_times – GTFS stoptimes.txt file

Returns

Filtered stoptimes file

GTFS_wrapper.remove_unwanted_route(VALID_ROUTE_TYPES: list, route)tuple[source]

Remove unwanted routes like sea ferries, Metro

Parameters
  • VALID_ROUTE_TYPES (list) –

  • route – GTFS routes.txt file

Returns

Filters route file and a set containing all routes ids.

Examples

>>> valid_routes_set, route = remove_unwanted_route([3], route)
GTFS_wrapper.rename_route(stop_times, trips)tuple[source]

Rename the route Id to integer. Route Id are assumed to start from 1000.

Parameters
  • stop_times – GTFS stoptimes.txt file

  • trips – GTFS trips.txt file

Returns

Route Id mapping, filtered stoptimes and trip file

GTFS_wrapper.rename_trips(stop_times, trips)[source]

Rename trips

Parameters
  • stop_times – GTFS stoptimes.txt file

  • trips – GTFS trips.txt file

Returns

Filtered stoptimes.txt and trips.txt file

GTFS_wrapper.save_final(SAVE_PATH: str, trips, stop_times, stops)None[source]

Save the final GTFS set and print statistics

Parameters
  • SAVE_PATH (str) – Path to save GTFS

  • trips – GTFS trips.txt file

  • stop_times – GTFS stop_times.txt file

  • stops – GTFS stops.txt file

Returns

None

GTFS_wrapper.stoptimes_filter(stop_times)[source]
Apply filters to stoptimes file. Following filters are applied:
  1. Remove singleton routes, i.e., route of length 1. These can come due to various filters applied before.

  2. Drop routes circling back to same stop

  3. Rename stop_sequence for every trip starting from 0

Parameters

stop_times – GTFS stoptimes.txt file

Returns

Filtered stoptimes.txt GTFS file

GTFS_wrapper.take_inputs()tuple[source]

Takes the required inputs for building GTFS wrapper

Returns

NETWORK_NAME, DATE_TOFILTER_ON, VALID_ROUTE_TYPES, BUILD_TRANSFER, breaker, READ_PATH, SAVE_PATH

Examples

>>> NETWORK_NAME, DATE_TOFILTER_ON, VALID_ROUTE_TYPES, BUILD_TRANSFER, breaker, READ_PATH, SAVE_PATH = take_inputs()