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 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%|▍ | 41/1007 [00:00<00:02, 401.40it/s]
Loading trips (Step: 5/12) : 8%|▊ | 82/1007 [00:00<00:03, 245.03it/s]
Loading trips (Step: 5/12) : 12%|█▏ | 122/1007 [00:00<00:02, 295.72it/s]
Loading trips (Step: 5/12) : 16%|█▌ | 161/1007 [00:00<00:02, 325.59it/s]
Loading trips (Step: 5/12) : 20%|█▉ | 201/1007 [00:00<00:02, 348.05it/s]
Loading trips (Step: 5/12) : 24%|██▍ | 240/1007 [00:00<00:02, 360.71it/s]
Loading trips (Step: 5/12) : 28%|██▊ | 280/1007 [00:00<00:01, 370.01it/s]
Loading trips (Step: 5/12) : 32%|███▏ | 320/1007 [00:00<00:01, 377.95it/s]
Loading trips (Step: 5/12) : 36%|███▌ | 360/1007 [00:01<00:01, 381.75it/s]
Loading trips (Step: 5/12) : 40%|███▉ | 400/1007 [00:01<00:01, 386.06it/s]
Loading trips (Step: 5/12) : 44%|████▎ | 440/1007 [00:01<00:01, 388.25it/s]
Loading trips (Step: 5/12) : 48%|████▊ | 480/1007 [00:01<00:01, 389.32it/s]
Loading trips (Step: 5/12) : 52%|█████▏ | 520/1007 [00:01<00:01, 392.18it/s]
Loading trips (Step: 5/12) : 56%|█████▌ | 561/1007 [00:01<00:01, 395.68it/s]
Loading trips (Step: 5/12) : 60%|█████▉ | 602/1007 [00:01<00:01, 399.34it/s]
Loading trips (Step: 5/12) : 64%|██████▍ | 643/1007 [00:01<00:00, 401.20it/s]
Loading trips (Step: 5/12) : 68%|██████▊ | 684/1007 [00:01<00:00, 403.32it/s]
Loading trips (Step: 5/12) : 72%|███████▏ | 725/1007 [00:01<00:00, 404.00it/s]
Loading trips (Step: 5/12) : 76%|███████▌ | 766/1007 [00:02<00:00, 401.06it/s]
Loading trips (Step: 5/12) : 80%|████████ | 807/1007 [00:02<00:00, 403.69it/s]
Loading trips (Step: 5/12) : 84%|████████▍ | 848/1007 [00:02<00:00, 286.52it/s]
Loading trips (Step: 5/12) : 88%|████████▊ | 889/1007 [00:02<00:00, 314.53it/s]
Loading trips (Step: 5/12) : 92%|█████████▏| 930/1007 [00:02<00:00, 337.06it/s]
Loading trips (Step: 5/12) : 97%|█████████▋| 972/1007 [00:02<00:00, 357.03it/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.55it/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.75it/s]
Building graphs - Indexing links (Step: 8/12) : 0it [00:00, ?it/s]
Building graphs - Indexing links (Step: 8/12) : 842it [00:00, 8415.43it/s]
Building graphs - Indexing links (Step: 8/12) : 1685it [00:00, 8420.16it/s]
Building graphs - Indexing links (Step: 8/12) : 2528it [00:00, 8327.46it/s]
Building graphs - Indexing links (Step: 8/12) : 3361it [00:00, 8050.18it/s]
Building graphs - Indexing links (Step: 8/12) : 4168it [00:00, 7867.84it/s]
Building graphs - Indexing links (Step: 8/12) : 4956it [00:00, 7609.08it/s]
Building graphs - Indexing links (Step: 8/12) : 5719it [00:00, 7294.04it/s]
Building graphs - Indexing links (Step: 8/12) : 6451it [00:00, 7124.29it/s]
Building graphs - Indexing links (Step: 8/12) : 7165it [00:00, 6828.76it/s]
Building graphs - Indexing links (Step: 8/12) : 7874it [00:01, 6902.59it/s]
Building graphs - Indexing links (Step: 8/12) : 8595it [00:01, 6989.90it/s]
Building graphs - Indexing links (Step: 8/12) : 9323it [00:01, 7073.83it/s]
Building graphs - Indexing links (Step: 8/12) : 10032it [00:01, 6995.73it/s]
Building graphs - Indexing links (Step: 8/12) : 10733it [00:01, 6852.45it/s]
Building graphs - Indexing links (Step: 8/12) : 11430it [00:01, 6886.68it/s]
Building graphs - Indexing links (Step: 8/12) : 12120it [00:01, 6798.55it/s]
Building graphs - Indexing links (Step: 8/12) : 12801it [00:01, 6623.52it/s]
Building graphs - Indexing links (Step: 8/12) : 13503it [00:01, 6737.49it/s]
Building graphs - Indexing links (Step: 8/12) : 14227it [00:01, 6884.14it/s]
Building graphs - Indexing links (Step: 8/12) : 14946it [00:02, 6974.04it/s]
Building graphs - Indexing links (Step: 8/12) : 15645it [00:02, 6869.92it/s]
Building graphs - Indexing links (Step: 8/12) : 16333it [00:02, 6744.84it/s]
Building graphs - Indexing links (Step: 8/12) : 17009it [00:02, 6196.96it/s]
Building graphs - Indexing links (Step: 8/12) : 17649it [00:02, 6237.72it/s]
Building graphs - Indexing links (Step: 8/12) : 18331it [00:02, 6400.36it/s]
Building graphs - Indexing links (Step: 8/12) : 18977it [00:02, 6381.08it/s]
Building graphs - Indexing links (Step: 8/12) : 19673it [00:02, 6548.29it/s]
Building graphs - Indexing links (Step: 8/12) : 20331it [00:02, 6466.78it/s]
Building graphs - Indexing links (Step: 8/12) : 21010it [00:03, 6542.58it/s]
Building graphs - Indexing links (Step: 8/12) : 21738it [00:03, 6750.86it/s]
Building graphs - Indexing links (Step: 8/12) : 22420it [00:03, 6769.80it/s]
Building graphs - Indexing links (Step: 8/12) : 23104it [00:03, 6789.63it/s]
Building graphs - Indexing links (Step: 8/12) : 23784it [00:03, 6747.56it/s]
Building graphs - Indexing links (Step: 8/12) : 24472it [00:03, 6786.24it/s]
Building graphs - Indexing links (Step: 8/12) : 25152it [00:03, 6759.55it/s]
Building graphs - Indexing links (Step: 8/12) : 25861it [00:03, 6856.30it/s]
Building graphs - Indexing links (Step: 8/12) : 26550it [00:03, 6864.71it/s]
Building graphs - Indexing links (Step: 8/12) : 27237it [00:03, 6768.52it/s]
Building graphs - Indexing links (Step: 8/12) : 27921it [00:04, 6788.04it/s]
Building graphs - Indexing links (Step: 8/12) : 28601it [00:04, 6727.09it/s]
Building graphs - Indexing links (Step: 8/12) : 29323it [00:04, 6853.40it/s]
Building graphs - Indexing links (Step: 8/12) : 30009it [00:04, 6849.00it/s]
Building graphs - Indexing links (Step: 8/12) : 30695it [00:04, 6746.68it/s]
Building graphs - Indexing links (Step: 8/12) : 31371it [00:04, 6723.53it/s]
Building graphs - Indexing links (Step: 8/12) : 32050it [00:04, 6741.55it/s]
Building graphs - Indexing links (Step: 8/12) : 32725it [00:04, 6728.65it/s]
Building graphs - Indexing links (Step: 8/12) : 33427it [00:04, 6813.22it/s]
Building graphs - Indexing links (Step: 8/12) : 34109it [00:04, 6679.12it/s]
Building graphs - Breaking links (Step: 8/12) : 0%| | 0/78 [00:00<?, ?it/s]
Building graphs - Breaking links (Step: 8/12) : 15%|█▌ | 12/78 [00:00<00:00, 119.36it/s]
Building graphs - Breaking links (Step: 8/12) : 32%|███▏ | 25/78 [00:00<00:00, 125.13it/s]
Building graphs - Breaking links (Step: 8/12) : 49%|████▊ | 38/78 [00:00<00:00, 114.34it/s]
Building graphs - Breaking links (Step: 8/12) : 64%|██████▍ | 50/78 [00:00<00:00, 112.02it/s]
Building graphs - Breaking links (Step: 8/12) : 79%|███████▉ | 62/78 [00:00<00:00, 101.78it/s]
Building graphs - Breaking links (Step: 8/12) : 94%|█████████▎| 73/78 [00:00<00:00, 100.52it/s]
Map-matching patterns : 3%|▎ | 2/78 [00:11<00:00, 100.52it/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, 47.50it/s]
Saving stops (Step: 12/12) : 13%|█▎ | 10/78 [00:00<00:01, 39.91it/s]
Saving stops (Step: 12/12) : 19%|█▉ | 15/78 [00:00<00:01, 39.06it/s]
Saving stops (Step: 12/12) : 24%|██▍ | 19/78 [00:00<00:01, 39.08it/s]
Saving stops (Step: 12/12) : 29%|██▉ | 23/78 [00:00<00:01, 38.85it/s]
Saving stops (Step: 12/12) : 35%|███▍ | 27/78 [00:00<00:01, 38.95it/s]
Saving stops (Step: 12/12) : 40%|███▉ | 31/78 [00:00<00:01, 38.74it/s]
Saving stops (Step: 12/12) : 45%|████▍ | 35/78 [00:00<00:01, 38.87it/s]
Saving stops (Step: 12/12) : 50%|█████ | 39/78 [00:00<00:01, 38.68it/s]
Saving stops (Step: 12/12) : 55%|█████▌ | 43/78 [00:01<00:00, 38.82it/s]
Saving stops (Step: 12/12) : 60%|██████ | 47/78 [00:01<00:00, 38.44it/s]
Saving stops (Step: 12/12) : 65%|██████▌ | 51/78 [00:01<00:00, 37.43it/s]
Saving stops (Step: 12/12) : 71%|███████ | 55/78 [00:01<00:00, 37.55it/s]
Saving stops (Step: 12/12) : 76%|███████▌ | 59/78 [00:01<00:00, 37.29it/s]
Saving stops (Step: 12/12) : 81%|████████ | 63/78 [00:01<00:00, 37.33it/s]
Saving stops (Step: 12/12) : 86%|████████▌ | 67/78 [00:01<00:00, 36.87it/s]
Saving stops (Step: 12/12) : 91%|█████████ | 71/78 [00:01<00:00, 37.13it/s]
Saving stops (Step: 12/12) : 96%|█████████▌| 75/78 [00:01<00:00, 37.13it/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.637 seconds)