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 geopandas as gpd
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)                        :   2%|▏         | 25/1007 [00:00<00:04, 243.58it/s]
Loading trips (Step: 5/12)                        :   5%|▍         | 50/1007 [00:00<00:04, 238.71it/s]
Loading trips (Step: 5/12)                        :   7%|▋         | 74/1007 [00:00<00:05, 165.55it/s]
Loading trips (Step: 5/12)                        :  10%|▉         | 98/1007 [00:00<00:04, 187.55it/s]
Loading trips (Step: 5/12)                        :  12%|█▏        | 122/1007 [00:00<00:04, 202.93it/s]
Loading trips (Step: 5/12)                        :  14%|█▍        | 146/1007 [00:00<00:04, 212.99it/s]
Loading trips (Step: 5/12)                        :  17%|█▋        | 170/1007 [00:00<00:03, 220.36it/s]
Loading trips (Step: 5/12)                        :  19%|█▉        | 194/1007 [00:00<00:03, 225.58it/s]
Loading trips (Step: 5/12)                        :  22%|██▏       | 218/1007 [00:01<00:03, 228.20it/s]
Loading trips (Step: 5/12)                        :  24%|██▍       | 242/1007 [00:01<00:03, 231.17it/s]
Loading trips (Step: 5/12)                        :  26%|██▋       | 266/1007 [00:01<00:03, 233.34it/s]
Loading trips (Step: 5/12)                        :  29%|██▉       | 290/1007 [00:01<00:03, 233.59it/s]
Loading trips (Step: 5/12)                        :  31%|███       | 314/1007 [00:01<00:02, 234.79it/s]
Loading trips (Step: 5/12)                        :  34%|███▎      | 338/1007 [00:01<00:02, 234.30it/s]
Loading trips (Step: 5/12)                        :  36%|███▌      | 362/1007 [00:01<00:02, 234.59it/s]
Loading trips (Step: 5/12)                        :  38%|███▊      | 386/1007 [00:01<00:02, 235.56it/s]
Loading trips (Step: 5/12)                        :  41%|████      | 410/1007 [00:01<00:02, 235.53it/s]
Loading trips (Step: 5/12)                        :  43%|████▎     | 434/1007 [00:01<00:02, 236.40it/s]
Loading trips (Step: 5/12)                        :  45%|████▌     | 458/1007 [00:02<00:02, 236.55it/s]
Loading trips (Step: 5/12)                        :  48%|████▊     | 482/1007 [00:02<00:02, 236.28it/s]
Loading trips (Step: 5/12)                        :  50%|█████     | 506/1007 [00:02<00:02, 234.39it/s]
Loading trips (Step: 5/12)                        :  53%|█████▎    | 532/1007 [00:02<00:01, 240.75it/s]
Loading trips (Step: 5/12)                        :  55%|█████▌    | 558/1007 [00:02<00:01, 244.88it/s]
Loading trips (Step: 5/12)                        :  58%|█████▊    | 584/1007 [00:02<00:01, 248.37it/s]
Loading trips (Step: 5/12)                        :  61%|██████    | 610/1007 [00:02<00:01, 250.22it/s]
Loading trips (Step: 5/12)                        :  63%|██████▎   | 636/1007 [00:02<00:01, 251.13it/s]
Loading trips (Step: 5/12)                        :  66%|██████▌   | 662/1007 [00:02<00:01, 252.61it/s]
Loading trips (Step: 5/12)                        :  68%|██████▊   | 688/1007 [00:02<00:01, 252.70it/s]
Loading trips (Step: 5/12)                        :  71%|███████   | 714/1007 [00:03<00:01, 253.52it/s]
Loading trips (Step: 5/12)                        :  73%|███████▎  | 740/1007 [00:03<00:01, 254.06it/s]
Loading trips (Step: 5/12)                        :  76%|███████▌  | 766/1007 [00:03<00:00, 252.51it/s]
Loading trips (Step: 5/12)                        :  79%|███████▊  | 792/1007 [00:03<00:00, 253.30it/s]
Loading trips (Step: 5/12)                        :  81%|████████  | 818/1007 [00:03<00:00, 253.70it/s]
Loading trips (Step: 5/12)                        :  84%|████████▍ | 844/1007 [00:03<00:00, 177.51it/s]
Loading trips (Step: 5/12)                        :  86%|████████▋ | 870/1007 [00:03<00:00, 195.06it/s]
Loading trips (Step: 5/12)                        :  89%|████████▉ | 896/1007 [00:03<00:00, 210.34it/s]
Loading trips (Step: 5/12)                        :  92%|█████████▏| 922/1007 [00:04<00:00, 221.21it/s]
Loading trips (Step: 5/12)                        :  94%|█████████▍| 948/1007 [00:04<00:00, 229.69it/s]
Loading trips (Step: 5/12)                        :  97%|█████████▋| 973/1007 [00:04<00:00, 235.03it/s]
Loading trips (Step: 5/12)                        :  99%|█████████▉| 999/1007 [00:04<00:00, 240.38it/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.60it/s]


