.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/creating_models/plot_import_gtfs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_creating_models_plot_import_gtfs.py: .. _example_gtfs: 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. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.transit.Transit` * :func:`aequilibrae.transit.lib_gtfs.GTFSRouteSystemBuilder` .. GENERATED FROM PYTHON SOURCE LINES 19-34 .. code-block:: Python # 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 .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python # Let's create an empty project on an arbitrary folder. fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "coquimbo") .. GENERATED FROM PYTHON SOURCE LINES 42-44 As the Coquimbo example already has a complete GTFS model, we shall remove its public transport database for the sake of this example. .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python remove(join(fldr, "public_transport.sqlite")) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Let's import the GTFS feed. .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python dest_path = join(fldr, "gtfs_coquimbo.zip") .. GENERATED FROM PYTHON SOURCE LINES 51-53 Now we create our Transit object and import the GTFS feed into our model. This will automatically create a new public transport database. .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python data = Transit(project) transit = data.new_gtfs_builder(agency="Lisanco", file_path=dest_path) .. GENERATED FROM PYTHON SOURCE LINES 59-62 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. .. GENERATED FROM PYTHON SOURCE LINES 62-73 .. code-block:: Python 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() .. rst-class:: sphx-glr-script-out .. code-block:: none Loading routes (Step: 1/12) : 0it [00:00, ?it/s] Loading stops (Step: 2/12) : 0%| | 0/78 [00:00pattern_id: {row.pattern_id}", 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"stop_id: {row.stop_id}", color="black", radius=2, fill=True, fillColor="black", fillOpacity=1.0, ).add_to(gtfs_stops) .. GENERATED FROM PYTHON SOURCE LINES 117-118 Let's create the map! .. GENERATED FROM PYTHON SOURCE LINES 118-128 .. code-block:: Python 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 .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 129-130 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 25.789 seconds) .. _sphx_glr_download__auto_examples_creating_models_plot_import_gtfs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_import_gtfs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_import_gtfs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_import_gtfs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_