Project Connections#

In this example, we show how to use AequilibraE’s database connections within a project.

See also

Several functions, methods, classes and modules are used in this example:

# Imports
from uuid import uuid4
from tempfile import gettempdir
from pathlib import Path

import geopandas as gpd
import pandas as pd

from aequilibrae.examples import create_example
# We create the example project inside our temp folder.
fldr = Path(gettempdir()) / uuid4().hex
project = create_example(fldr, "sioux_falls")

All AequilibraE projects present three types of connections in the form of properties:

  • Spatial connection to the project database

  • General connection to the results database

  • Spatial connection to the transit database

Each connection can be easily accessed as follows:

with project.db_connection as conn:
    modes = pd.read_sql("SELECT * FROM modes", conn)
modes
mode_name mode_id description alpha pce beta gamma delta epsilon zeta iota sigma phi tau vot ppv
0 car c All motorized vehicles None 1.00 None None None None None None None None None None 1
1 transit t Public transport vehicles None 3.00 None None None None None None None None None None 1
2 walk w Walking links None 0.10 None None None None None None None None None None 1
3 bicycle b Biking links None 0.15 None None None None None None None None None None 1
4 trucks T Trucks None 2.50 None None None None None None None None None None 1
5 motorcycles M Motorcycle None 0.20 None None None None None None None None None None 1


As the project database connection is spatial, it can also be used to query spatial data directly.

with project.db_connection as conn:
    nodes = gpd.read_postgis("SELECT zone_id, ST_AsBinary(geometry) geom FROM zones;", con=conn, geom_col="geom", crs=4326)
nodes.head()
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....


For accessing both results and transit databases, the procedure is the same.

When you’re done, don’t forget to close the project.

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

Gallery generated by Sphinx-Gallery