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: 0%|▏ | 21.0k/13.7M [00:00<01:40, 142kiB/s]
example.zip: 0%|▌ | 55.0k/13.7M [00:00<01:18, 182kiB/s]
example.zip: 1%|█▍ | 130k/13.7M [00:00<00:46, 308kiB/s]
example.zip: 2%|███▎ | 287k/13.7M [00:00<00:24, 568kiB/s]
example.zip: 4%|██████▉ | 614k/13.7M [00:00<00:12, 1.09MiB/s]
example.zip: 8%|████████████▊ | 1.12M/13.7M [00:00<00:07, 1.85MiB/s]
example.zip: 17%|██████████████████████████▏ | 2.28M/13.7M [00:01<00:03, 3.63MiB/s]
example.zip: 34%|████████████████████████████████████████████████████▋ | 4.60M/13.7M [00:01<00:01, 7.13MiB/s]
example.zip: 46%|███████████████████████████████████████████████████████████████████████▌ | 6.25M/13.7M [00:01<00:00, 8.18MiB/s]
example.zip: 65%|██████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 8.92M/13.7M [00:01<00:00, 10.6MiB/s]
example.zip: 87%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 11.9M/13.7M [00:01<00:00, 12.9MiB/s]
example.zip: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 13.7M/13.7M [00:01<00:00, 7.54MiB/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)
/root/development/srai/.venv/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)
/root/development/srai/.venv/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( /root/development/srai/.venv/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) /root/development/srai/.venv/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 /root/development/srai/.venv/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 GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC GŁÓWNY, Żórawina - Niepodl... | {Wojszycka, DWORZEC AUTOBUSOWY, DWORZEC GŁÓWNY... | {Wojszycka, DWORZEC AUTOBUSOWY, Żórawina - Nie... | {Wojszycka, JARNOŁTÓW, KRZYKI, GAJ - pętla} |
1004 | 4.0 | 3.0 | 3.0 | 3.0 | 11.0 | 18.0 | 21.0 | 12.0 | 9.0 | 6.0 | ... | {GAJ - pętla, PRACZE ODRZAŃSKIE, Janówek, Bisk... | {KUŹNIKI, PRACZE ODRZAŃSKIE, OPORÓW, GAJ - pętla} | {KUŹNIKI, OPORÓW, GAJ - pętla} | {KUŹNIKI, OPORÓW, GAJ - pętla} | {KUŹNIKI, OPORÓW, GAJ - pętla} | {KUŹNIKI, RĘDZIŃSKA, OPORÓW, GAJ - pętla} | {KUŹNIKI, OPORÓW, GAJ - pętla} | {GAJ - pętla, Janówek, Biskupice Podg. LG Ener... | {KRZYKI, GAJ - pętla, PRACZE ODRZAŃSKIE, LEŚNI... | {KUŹNIKI, OPORÓW, 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, Kwiska, PL. ... | {Zajezdnia Obornicka, Metalowców, Kwiska, PL. ... | {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