gtfs_utils

gtfs_utils

Utility functions for GTFS archives.

Functions

Name Description
bbox_filter_gtfs Filter a GTFS feed to any routes intersecting with a bounding box.
convert_pandas_to_plotly Convert a pandas dataframe to a visual plotly figure.
filter_gtfs Filter the GTFS to either a bbox or a date.
filter_gtfs_around_trip Filter a GTFS file to an area around a given trip in the GTFS.

bbox_filter_gtfs

gtfs_utils.bbox_filter_gtfs(in_pth=(os.path.join(PKG_PATH, 'data', 'newport-20230613_gtfs.zip')), out_pth=pathlib.Path(here('data/external/filtered_gtfs.zip')), bbox=[-3.077081, 51.52222, -2.925075, 51.593596], units='km', crs='epsg:4326', filter_dates=[])

Filter a GTFS feed to any routes intersecting with a bounding box.

Optionally filter to a list of given dates.

WARNING: THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED IN A FUTURE VERSION. USE filter_gtfs() INSTEAD.

Parameters

Name Type Description Default
in_pth Union[pathlib.Path, str] Path to the unfiltered GTFS feed. Defaults to os.path.join(PKG_PATH, “data”, “newport-20230613_gtfs.zip”). (os.path.join(PKG_PATH, 'data', 'newport-20230613_gtfs.zip'))
out_pth Union[pathlib.Path, str] Path to write the filtered feed to. Defaults to here(“data/external/filtered_gtfs.zip”). pathlib.Path(here('data/external/filtered_gtfs.zip'))
bbox Union[gpd.GeoDataFrame, list(float)] A list of x and y values in the order of minx, miny, maxx, maxy. Defaults to [-3.077081, 51.52222, -2.925075, 51.593596]. [-3.077081, 51.52222, -2.925075, 51.593596]
units str Distance units of the original GTFS. Defaults to “km”. 'km'
crs str What projection should the bbox_list be interpreted as. Defaults to “epsg:4326” for lat long. 'epsg:4326'
filter_dates list A list of dates to restrict the feed to. Not providing filter_dates means that date filtering will not be applied. Defaults to []. []

Returns

Type Description
None

Raises

Type Description
TypeError bbox is not of type list or gpd.GeoDataFrame. units or crs are not of type str. out_pth or in_pth are not of types str or pathlib.Path. Elements of a bbox list are not of type float.
FileExistsError in_pth does not exist on disk.
ValueError in_pth or out_pth does not have the expected .zip extension.

convert_pandas_to_plotly

gtfs_utils.convert_pandas_to_plotly(df, return_html=False)

Convert a pandas dataframe to a visual plotly figure.

Parameters

Name Type Description Default
df pd.DataFrame A pandas dataframe to convert to plotly (single index only) required
return_html bool Whether or not to return the html element, by default False False

Returns

Type Description
go.Figure A plotly figure containing the drawn dataframe

Raises

Type Description
LookupError An error raised if an invalid colour scheme is passed
TypeError An error raised if the given pandas dataframe is MultiIndex

filter_gtfs

gtfs_utils.filter_gtfs(gtfs, bbox=None, crs='epsg:4326', filter_dates=[])

Filter the GTFS to either a bbox or a date.

Parameters

Name Type Description Default
gtfs GtfsInstance The GTFS to filter required
bbox Union[GeoDataFrame, list, None] The bbox to filter the GTFS to. Leave as none if the GTFS does not need to be cropped. Format - [xmin, ymin, xmax, ymax], by default None None
crs Union[str, int] The CRS of the given bbox, by default “epsg:4326” 'epsg:4326'
filter_dates list The dates to filter the GTFS to. Leave as an empty list if you do not require the GTFS to be filtered to a date, by default [] []

Returns

Type Description
None

Raises

Type Description
ValueError Raised if any of the dates passed to ‘filter_dates’ isn’t present in the gtfs

filter_gtfs_around_trip

gtfs_utils.filter_gtfs_around_trip(gtfs, trip_id, buffer_dist=10000, units='m', crs='27700', out_pth=os.path.join('data', 'external', 'trip_gtfs.zip'))

Filter a GTFS file to an area around a given trip in the GTFS.

Parameters

Name Type Description Default
gtfs GtfsInstance The GtfsInstance object to crop required
trip_id str The trip ID required
buffer_dist int The distance to create a buffer around the trip, by default 10000 10000
units str Distance units of the original GTFS, by default “m” 'm'
crs str The CRS to use for adding a buffer, by default “27700” '27700'
out_pth type Where to save the new GTFS file, by default os.path.join(“data”, “external”, “trip_gtfs.zip”) os.path.join('data', 'external', 'trip_gtfs.zip')

Returns

Type Description
None

Raises

Type Description
ValueError An error is raised if a shapeID is not available