.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_path_and_skimming.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_plot_path_and_skimming.py: Path and skimming ================= On this example we show how to perform path computation and network skimming for the Sioux Falls example model. .. GENERATED FROM PYTHON SOURCE LINES 8-20 .. code-block:: python ## Imports from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae.utils.create_example import create_example # We create the example project inside our temp folder fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr) .. GENERATED FROM PYTHON SOURCE LINES 21-31 .. code-block:: python from aequilibrae import logger import logging import sys # We the project open, we can tell the logger to direct all messages to the terminal as well stdout_handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter("%(asctime)s;%(name)s;%(levelname)s ; %(message)s") stdout_handler.setFormatter(formatter) logger.addHandler(stdout_handler) .. GENERATED FROM PYTHON SOURCE LINES 32-33 imports .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: python from aequilibrae.paths import PathResults .. GENERATED FROM PYTHON SOURCE LINES 36-37 we build all graphs .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: python project.network.build_graphs() # We get warnings that several fields in the project are filled with NaNs. Which is true, but we won't use those fields .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2022-03-30 06:33:33,453;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations 2022-03-30 06:33:33,494;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations 2022-03-30 06:33:33,533;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations 2022-03-30 06:33:33,571;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations 2022-03-30 06:33:33,608;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations 2022-03-30 06:33:33,646;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations .. GENERATED FROM PYTHON SOURCE LINES 41-42 we grab the graph for cars .. GENERATED FROM PYTHON SOURCE LINES 42-58 .. code-block:: python graph = project.network.graphs['c'] # we also see what graphs are available # project.network.graphs.keys() # let's say we want to minimize distance graph.set_graph('distance') # And will skim time and distance while we are at it graph.set_skimming(['free_flow_time', 'distance']) # And we will allow paths to be compute going through other centroids/centroid connectors # required for the Sioux Falls network, as all nodes are centroids # BE CAREFUL WITH THIS SETTING graph.set_blocked_centroid_flows(False) .. GENERATED FROM PYTHON SOURCE LINES 59-60 instantiate a path results object and prepare it to work with the graph .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: python res = PathResults() res.prepare(graph) # compute a path from node 8 to 13 res.compute_path(8, 4) .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: python # We can get the sequence of nodes we traverse res.path_nodes .. rst-class:: sphx-glr-script-out Out: .. code-block:: none array([8, 9, 5, 4]) .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. code-block:: python # We can get the link sequence we traverse res.path .. rst-class:: sphx-glr-script-out Out: .. code-block:: none array([21, 23, 11]) .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: python # We can get the mileposts for our sequence of nodes res.milepost .. rst-class:: sphx-glr-script-out Out: .. code-block:: none array([ 0. , 2213.32449172, 3927.86012613, 5188.91313598]) .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: python # If we want to compute the path for a different destination and same origin, we can just do this # It is way faster when you have large networks res.update_trace(13) .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: python res.path_nodes .. rst-class:: sphx-glr-script-out Out: .. code-block:: none array([ 8, 9, 10, 15, 22, 21, 24, 13]) .. GENERATED FROM PYTHON SOURCE LINES 92-94 If you want to show the path in Python We do NOT recommend this, though.... It is very slow for real networks .. GENERATED FROM PYTHON SOURCE LINES 94-111 .. code-block:: python import matplotlib.pyplot as plt from shapely.ops import linemerge links = project.network.links # We plot the entire network curr = project.conn.cursor() curr.execute('Select link_id from links;') for lid in curr.fetchall(): geo = links.get(lid[0]).geometry plt.plot(*geo.xy, color='red') path_geometry = linemerge(links.get(lid).geometry for lid in res.path) plt.plot(*path_geometry.xy, color='blue', linestyle='dashed', linewidth=2) plt.show() .. image-sg:: /_auto_examples/images/sphx_glr_plot_path_and_skimming_001.png :alt: plot path and skimming :srcset: /_auto_examples/images/sphx_glr_plot_path_and_skimming_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: python ## Now to skimming .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: python from aequilibrae.paths import NetworkSkimming .. GENERATED FROM PYTHON SOURCE LINES 120-121 But let's say we only want a skim matrix for nodes 1, 3, 6 & 8 .. GENERATED FROM PYTHON SOURCE LINES 121-124 .. code-block:: python import numpy as np graph.prepare_graph(np.array([1, 3, 6, 8])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2022-03-30 06:33:33,838;aequilibrae;WARNING ; Field(s) name, lanes has(ve) at least one NaN value. Check your computations .. GENERATED FROM PYTHON SOURCE LINES 125-130 .. code-block:: python # And run the skimming skm = NetworkSkimming(graph) skm.execute() .. GENERATED FROM PYTHON SOURCE LINES 131-138 .. code-block:: python # The result is an AequilibraEMatrix object skims = skm.results.skims # Which we can manipulate directly from its temp file, if we wish skims.matrices .. rst-class:: sphx-glr-script-out Out: .. code-block:: none memmap([[[0.00000000e+00, 0.00000000e+00], [6.00000000e+00, 4.84008627e+03], [9.00000000e+00, 6.46536178e+03], [1.30000000e+01, 8.88889720e+03]], [[6.00000000e+00, 4.84008627e+03], [0.00000000e+00, 0.00000000e+00], [9.00000000e+00, 6.73286110e+03], [2.10000000e+01, 6.82126560e+03]], [[9.00000000e+00, 4.97310839e+03], [1.00000000e+01, 8.30423993e+03], [0.00000000e+00, 0.00000000e+00], [4.00000000e+00, 2.42353542e+03]], [[1.10000000e+01, 6.23416140e+03], [2.00000000e+01, 8.12432422e+03], [2.00000000e+00, 1.26105301e+03], [0.00000000e+00, 0.00000000e+00]]]) .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: python # Or access each matrix skims.free_flow_time .. rst-class:: sphx-glr-script-out Out: .. code-block:: none memmap([[ 0., 6., 9., 13.], [ 6., 0., 9., 21.], [ 9., 10., 0., 4.], [11., 20., 2., 0.]]) .. GENERATED FROM PYTHON SOURCE LINES 144-154 .. code-block:: python # We can save it to the project if we want skm.save_to_project('base_skims') # We can also retrieve this skim record to write something to its description matrices = project.matrices mat_record = matrices.get_record('base_skims') mat_record.description = 'minimized FF travel time while also skimming distance for just a few nodes' mat_record.save() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2022-03-30 06:33:34,005;aequilibrae;WARNING ; Matrix Record has been saved to the database .. GENERATED FROM PYTHON SOURCE LINES 155-156 .. code-block:: python project.close() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2022-03-30 06:33:34,269;aequilibrae;INFO ; Closed project on /tmp/1042d65255364bcf8be6396e45d43738 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.118 seconds) .. _sphx_glr_download__auto_examples_plot_path_and_skimming.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_path_and_skimming.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_path_and_skimming.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_