.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/aequilibrae_project/plot_connections.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_aequilibrae_project_plot_connections.py: .. _example_aequilibrae_connections: Project Connections =================== In this example, we show how to use AequilibraE's database connections within a project. .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. admonition:: References * :doc:`../../aequilibrae_project` .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.project.project.Project.db_connection` * :func:`aequilibrae.project.project.Project.db_connection_spatial` * :func:`aequilibrae.project.project.Project.results_connection` * :func:`aequilibrae.project.project.Project.transit_connection` .. GENERATED FROM PYTHON SOURCE LINES 25-36 .. code-block:: Python # Imports from uuid import uuid4 from tempfile import gettempdir from pathlib import Path import geopandas as gpd import pandas as pd from aequilibrae.utils.create_example import create_example .. rst-class:: sphx-glr-script-out .. code-block:: none No pre-existing parameter file exists for this project. Will use default .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python # We create the example project inside our temp folder. fldr = Path(gettempdir()) / uuid4().hex project = create_example(fldr, "sioux_falls") .. GENERATED FROM PYTHON SOURCE LINES 43-49 All AequilibraE projects presents four types of connections in the form of properties: * General connection to the project database * Spatial connection to the project database * General connection to the results database * Spatial connection to the transit database .. GENERATED FROM PYTHON SOURCE LINES 51-52 Each connection can be easily accessed as follows: .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python with project.db_connection as conn: matrices = pd.read_sql("SELECT * FROM matrices", conn) .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python matrices .. raw:: html
name file_name cores procedure procedure_id timestamp description
0 demand_omx demand.omx 1 None None 2020-11-24 08:47:18 Original data imported to OMX format
1 demand_mc demand_mc.omx 3 None None 2021-02-24 00:51:35 None
2 skims skims.omx 2 None None None Example skim
3 demand_aem demand.aem 1 None None 2020-11-24 08:46:42 Original data imported to AEM format


.. GENERATED FROM PYTHON SOURCE LINES 59-60 We encourage using spatial connections only when handling spatial data. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python with project.db_connection_spatial as conn: nodes = gpd.read_postgis("SELECT zone_id, ST_AsBinary(geometry) geom FROM zones;", con=conn, geom_col="geom", crs=4326) .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python nodes.head() .. raw:: html
zone_id geom
0 8 MULTIPOLYGON (((-96.705 43.5611, -96.70333 43....
1 10 MULTIPOLYGON (((-96.72833 43.538, -96.73 43.53...
2 7 MULTIPOLYGON (((-96.685 43.5611, -96.68333 43....
3 12 MULTIPOLYGON (((-96.78333 43.5178, -96.785 43....
4 9 MULTIPOLYGON (((-96.72833 43.54955, -96.73 43....


.. GENERATED FROM PYTHON SOURCE LINES 67-70 For accessing both results and transit databases, the procedure is the same. When you're done, don't forget to close the project. .. GENERATED FROM PYTHON SOURCE LINES 70-71 .. code-block:: Python project.close() .. rst-class:: sphx-glr-script-out .. code-block:: none This project at /tmp/6740f4171afa4284928926c2ad1c6519 is already closed .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.970 seconds) .. _sphx_glr_download__auto_examples_aequilibrae_project_plot_connections.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_connections.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_connections.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_connections.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_