Loading data for 2016-04-13 (Step: 9/12) -        :   0%|          | 0/1 [00:00<?, ?it/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)                        :   8%|▊         | 6/78 [00:00<00:01, 50.01it/s]
Saving stops (Step: 12/12)                        :  15%|█▌        | 12/78 [00:00<00:01, 45.12it/s]
Saving stops (Step: 12/12)                        :  22%|██▏       | 17/78 [00:00<00:01, 44.01it/s]
Saving stops (Step: 12/12)                        :  28%|██▊       | 22/78 [00:00<00:01, 43.11it/s]
Saving stops (Step: 12/12)                        :  35%|███▍      | 27/78 [00:00<00:01, 42.55it/s]
Saving stops (Step: 12/12)                        :  41%|████      | 32/78 [00:00<00:01, 42.45it/s]
Saving stops (Step: 12/12)                        :  47%|████▋     | 37/78 [00:00<00:00, 42.03it/s]
Saving stops (Step: 12/12)                        :  54%|█████▍    | 42/78 [00:00<00:00, 41.80it/s]
Saving stops (Step: 12/12)                        :  60%|██████    | 47/78 [00:01<00:00, 41.52it/s]
Saving stops (Step: 12/12)                        :  67%|██████▋   | 52/78 [00:01<00:00, 41.70it/s]
Saving stops (Step: 12/12)                        :  73%|███████▎  | 57/78 [00:01<00:00, 41.78it/s]
Saving stops (Step: 12/12)                        :  79%|███████▉  | 62/78 [00:01<00:00, 42.16it/s]
Saving stops (Step: 12/12)                        :  86%|████████▌ | 67/78 [00:01<00:00, 42.09it/s]
Saving stops (Step: 12/12)                        :  92%|█████████▏| 72/78 [00:01<00:00, 42.02it/s]
Saving stops (Step: 12/12)                        :  99%|█████████▊| 77/78 [00:01<00:00, 42.26it/s]

Now we will plot one of the route’s patterns we just imported

conn = database_connection("transit")

patterns = 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)

We turn the patterns and stops DataFrames into GeoDataFrames so we can plot them more easily.

patterns = gpd.GeoDataFrame(patterns, geometry=gpd.GeoSeries.from_wkt(patterns["geom"]), crs=4326)
stops = gpd.GeoDataFrame(stops, geometry=gpd.GeoSeries.from_xy(stops["X"], stops["Y"]), crs=4326)

And plot out data!

map = patterns.explore(color=["#146DB3", "#EB9719"], style_kwds={"weight": 4}, name="links")
map = stops.explore(m=map, color="black", style_kwds={"radius": 2, "fillOpacity": 1.0}, name="stops")

folium.LayerControl().add_to(map)
map
Make this Notebook Trusted to load map: File -> Trust Notebook


project.close()
This project at /tmp/b7a17710b9874cffbb71a6350e29d3c0 is already closed

Total running time of the script: (0 minutes 8.647 seconds)

Gallery generated by Sphinx-Gallery