.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/path_computation/plot_path_computation.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_path_computation_plot_path_computation.py: .. _example_usage_path_computation: Path computation ================ In this example, we show how to perform path computation for Coquimbo, a city in La Serena Metropolitan Area in Chile. .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.graph` * :func:`aequilibrae.routing.results.path_results` .. GENERATED FROM PYTHON SOURCE LINES 17-18 Imports .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python from os.path import join from tempfile import gettempdir from uuid import uuid4 import geopandas as gpd from aequilibrae.routing import estimate_heuristic_scale from aequilibrae.examples import create_example from aequilibrae.utils import basic_config .. GENERATED FROM PYTHON SOURCE LINES 29-30 We create the example project inside our temp folder .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "coquimbo") .. GENERATED FROM PYTHON SOURCE LINES 35-40 .. code-block:: Python # We'll also apply a basic logging configuration. basic_config() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 41-43 Path Computation ---------------- .. GENERATED FROM PYTHON SOURCE LINES 45-47 Load the network snapshot We get warnings that several fields in the project are filled with ``NaN``s. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python network = project.road.build_network() # This is true, but we won't use those fields. .. GENERATED FROM PYTHON SOURCE LINES 51-52 We build the graph for cars, minimise distance, and skim time and distance. .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python graph = network.build_graph(mode="c", cost="distance", skimming=["travel_time", "distance"]) .. GENERATED FROM PYTHON SOURCE LINES 55-56 we'll also see what modes are available. .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python network.modes .. rst-class:: sphx-glr-script-out .. code-block:: none ('b', 'c', 't', 'w') .. GENERATED FROM PYTHON SOURCE LINES 59-61 Let's create a path results object from the graph and compute a path from node 32343 (near the airport) to 22041 (near Fort Lambert, overlooking Coquimbo Bay). .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python res = graph.compute_path(32343, 22041) .. GENERATED FROM PYTHON SOURCE LINES 64-71 Computing paths directly from the graph is more straightforward, though we could alternatively use the ``PathResults`` class to achieve the same result. from aequilibrae.routing import PathResults res = PathResults(graph, 32343, 22041) ``graph.compute_path`` has already performed this setup above. .. GENERATED FROM PYTHON SOURCE LINES 73-74 We can get the sequence of nodes we traverse .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python res.path_nodes .. rst-class:: sphx-glr-script-out .. code-block:: none array([32343, 79778, 68225, 32487, 63937, 63192, 46510, 32380, 32373, 55817, 55816, 11982, 46516, 75015, 79704, 79785, 78576, 68242, 79144, 78635, 79784, 78748, 79082, 65861, 78343, 21311, 20312, 21308, 78834, 79862, 79450, 63873, 79458, 78986, 78884, 79152, 78645, 78549, 68503, 13380, 13383, 79199, 79745, 79457, 80001, 78217, 78093, 80013, 25130, 80012, 40633, 11010, 11009, 40846, 21827, 80056, 80055, 79481, 79486, 79485, 75142, 11448, 11446, 11445, 67684, 60645, 11447, 11422, 11420, 11421, 13723, 10851, 79462, 26681, 13718, 12079, 79460, 23707, 29778, 75451, 75445, 45342, 39399, 13626, 13627, 45379, 21384, 63812, 40005, 12207, 44243, 44241, 23405, 60002, 27114, 79431, 15148, 15146, 60000, 75486, 55963, 55958, 59043, 59050, 59988, 39402, 59017, 59019, 79398, 75520, 75516, 75512, 75509, 75505, 75511, 63544, 63543, 75510, 75515, 75476, 63539, 30138, 11695, 61061, 30148, 44192, 75556, 79364, 75534, 75552, 75548, 75321, 75532, 14802, 14823, 71435, 65497, 64708, 64709, 64712, 64713, 40374, 40375, 77308, 65518, 75566, 68526, 75573, 41306, 41308, 75619, 75617, 14899, 14875, 38674, 75595, 65067, 65068, 79508, 29452, 44797, 29447, 10065, 44798, 30552, 44783, 44808, 75612, 73617, 79653, 79651, 73620, 73923, 79820, 14864, 69009, 22040, 22041], dtype=uint64) .. GENERATED FROM PYTHON SOURCE LINES 77-79 We can get the physical link IDs and directions we traverse. ``res.path`` contains local graph-link indices instead. .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python res.get_network_links() .. rst-class:: sphx-glr-script-out .. code-block:: none (array([34709, 34710, 34711, 34712, 34713, 34714, 34715, 34716, 34717, 34718, 34719, 34720, 34721, 34722, 3321, 3322, 3323, 3324, 3325, 3326, 3327, 3328, 3329, 3330, 3331, 3332, 2970, 2971, 2969, 19995, 1434, 1435, 1436, 19326, 19327, 19328, 19329, 19330, 33674, 33675, 33676, 33677, 26525, 20765, 20746, 20747, 20748, 20749, 20750, 20751, 20752, 496, 497, 498, 499, 500, 501, 10380, 15408, 553, 552, 633, 634, 635, 630, 631, 632, 623, 624, 625, 626, 471, 5363, 34169, 34170, 34171, 34785, 6466, 6465, 29938, 29939, 29940, 29941, 1446, 1447, 1448, 1449, 1450, 939, 940, 941, 9840, 9841, 26314, 26313, 26312, 26311, 26310, 26309, 26308, 26307, 26306, 26305, 26304, 26303, 26302, 26301, 26300, 34079, 34147, 29962, 26422, 26421, 26420, 765, 764, 763, 762, 761, 760, 736, 10973, 10974, 10975, 725, 10972, 727, 728, 26424, 733, 734, 29899, 20970, 20969, 20968, 20967, 20966, 20965, 20964, 20963, 20962, 9584, 9583, 20981, 21398, 20982, 20983, 20984, 20985, 10030, 10031, 10032, 10033, 10034, 10035, 10036, 64, 65, 21260, 21261, 21262, 21263, 21264, 21265, 21266, 33, 11145, 11146, 71, 72, 34529, 34530, 34531, 28691, 28692, 28693, 3574], dtype=uint64), array([ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int8)) .. GENERATED FROM PYTHON SOURCE LINES 82-83 We can get the mileposts for our sequence of nodes .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python res.milepost .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 0. , 161.94834565, 252.51996291, 390.08508135, 549.82648658, 561.81026027, 581.36871507, 593.21987605, 630.08836889, 1030.57121686, 1052.34444478, 1112.07906484, 1165.81004929, 1267.22602763, 1624.43103234, 1924.50863633, 1972.01917098, 2021.6997169 , 2062.34315111, 2109.67655695, 2155.98823775, 2196.91106309, 2221.69061004, 2249.01761535, 2298.72337036, 2363.21417492, 2375.9615406 , 2392.22488207, 2426.81462733, 2675.27978499, 3632.15818275, 3699.27505758, 3823.65932479, 3956.65040737, 4017.46560312, 4072.01402297, 4129.64308736, 4163.12532905, 4187.96101397, 4224.21499938, 4307.91646806, 4323.79479478, 4458.86701963, 4569.20833913, 4692.25740782, 4930.34266637, 4997.45500599, 5068.23739204, 5120.12897437, 5187.77254139, 5207.89952503, 5327.95612724, 5368.62533167, 5378.38940327, 5385.33334293, 5426.19568418, 5468.3231786 , 5523.97170089, 5548.40671886, 5559.07102378, 5592.3855075 , 5757.7535581 , 5923.39441593, 5950.82694781, 5956.69374937, 5982.88386915, 6047.00805103, 6254.07203583, 6284.94026694, 6297.98296824, 7047.68761904, 7322.48973364, 7410.01354224, 7554.81816989, 7654.30645223, 8000.95440184, 8009.6579379 , 8048.90437039, 8056.1819337 , 8230.32269321, 8476.75016293, 8620.89727688, 9010.45521832, 9274.61196368, 9280.12960244, 9383.74516275, 9496.88846171, 9711.90807592, 10093.92389013, 10097.37343293, 10098.79806269, 10100.74846208, 10158.97660612, 10308.02183308, 11038.25297634, 11043.82678472, 11184.07536528, 11241.90536698, 11251.4005022 , 11507.50220541, 11734.64237287, 11891.6028426 , 11914.05783947, 11931.91664218, 11955.2823231 , 12032.22401165, 12107.39910016, 12119.5125075 , 12136.71574687, 12210.28469894, 12389.41094897, 12417.43348286, 12718.35052704, 12730.00818724, 12843.38553893, 12911.9429086 , 12921.34664177, 13071.40008271, 13080.33320947, 13161.6119503 , 13335.34223859, 13388.53619865, 13412.08272452, 13574.14596808, 13738.18613296, 13798.71647839, 14036.00947087, 14102.56840025, 14197.10918933, 14292.09203161, 14315.76699626, 14434.43917168, 14484.88708304, 14491.84542125, 14851.61908868, 14868.34527227, 14877.57764797, 15028.49457611, 15037.54620347, 15204.94094821, 15215.00009437, 15351.10413143, 15623.06737615, 15672.61684115, 15722.54738786, 15807.97421917, 15901.71819152, 15937.27964123, 16179.52060712, 16191.20903769, 16203.56934613, 16308.11327422, 16479.15664668, 16536.53372128, 16548.12481843, 16559.65540426, 16680.70266884, 16720.69837006, 16796.68692111, 16841.49843813, 16877.97092451, 16889.16327603, 16920.08515864, 16976.52291632, 17076.47607269, 17094.22058834, 17134.27959626, 17183.85951634, 17472.72121764, 17554.85677394, 17659.06248146, 17923.55576674, 17933.3892542 , 17942.66958063, 18164.10699022, 18421.89973826, 18597.64939792, 18599.05175306]) .. GENERATED FROM PYTHON SOURCE LINES 86-92 Additionally, you can also provide ``early_exit=True`` or ``a_star=True`` to `compute_path` to adjust its path-finding behaviour. Providing ``early_exit=True`` allows you to quit the path-finding procedure once it discovers the destination. This setup works better for topographically close origin-destination pairs. However, exiting early may cause subsequent calls to ``update_trace`` to recompute the tree in cases where it typically wouldn't. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python res = graph.compute_path(32343, 22041, early_exit=True) .. GENERATED FROM PYTHON SOURCE LINES 95-99 To guide the search towards the destination, provide ``a_star=True`` to use `A*` with a heuristic. ``update_trace`` reuses finalised paths, or searches again if needed, retaining the algorithm, heuristic, scale and heap. Note that a_star takes precedence over early_exit. Euclidean distance needs node coordinates projected to a suitable local CRS and a scale parameter to transform it into a cost value. .. GENERATED FROM PYTHON SOURCE LINES 99-107 .. code-block:: Python lonlat = network.lonlat points = gpd.GeoSeries(gpd.points_from_xy(lonlat.lon, lonlat.lat), index=lonlat.index, crs=4326) points = points.to_crs(points.estimate_utm_crs()) coordinates = points.get_coordinates() scale = estimate_heuristic_scale(graph, coordinates) res = graph.compute_path(32343, 22041, a_star=True, coordinates=coordinates, heuristic_scale=scale) .. GENERATED FROM PYTHON SOURCE LINES 108-110 If you are using `a_star`, it is possible to use different heuristics to compute the path. By default, a Euclidean heuristic is used, and we can view the available heuristics via: .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python res.get_heuristics() .. rst-class:: sphx-glr-script-out .. code-block:: none ['euclidean', 'haversine'] .. GENERATED FROM PYTHON SOURCE LINES 113-115 To use geographic distance without projecting the coordinates, choose "haversine". Pass the network's longitude/latitude data explicitly to both the scale estimator and the search. .. GENERATED FROM PYTHON SOURCE LINES 115-120 .. code-block:: Python scale = estimate_heuristic_scale(graph, lonlat, heuristic="haversine") res = graph.compute_path( 32343, 22041, a_star=True, heuristic="haversine", coordinates=lonlat, heuristic_scale=scale ) .. GENERATED FROM PYTHON SOURCE LINES 121-126 Suppose you want to adjust the path to the University of La Serena instead of Fort Lambert. It is possible to adjust the existing path computation for this alteration. The following code allows both `early_exit` and `A*` settings to persist when calling ``update_trace``. If you’d like to adjust them for subsequent path re-computations, call ``compute_path`` with the desired settings. Notice that this procedure is much faster when you have large networks and the search for the previous destination happened to include the new destination. .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. code-block:: Python res.update_trace(73131) res.path_nodes .. rst-class:: sphx-glr-script-out .. code-block:: none array([32343, 79778, 68225, 32487, 63937, 63192, 46510, 32380, 32373, 55817, 55816, 11982, 46516, 75015, 79704, 79785, 78576, 68242, 79144, 78635, 79784, 78748, 79082, 65861, 78343, 21311, 20312, 21308, 78834, 79862, 79450, 63873, 79458, 78986, 78884, 79152, 78645, 78549, 68503, 13380, 13383, 79199, 79745, 79457, 80001, 78217, 78093, 80013, 25130, 80012, 40633, 11010, 11009, 40846, 21827, 80056, 80055, 79481, 79486, 79485, 75142, 11448, 11446, 11445, 67684, 60645, 11447, 11422, 11420, 11421, 13723, 10851, 79462, 26681, 13718, 12079, 79460, 23707, 29778, 75451, 75445, 45342, 39399, 13626, 13627, 45379, 21384, 63812, 40005, 12207, 44243, 44241, 23405, 60002, 27114, 79431, 15148, 15146, 60000, 75486, 55963, 55958, 59043, 59050, 59988, 39402, 59017, 59019, 79398, 75520, 75516, 75512, 75509, 75505, 75511, 63544, 63543, 75510, 75515, 75476, 63539, 30138, 11695, 61061, 30148, 44192, 75556, 79364, 75534, 75552, 75548, 75321, 75532, 14802, 14823, 71435, 65497, 64708, 64709, 64712, 64713, 40374, 40375, 77308, 65518, 75566, 68526, 79517, 51754, 77189, 65059, 10093, 65058, 30491, 66966, 66863, 30492, 77190, 77191, 79366, 77417, 79368, 77406, 77421, 77425, 77393, 77398, 53993, 77394, 70959, 77395, 27752, 65293, 73131], dtype=uint64) .. GENERATED FROM PYTHON SOURCE LINES 132-135 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 135-139 .. code-block:: Python links = project.tables.links.data.set_index("link_id") link_ids, directions = res.get_network_links() links = links.loc[link_ids] .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python links.explore(color="blue", style_kwds={"weight": 5}) .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


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