Note
Go to the end to download the full example code.
Import GTFS#
In this example, we import a GTFS feed to our model and perform map matching.
We use data from Coquimbo, a city in La Serena Metropolitan Area in Chile.
See also
Several functions, methods, classes and modules are used in this example:
# Imports
from uuid import uuid4
from os import remove
from os.path import join
from tempfile import gettempdir
import folium
import pandas as pd
from aequilibrae.project.database_connection import database_connection
from aequilibrae.transit import Transit
from aequilibrae.utils.create_example import create_example
# Let's create an empty project on an arbitrary folder.
fldr = join(gettempdir(), uuid4().hex)
project = create_example(fldr, "coquimbo")
As the Coquimbo example already has a complete GTFS model, we shall remove its public transport database for the sake of this example.
remove(join(fldr, "public_transport.sqlite"))
Let’s import the GTFS feed.
dest_path = join(fldr, "gtfs_coquimbo.zip")
Now we create our Transit object and import the GTFS feed into our model. This will automatically create a new public transport database.
data = Transit(project)
transit = data.new_gtfs_builder(agency="Lisanco", file_path=dest_path)
To load the data, we must choose one date. We’re going to continue with 2016-04-13 but feel free to experiment with any other available dates. Transit class has a function allowing you to check dates for the GTFS feed. It should take approximately 2 minutes to load the data.
transit.load_date("2016-04-13")
# Now we execute the map matching to find the real paths.
# Depending on the GTFS size, this process can be really time-consuming.
transit.set_allow_map_match(True)
transit.map_match()
# Finally, we save our GTFS into our model.
transit.save_to_disk()
Loading routes (Step: 1/12) : 0it [00:00, ?it/s]
Loading stops (Step: 2/12) : 0%| | 0/78 [00:00<?, ?it/s]
Loading stop times (Step: 3/12) : 0%| | 0/1007 [00:00<?, ?it/s]
Loading stop times (Step: 3/12) : 63%|██████▎ | 637/1007 [00:00<00:00, 4435.68it/s]
Loading shapes (Step: 4/12) : 0%| | 0/2 [00:00<?, ?it/s]
Loading trips (Step: 5/12) : 0%| | 0/1007 [00:00<?, ?it/s]
Loading trips (Step: 5/12) : 4%|▍ | 42/1007 [00:00<00:02, 411.34it/s]
Loading trips (Step: 5/12) : 8%|▊ | 84/1007 [00:00<00:02, 406.17it/s]
Loading trips (Step: 5/12) : 13%|█▎ | 126/1007 [00:00<00:02, 412.27it/s]
Loading trips (Step: 5/12) : 17%|█▋ | 168/1007 [00:00<00:02, 411.01it/s]
Loading trips (Step: 5/12) : 21%|██ | 210/1007 [00:00<00:01, 409.72it/s]
Loading trips (Step: 5/12) : 25%|██▌ | 252/1007 [00:00<00:01, 411.10it/s]
Loading trips (Step: 5/12) : 29%|██▉ | 294/1007 [00:00<00:01, 410.02it/s]
Loading trips (Step: 5/12) : 33%|███▎ | 336/1007 [00:00<00:01, 412.09it/s]
Loading trips (Step: 5/12) : 38%|███▊ | 378/1007 [00:00<00:01, 410.93it/s]
Loading trips (Step: 5/12) : 42%|████▏ | 420/1007 [00:01<00:01, 408.98it/s]
Loading trips (Step: 5/12) : 46%|████▌ | 462/1007 [00:01<00:01, 410.24it/s]
Loading trips (Step: 5/12) : 50%|█████ | 504/1007 [00:01<00:01, 405.31it/s]
Loading trips (Step: 5/12) : 54%|█████▍ | 546/1007 [00:01<00:01, 409.42it/s]
Loading trips (Step: 5/12) : 58%|█████▊ | 588/1007 [00:01<00:01, 412.42it/s]
Loading trips (Step: 5/12) : 63%|██████▎ | 631/1007 [00:01<00:00, 416.41it/s]
Loading trips (Step: 5/12) : 67%|██████▋ | 674/1007 [00:01<00:00, 420.29it/s]
Loading trips (Step: 5/12) : 71%|███████ | 717/1007 [00:01<00:00, 300.82it/s]
Loading trips (Step: 5/12) : 75%|███████▌ | 760/1007 [00:01<00:00, 329.34it/s]
Loading trips (Step: 5/12) : 80%|███████▉ | 803/1007 [00:02<00:00, 352.50it/s]
Loading trips (Step: 5/12) : 84%|████████▍ | 846/1007 [00:02<00:00, 369.06it/s]
Loading trips (Step: 5/12) : 88%|████████▊ | 888/1007 [00:02<00:00, 381.17it/s]
Loading trips (Step: 5/12) : 92%|█████████▏| 931/1007 [00:02<00:00, 392.66it/s]
Loading trips (Step: 5/12) : 97%|█████████▋| 975/1007 [00:02<00:00, 403.78it/s]
De-conflicting stop times (Step: 6/12) : 0%| | 0/1 [00:00<?, ?it/s]
Loading fare data (Step: 7/12) : 100%|██████████| 1/1 [00:00<00:00, 1.64it/s]
Loading data for 2016-04-13 (Step: 9/12) - : 0%| | 0/1 [00:00<?, ?it/s]
Map-matching patterns : 0%| | 0/2 [00:00<?, ?it/s]
Creating map-matching graph from scratch for mode_id=3 (Step: 8/12): 50%|█████ | 1/2 [00:00<00:00, 6.43it/s]
Building graphs - Indexing links (Step: 8/12) : 0it [00:00, ?it/s]
Building graphs - Indexing links (Step: 8/12) : 873it [00:00, 8729.34it/s]
Building graphs - Indexing links (Step: 8/12) : 1746it [00:00, 8679.46it/s]
Building graphs - Indexing links (Step: 8/12) : 2614it [00:00, 8578.46it/s]
Building graphs - Indexing links (Step: 8/12) : 3472it [00:00, 8320.65it/s]
Building graphs - Indexing links (Step: 8/12) : 4306it [00:00, 8072.87it/s]
Building graphs - Indexing links (Step: 8/12) : 5115it [00:00, 7817.42it/s]
Building graphs - Indexing links (Step: 8/12) : 5899it [00:00, 7515.35it/s]
Building graphs - Indexing links (Step: 8/12) : 6653it [00:00, 7263.17it/s]
Building graphs - Indexing links (Step: 8/12) : 7382it [00:00, 7172.57it/s]
Building graphs - Indexing links (Step: 8/12) : 8133it [00:01, 7270.01it/s]
Building graphs - Indexing links (Step: 8/12) : 8898it [00:01, 7380.05it/s]
Building graphs - Indexing links (Step: 8/12) : 9651it [00:01, 7421.97it/s]
Building graphs - Indexing links (Step: 8/12) : 10395it [00:01, 7373.58it/s]
Building graphs - Indexing links (Step: 8/12) : 11134it [00:01, 7226.17it/s]
Building graphs - Indexing links (Step: 8/12) : 11858it [00:01, 7195.96it/s]
Building graphs - Indexing links (Step: 8/12) : 12579it [00:01, 7051.46it/s]
Building graphs - Indexing links (Step: 8/12) : 13285it [00:01, 6964.84it/s]
Building graphs - Indexing links (Step: 8/12) : 14022it [00:01, 7077.69it/s]
Building graphs - Indexing links (Step: 8/12) : 14761it [00:01, 7168.54it/s]
Building graphs - Indexing links (Step: 8/12) : 15479it [00:02, 7151.05it/s]
Building graphs - Indexing links (Step: 8/12) : 16195it [00:02, 7130.12it/s]
Building graphs - Indexing links (Step: 8/12) : 16909it [00:02, 7054.72it/s]
Building graphs - Indexing links (Step: 8/12) : 17615it [00:02, 6972.12it/s]
Building graphs - Indexing links (Step: 8/12) : 18336it [00:02, 7039.05it/s]
Building graphs - Indexing links (Step: 8/12) : 19041it [00:02, 6939.20it/s]
Building graphs - Indexing links (Step: 8/12) : 19765it [00:02, 7017.26it/s]
Building graphs - Indexing links (Step: 8/12) : 20468it [00:02, 6989.88it/s]
Building graphs - Indexing links (Step: 8/12) : 21170it [00:02, 6997.99it/s]
Building graphs - Indexing links (Step: 8/12) : 21929it [00:03, 7171.75it/s]
Building graphs - Indexing links (Step: 8/12) : 22647it [00:03, 7100.94it/s]
Building graphs - Indexing links (Step: 8/12) : 23358it [00:03, 7059.59it/s]
Building graphs - Indexing links (Step: 8/12) : 24084it [00:03, 7107.04it/s]
Building graphs - Indexing links (Step: 8/12) : 24795it [00:03, 7089.86it/s]
Building graphs - Indexing links (Step: 8/12) : 25558it [00:03, 7247.47it/s]
Building graphs - Indexing links (Step: 8/12) : 26283it [00:03, 7232.49it/s]
Building graphs - Indexing links (Step: 8/12) : 27030it [00:03, 7285.79it/s]
Building graphs - Indexing links (Step: 8/12) : 27759it [00:03, 7106.56it/s]
Building graphs - Indexing links (Step: 8/12) : 28471it [00:03, 7108.73it/s]
Building graphs - Indexing links (Step: 8/12) : 29208it [00:04, 7185.52it/s]
Building graphs - Indexing links (Step: 8/12) : 29928it [00:04, 7178.74it/s]
Building graphs - Indexing links (Step: 8/12) : 30647it [00:04, 7080.47it/s]
Building graphs - Indexing links (Step: 8/12) : 31356it [00:04, 7025.26it/s]
Building graphs - Indexing links (Step: 8/12) : 32070it [00:04, 7057.91it/s]
Building graphs - Indexing links (Step: 8/12) : 32783it [00:04, 7078.08it/s]
Building graphs - Indexing links (Step: 8/12) : 33523it [00:04, 7171.99it/s]
Building graphs - Indexing links (Step: 8/12) : 34241it [00:04, 7056.25it/s]
Building graphs - Breaking links (Step: 8/12) : 0%| | 0/78 [00:00<?, ?it/s]
Building graphs - Breaking links (Step: 8/12) : 17%|█▋ | 13/78 [00:00<00:00, 119.32it/s]
Building graphs - Breaking links (Step: 8/12) : 35%|███▍ | 27/78 [00:00<00:00, 128.46it/s]
Building graphs - Breaking links (Step: 8/12) : 51%|█████▏ | 40/78 [00:00<00:00, 122.14it/s]
Building graphs - Breaking links (Step: 8/12) : 68%|██████▊ | 53/78 [00:00<00:00, 123.19it/s]
Building graphs - Breaking links (Step: 8/12) : 85%|████████▍ | 66/78 [00:00<00:00, 104.19it/s]
Building graphs - Breaking links (Step: 8/12) : 99%|█████████▊| 77/78 [00:00<00:00, 96.65it/s]
Map-matching patterns : 3%|▎ | 2/78 [00:11<00:00, 96.65it/s]
Saving patterns (Step: 10/12) : 0%| | 0/2 [00:00<?, ?it/s]
Saving trips (Step: 11/12) : 0%| | 0/360 [00:00<?, ?it/s]
Saving links (Step: 11/12) : 0%| | 0/78 [00:00<?, ?it/s]
Saving stops (Step: 12/12) : 0%| | 0/78 [00:00<?, ?it/s]
Saving stops (Step: 12/12) : 6%|▋ | 5/78 [00:00<00:01, 45.16it/s]
Saving stops (Step: 12/12) : 13%|█▎ | 10/78 [00:00<00:01, 40.64it/s]
Saving stops (Step: 12/12) : 19%|█▉ | 15/78 [00:00<00:01, 37.67it/s]
Saving stops (Step: 12/12) : 24%|██▍ | 19/78 [00:00<00:01, 38.42it/s]
Saving stops (Step: 12/12) : 31%|███ | 24/78 [00:00<00:01, 39.11it/s]
Saving stops (Step: 12/12) : 37%|███▋ | 29/78 [00:00<00:01, 39.75it/s]
Saving stops (Step: 12/12) : 44%|████▎ | 34/78 [00:00<00:01, 39.93it/s]
Saving stops (Step: 12/12) : 50%|█████ | 39/78 [00:00<00:00, 39.92it/s]
Saving stops (Step: 12/12) : 56%|█████▋ | 44/78 [00:01<00:00, 40.23it/s]
Saving stops (Step: 12/12) : 63%|██████▎ | 49/78 [00:01<00:00, 40.14it/s]
Saving stops (Step: 12/12) : 69%|██████▉ | 54/78 [00:01<00:00, 40.10it/s]
Saving stops (Step: 12/12) : 76%|███████▌ | 59/78 [00:01<00:00, 40.35it/s]
Saving stops (Step: 12/12) : 82%|████████▏ | 64/78 [00:01<00:00, 40.17it/s]
Saving stops (Step: 12/12) : 88%|████████▊ | 69/78 [00:01<00:00, 40.14it/s]
Saving stops (Step: 12/12) : 95%|█████████▍| 74/78 [00:01<00:00, 40.35it/s]
Now we will plot one of the route’s patterns we just imported
conn = database_connection("transit")
links = pd.read_sql("SELECT pattern_id, ST_AsText(geometry) geom FROM routes;", con=conn)
stops = pd.read_sql("""SELECT stop_id, ST_X(geometry) X, ST_Y(geometry) Y FROM stops""", con=conn)
gtfs_links = folium.FeatureGroup("links")
gtfs_stops = folium.FeatureGroup("stops")
layers = [gtfs_links, gtfs_stops]
pattern_colors = ["#146DB3", "#EB9719"]
for i, row in links.iterrows():
points = row.geom.replace("MULTILINESTRING", "").replace("(", "").replace(")", "").split(", ")
points = "[[" + "],[".join([p.replace(" ", ", ") for p in points]) + "]]"
points = [[x[1], x[0]] for x in eval(points)]
_ = folium.vector_layers.PolyLine(
points,
popup=f"<b>pattern_id: {row.pattern_id}</b>",
color=pattern_colors[i],
weight=5,
).add_to(gtfs_links)
for i, row in stops.iterrows():
point = (row.Y, row.X)
_ = folium.vector_layers.CircleMarker(
point,
popup=f"<b>stop_id: {row.stop_id}</b>",
color="black",
radius=2,
fill=True,
fillColor="black",
fillOpacity=1.0,
).add_to(gtfs_stops)
Let’s create the map!
map_osm = folium.Map(location=[-29.93, -71.29], zoom_start=13)
# add all layers
for layer in layers:
layer.add_to(map_osm)
# And add layer control before we display it
folium.LayerControl().add_to(map_osm)
map_osm
project.close()
Total running time of the script: (0 minutes 26.082 seconds)