Note
Go to the end to download the full example code.
Network skimming#
In this example, we show how to perform network skimming for Coquimbo, a city in La Serena Metropolitan Area in Chile.
See also
Several functions, methods, classes and modules are used in this example:
aequilibrae.routing.graph()aequilibrae.routing.network_skimming()
# Imports
from uuid import uuid4
from tempfile import gettempdir
from os.path import join
from aequilibrae.examples import create_example
from aequilibrae.utils import basic_config
# We create the example project inside our temp folder
fldr = join(gettempdir(), uuid4().hex)
project = create_example(fldr, "coquimbo")
Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path
Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path
# We'll also apply a basic logging configuration.
basic_config()
<AequilibraETQDMStreamHandler (NOTSET)>
Network Skimming#
import numpy as np
Load the network snapshot
network = project.road.build_network()
# Several fields in the project are filled with ``NaN``s.
# This is true, but we won't use those fields.
we also see what modes are available
network.modes
# let's say we want to minimise the distance
# And will skim distance while we are at it, other fields like ``free_flow_time`` or ``travel_time``
# can be added here as well
# But let's say we only want a skim matrix for nodes 28-40, and 49-90 (inclusive),
# these happen to be a selection of western centroids.
centroids = np.array(list(range(28, 41)) + list(range(49, 91)))
graph = network.build_graph(mode="c", cost="distance", skimming=["distance"], centroids=centroids, compact=True)
And run the skimming
: 0%| | 0/55 [00:00<?, ?it/s]
55/55 : 100%|██████████| 55/55 [00:00<00:00, 1476.42it/s]
Saving Outputs : 100%|██████████| 55/55 [00:00<00:00, 1469.91it/s]
Building network skims directly from the graph is more straightforward, though
we could alternatively use the class NetworkSkimming to achieve the same result.
# from aequilibrae.routing import NetworkSkimming
# skm = NetworkSkimming(graph)
# skm.execute()
The result is an AequilibraeMatrix object
skims = skm.results.skims
# Which we can manipulate directly in memory, if we wish
skims.imat["distance", :3, :3]
array([[ 0. , 4017.31354851, 6028.43673889],
[2839.42820648, 0. , 2448.7555198 ],
[4898.09421015, 2450.66722204, 0. ]])
Or access each matrix, lets just look at the first 3x3
skims["distance"][:3, :3]
array([[ 0. , 4017.31354851, 6028.43673889],
[2839.42820648, 0. , 2448.7555198 ],
[4898.09421015, 2450.66722204, 0. ]])
We can save it to the project if we want
project.matrices.save_matrix(
"base_skims.omx",
skims,
procedure="Network skimming",
procedure_id=skm.procedure_id,
timestamp=skm.procedure_date,
)
We can also open this skim file to write something to its description
matrices = project.matrices
with matrices.get_matrix("base_skims.omx", mode="r+") as matrix:
matrix.metadata["description"] = "minimised distance while also skimming distance for just a few nodes"
Total running time of the script: (0 minutes 0.341 seconds)