GTFS Loader Example¶
In [1]:
Copied!
from pathlib import Path
import geopandas as gpd
import gtfs_kit as gk
from shapely.geometry import Point
from srai.constants import WGS84_CRS
from srai.loaders import GTFSLoader, download_file
from pathlib import Path
import geopandas as gpd
import gtfs_kit as gk
from shapely.geometry import Point
from srai.constants import WGS84_CRS
from srai.loaders import GTFSLoader, download_file
Download an example GTFS feed from Wroclaw, Poland¶
In this notebook we use the GTFS feed for Wroclaw, Poland as an example, which is available in Wroclaw's open data repository[1]. This download uses transitfeeds.com[2] to download the feed, but you can also download the feed directly from the Wroclaw open data repository.
In [2]:
Copied!
wroclaw_gtfs = Path().resolve() / "files" / "example.zip"
gtfs_url = "https://transitfeeds.com/p/mpk-wroc-aw/663/20221221/download"
download_file(gtfs_url, wroclaw_gtfs.as_posix())
wroclaw_gtfs = Path().resolve() / "files" / "example.zip"
gtfs_url = "https://transitfeeds.com/p/mpk-wroc-aw/663/20221221/download"
download_file(gtfs_url, wroclaw_gtfs.as_posix())
Peek at the feed using gtfs_kit
directly¶
In [3]:
Copied!
feed = gk.read_feed(wroclaw_gtfs, dist_units="km")
stops_df = feed.stops[["stop_id", "stop_lat", "stop_lon"]].set_index("stop_id")
stops_df["geometry"] = stops_df.apply(lambda row: Point(row["stop_lon"], row["stop_lat"]), axis=1)
stops_gdf = gpd.GeoDataFrame(
stops_df,
geometry="geometry",
crs=WGS84_CRS,
)
stops_gdf.explore(tiles="CartoDB positron")
feed = gk.read_feed(wroclaw_gtfs, dist_units="km")
stops_df = feed.stops[["stop_id", "stop_lat", "stop_lon"]].set_index("stop_id")
stops_df["geometry"] = stops_df.apply(lambda row: Point(row["stop_lon"], row["stop_lat"]), axis=1)
stops_gdf = gpd.GeoDataFrame(
stops_df,
geometry="geometry",
crs=WGS84_CRS,
)
stops_gdf.explore(tiles="CartoDB positron")
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Use GTFSLoader to load stops statistics from the feed¶
In [4]:
Copied!
gtfs_loader = GTFSLoader()
trips_gdf = gtfs_loader.load(wroclaw_gtfs)
print(trips_gdf.columns)
gtfs_loader = GTFSLoader()
trips_gdf = gtfs_loader.load(wroclaw_gtfs)
print(trips_gdf.columns)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/gtfs_kit/stops.py:427: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. rng = pd.date_range(start, end, freq=freq)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:401: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. if f.empty or pd.tseries.frequencies.to_offset( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:409: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. result = f.resample(freq).sum(min_count=1) /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:452: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. result.index.freq = freq /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/gtfs_kit/stops.py:544: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. f.index.freq = pd.tseries.frequencies.to_offset(freq)
Index(['trips_at_0', 'trips_at_1', 'trips_at_2', 'trips_at_3', 'trips_at_4', 'trips_at_5', 'trips_at_6', 'trips_at_7', 'trips_at_8', 'trips_at_9', 'trips_at_10', 'trips_at_11', 'trips_at_12', 'trips_at_13', 'trips_at_14', 'trips_at_15', 'trips_at_16', 'trips_at_17', 'trips_at_18', 'trips_at_19', 'trips_at_20', 'trips_at_21', 'trips_at_22', 'trips_at_23', 'geometry', 'directions_at_0', 'directions_at_1', 'directions_at_2', 'directions_at_3', 'directions_at_4', 'directions_at_5', 'directions_at_6', 'directions_at_7', 'directions_at_8', 'directions_at_9', 'directions_at_10', 'directions_at_11', 'directions_at_12', 'directions_at_13', 'directions_at_14', 'directions_at_15', 'directions_at_16', 'directions_at_17', 'directions_at_18', 'directions_at_19', 'directions_at_20', 'directions_at_21', 'directions_at_22', 'directions_at_23'], dtype='object')