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.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%|██████▎   | 639/1007 [00:00<00:00, 3612.73it/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%|▏         | 20/1007 [00:00<00:04, 199.21it/s]
Loading trips (Step: 5/12)                        :   4%|▍         | 40/1007 [00:00<00:04, 194.82it/s]
Loading trips (Step: 5/12)                        :   6%|▌         | 60/1007 [00:00<00:04, 193.72it/s]
Loading trips (Step: 5/12)                        :   8%|▊         | 80/1007 [00:00<00:04, 192.35it/s]
Loading trips (Step: 5/12)                        :  10%|▉         | 100/1007 [00:00<00:04, 192.42it/s]
Loading trips (Step: 5/12)                        :  12%|█▏        | 120/1007 [00:00<00:04, 192.82it/s]
Loading trips (Step: 5/12)                        :  14%|█▍        | 140/1007 [00:00<00:04, 192.16it/s]
Loading trips (Step: 5/12)                        :  16%|█▌        | 160/1007 [00:00<00:04, 192.92it/s]
Loading trips (Step: 5/12)                        :  18%|█▊        | 180/1007 [00:00<00:04, 193.13it/s]
Loading trips (Step: 5/12)                        :  20%|█▉        | 200/1007 [00:01<00:04, 193.44it/s]
Loading trips (Step: 5/12)                        :  22%|██▏       | 220/1007 [00:01<00:04, 192.14it/s]
Loading trips (Step: 5/12)                        :  24%|██▍       | 240/1007 [00:01<00:03, 192.76it/s]
Loading trips (Step: 5/12)                        :  26%|██▌       | 260/1007 [00:01<00:03, 193.10it/s]
Loading trips (Step: 5/12)                        :  28%|██▊       | 280/1007 [00:01<00:03, 192.64it/s]
Loading trips (Step: 5/12)                        :  30%|██▉       | 300/1007 [00:01<00:03, 193.09it/s]
Loading trips (Step: 5/12)                        :  32%|███▏      | 320/1007 [00:01<00:03, 193.96it/s]
Loading trips (Step: 5/12)                        :  34%|███▍      | 345/1007 [00:01<00:03, 209.78it/s]
Loading trips (Step: 5/12)                        :  36%|███▋      | 367/1007 [00:01<00:03, 204.39it/s]
Loading trips (Step: 5/12)                        :  39%|███▊      | 388/1007 [00:01<00:03, 201.09it/s]
Loading trips (Step: 5/12)                        :  41%|████      | 409/1007 [00:02<00:03, 198.41it/s]
Loading trips (Step: 5/12)                        :  43%|████▎     | 429/1007 [00:02<00:02, 195.83it/s]
Loading trips (Step: 5/12)                        :  45%|████▍     | 449/1007 [00:02<00:02, 195.74it/s]
Loading trips (Step: 5/12)                        :  47%|████▋     | 469/1007 [00:02<00:02, 195.60it/s]
Loading trips (Step: 5/12)                        :  49%|████▊     | 489/1007 [00:02<00:02, 194.66it/s]
Loading trips (Step: 5/12)                        :  51%|█████     | 509/1007 [00:02<00:02, 194.80it/s]
Loading trips (Step: 5/12)                        :  53%|█████▎    | 530/1007 [00:02<00:02, 197.70it/s]
Loading trips (Step: 5/12)                        :  55%|█████▍    | 551/1007 [00:02<00:02, 199.70it/s]
Loading trips (Step: 5/12)                        :  57%|█████▋    | 572/1007 [00:02<00:02, 200.12it/s]
Loading trips (Step: 5/12)                        :  59%|█████▉    | 593/1007 [00:03<00:02, 201.45it/s]
Loading trips (Step: 5/12)                        :  61%|██████    | 614/1007 [00:03<00:01, 202.28it/s]
Loading trips (Step: 5/12)                        :  63%|██████▎   | 635/1007 [00:03<00:01, 202.18it/s]
Loading trips (Step: 5/12)                        :  65%|██████▌   | 656/1007 [00:03<00:01, 198.20it/s]
Loading trips (Step: 5/12)                        :  68%|██████▊   | 682/1007 [00:03<00:01, 214.56it/s]
Loading trips (Step: 5/12)                        :  71%|███████   | 710/1007 [00:03<00:01, 192.67it/s]
Loading trips (Step: 5/12)                        :  74%|███████▍  | 747/1007 [00:03<00:01, 237.05it/s]
Loading trips (Step: 5/12)                        :  78%|███████▊  | 784/1007 [00:03<00:00, 271.29it/s]
Loading trips (Step: 5/12)                        :  82%|████████▏ | 822/1007 [00:03<00:00, 300.34it/s]
Loading trips (Step: 5/12)                        :  85%|████████▌ | 859/1007 [00:04<00:00, 319.81it/s]
Loading trips (Step: 5/12)                        :  89%|████████▉ | 897/1007 [00:04<00:00, 336.40it/s]
Loading trips (Step: 5/12)                        :  93%|█████████▎| 935/1007 [00:04<00:00, 347.82it/s]
Loading trips (Step: 5/12)                        :  97%|█████████▋| 974/1007 [00:04<00:00, 357.64it/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.62it/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, 55.59it/s]
Saving stops (Step: 12/12)                        :  15%|█▌        | 12/78 [00:00<00:01, 49.92it/s]
Saving stops (Step: 12/12)                        :  23%|██▎       | 18/78 [00:00<00:01, 48.70it/s]
Saving stops (Step: 12/12)                        :  29%|██▉       | 23/78 [00:00<00:01, 47.77it/s]
Saving stops (Step: 12/12)                        :  36%|███▌      | 28/78 [00:00<00:01, 47.28it/s]
Saving stops (Step: 12/12)                        :  42%|████▏     | 33/78 [00:00<00:00, 45.77it/s]
Saving stops (Step: 12/12)                        :  49%|████▊     | 38/78 [00:00<00:00, 45.89it/s]
Saving stops (Step: 12/12)                        :  55%|█████▌    | 43/78 [00:00<00:00, 46.06it/s]
Saving stops (Step: 12/12)                        :  62%|██████▏   | 48/78 [00:01<00:00, 45.99it/s]
Saving stops (Step: 12/12)                        :  68%|██████▊   | 53/78 [00:01<00:00, 46.05it/s]
Saving stops (Step: 12/12)                        :  74%|███████▍  | 58/78 [00:01<00:00, 46.08it/s]
Saving stops (Step: 12/12)                        :  81%|████████  | 63/78 [00:01<00:00, 46.39it/s]
Saving stops (Step: 12/12)                        :  87%|████████▋ | 68/78 [00:01<00:00, 46.36it/s]
Saving stops (Step: 12/12)                        :  94%|█████████▎| 73/78 [00:01<00:00, 46.41it/s]
Saving stops (Step: 12/12)                        : 100%|██████████| 78/78 [00:01<00:00, 46.61it/s]

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

with project.transit_connection as conn:
    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/b9ad28ba904d4daaacb24f994e0929b8 is already closed

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

Gallery generated by Sphinx-Gallery