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())
example.zip: 0%| | 0.00/13.7M [00:00<?, ?iB/s]
example.zip: 54%|█████▎ | 7.36M/13.7M [00:00<00:00, 77.2MiB/s]
example.zip: 100%|██████████| 13.7M/13.7M [00:00<00:00, 83.8MiB/s]
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.14/x64/lib/python3.10/site-packages/gtfs_kit/stops.py:426: 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.14/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:400: 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.14/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:408: 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.14/x64/lib/python3.10/site-packages/gtfs_kit/helpers.py:451: 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.14/x64/lib/python3.10/site-packages/gtfs_kit/stops.py:543: 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')
In [5]:
Copied!
trips_gdf
trips_gdf
Out[5]:
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 | ... | 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 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
feature_id | |||||||||||||||||||||
100 | 0.0 | 0.0 | 0.0 | 0.0 | 2.0 | 3.0 | 4.0 | 3.0 | 3.0 | 3.0 | ... | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} | {LEŚNICA} |
1000 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 5.0 | 7.0 | 6.0 | 9.0 | 9.0 | ... | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | {KRZYKI, DWORZEC GŁÓWNY} | NaN |
1002 | 2.0 | 2.0 | 2.0 | 1.0 | 6.0 | 7.0 | 9.0 | 6.0 | 8.0 | 5.0 | ... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Wojszycka, DWORZEC AUTOBUSOWY, Zabrodzie - pę... | {Zabrodzie - pętla, Wojszycka, DWORZEC GŁÓWNY,... | {Wojszycka, Żórawina - Niepodległości (Mostek)... | {Wojszycka, KRZYKI, JARNOŁTÓW, GAJ - pętla} |
1004 | 4.0 | 3.0 | 3.0 | 3.0 | 11.0 | 18.0 | 21.0 | 12.0 | 9.0 | 6.0 | ... | {OPORÓW, KUŹNIKI, GAJ - pętla, Biskupice Podg.... | {OPORÓW, KUŹNIKI, GAJ - pętla, PRACZE ODRZAŃSKIE} | {OPORÓW, KUŹNIKI, GAJ - pętla} | {OPORÓW, KUŹNIKI, GAJ - pętla} | {OPORÓW, KUŹNIKI, GAJ - pętla} | {OPORÓW, KUŹNIKI, GAJ - pętla, RĘDZIŃSKA} | {OPORÓW, KUŹNIKI, GAJ - pętla} | {OPORÓW, KUŹNIKI, GAJ - pętla, Dworzec Główny ... | {OPORÓW, KUŹNIKI, GAJ - pętla, LEŚNICA, KRZYKI... | {OPORÓW, KUŹNIKI, GAJ - pętla} |
101 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 2.0 | 4.0 | 4.0 | 3.0 | 3.0 | ... | {Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Zajezdnia Obornicka, Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Zajezdnia Obornicka, Kwiska} |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
993 | 2.0 | 1.0 | 1.0 | 1.0 | 4.0 | 8.0 | 10.0 | 11.0 | 10.0 | 8.0 | ... | {Metalowców, PL. JANA PAWŁA II} | {Metalowców, PL. JANA PAWŁA II} | {Metalowców, PL. JANA PAWŁA II} | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, Metalowców, PL. JANA PAW... | {Zajezdnia Obornicka, DWORZEC NADODRZE} |
994 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 2.0 | 4.0 | 4.0 | 3.0 | 3.0 | ... | {Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Zajezdnia Obornicka, Kwiska} | {Kwiska} | {Kwiska} | {Kwiska} | {Zajezdnia Obornicka, Kwiska} |
997 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 5.0 | 6.0 | 7.0 | 6.0 | 5.0 | ... | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} | {GALERIA DOMINIKAŃSKA} |
998 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 2.0 | 1.0 | 2.0 | 0.0 | ... | {KLECINA (Stacja kolejowa)} | {KLECINA (Stacja kolejowa)} | {KLECINA (Stacja kolejowa)} | {Zajezdnia Obornicka, KLECINA (Stacja kolejowa)} | {Zajezdnia Obornicka} | NaN | NaN | NaN | NaN | NaN |
999 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | {KSIĘŻE WIELKIE} |
2270 rows × 49 columns