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)                        :   3%|▎         | 35/1007 [00:00<00:02, 348.20it/s]
Loading trips (Step: 5/12)                        :   7%|▋         | 70/1007 [00:00<00:02, 348.85it/s]
Loading trips (Step: 5/12)                        :  10%|█         | 105/1007 [00:00<00:03, 248.99it/s]
Loading trips (Step: 5/12)                        :  14%|█▍        | 140/1007 [00:00<00:03, 280.68it/s]
Loading trips (Step: 5/12)                        :  17%|█▋        | 176/1007 [00:00<00:02, 303.65it/s]
Loading trips (Step: 5/12)                        :  21%|██        | 211/1007 [00:00<00:02, 316.97it/s]
Loading trips (Step: 5/12)                        :  25%|██▍       | 247/1007 [00:00<00:02, 328.45it/s]
Loading trips (Step: 5/12)                        :  28%|██▊       | 283/1007 [00:00<00:02, 336.03it/s]
Loading trips (Step: 5/12)                        :  32%|███▏      | 319/1007 [00:00<00:02, 342.21it/s]
Loading trips (Step: 5/12)                        :  35%|███▌      | 354/1007 [00:01<00:01, 344.18it/s]
Loading trips (Step: 5/12)                        :  39%|███▊      | 390/1007 [00:01<00:01, 347.66it/s]
Loading trips (Step: 5/12)                        :  42%|████▏     | 426/1007 [00:01<00:01, 349.16it/s]
Loading trips (Step: 5/12)                        :  46%|████▌     | 462/1007 [00:01<00:01, 350.54it/s]
Loading trips (Step: 5/12)                        :  49%|████▉     | 498/1007 [00:01<00:01, 351.26it/s]
Loading trips (Step: 5/12)                        :  53%|█████▎    | 535/1007 [00:01<00:01, 355.99it/s]
Loading trips (Step: 5/12)                        :  57%|█████▋    | 572/1007 [00:01<00:01, 359.45it/s]
Loading trips (Step: 5/12)                        :  61%|██████    | 610/1007 [00:01<00:01, 362.84it/s]
Loading trips (Step: 5/12)                        :  64%|██████▍   | 647/1007 [00:01<00:00, 364.67it/s]
Loading trips (Step: 5/12)                        :  68%|██████▊   | 684/1007 [00:02<00:00, 365.63it/s]
Loading trips (Step: 5/12)                        :  72%|███████▏  | 721/1007 [00:02<00:00, 362.72it/s]
Loading trips (Step: 5/12)                        :  75%|███████▌  | 758/1007 [00:02<00:00, 364.46it/s]
Loading trips (Step: 5/12)                        :  79%|███████▉  | 796/1007 [00:02<00:00, 366.63it/s]
Loading trips (Step: 5/12)                        :  83%|████████▎ | 833/1007 [00:02<00:00, 367.53it/s]
Loading trips (Step: 5/12)                        :  86%|████████▋ | 870/1007 [00:02<00:00, 274.37it/s]
Loading trips (Step: 5/12)                        :  90%|█████████ | 907/1007 [00:02<00:00, 297.15it/s]
Loading trips (Step: 5/12)                        :  94%|█████████▍| 945/1007 [00:02<00:00, 316.38it/s]
Loading trips (Step: 5/12)                        :  98%|█████████▊| 982/1007 [00:02<00:00, 330.62it/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.59it/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, 51.25it/s]
Saving stops (Step: 12/12)                        :  15%|█▌        | 12/78 [00:00<00:01, 45.97it/s]
Saving stops (Step: 12/12)                        :  22%|██▏       | 17/78 [00:00<00:01, 44.67it/s]
Saving stops (Step: 12/12)                        :  28%|██▊       | 22/78 [00:00<00:01, 43.99it/s]
Saving stops (Step: 12/12)                        :  35%|███▍      | 27/78 [00:00<00:01, 43.48it/s]
Saving stops (Step: 12/12)                        :  41%|████      | 32/78 [00:00<00:01, 43.40it/s]
Saving stops (Step: 12/12)                        :  47%|████▋     | 37/78 [00:00<00:00, 43.07it/s]
Saving stops (Step: 12/12)                        :  54%|█████▍    | 42/78 [00:00<00:00, 42.86it/s]
Saving stops (Step: 12/12)                        :  60%|██████    | 47/78 [00:01<00:00, 42.92it/s]
Saving stops (Step: 12/12)                        :  67%|██████▋   | 52/78 [00:01<00:00, 42.65it/s]
Saving stops (Step: 12/12)                        :  73%|███████▎  | 57/78 [00:01<00:00, 42.41it/s]
Saving stops (Step: 12/12)                        :  79%|███████▉  | 62/78 [00:01<00:00, 42.51it/s]
Saving stops (Step: 12/12)                        :  86%|████████▌ | 67/78 [00:01<00:00, 42.37it/s]
Saving stops (Step: 12/12)                        :  92%|█████████▏| 72/78 [00:01<00:00, 42.31it/s]
Saving stops (Step: 12/12)                        :  99%|█████████▊| 77/78 [00:01<00:00, 42.35it/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/71ec50680b8345c38840ae144f2cd978 is already closed

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

Gallery generated by Sphinx-Gallery