Computing paths over the network

Computing paths over the network#

In this example we show how to use the Polaris Batch Router to compute paths over the network

Imports#

sphinx_gallery_thumbnail_path = “../../examples/working_with_models/routed_path.png”

from pathlib import Path

import folium
import numpy as np

from polaris import Polaris
from polaris.utils.database.db_utils import read_and_close

Data Sources#

Open the demand database for analysis

model_fldr = "/tmp/Grid"
# model_fldr = r"D:\src\argonne\MODELS\Grid"

project = Polaris.from_dir(Path(model_fldr))

# If you have a custom build batch_router.dll you can specify it via this command
# project.set_router_lib(Path('/home/user/bin/batch_router.so'))

router = project.router
/builds/polaris/code/polarislib/polaris/runs/router/batch_router.py:40: UserWarning: Loading scenario into the router. This may take some time
  warnings.warn("Loading scenario into the router. This may take some time")

We can first compute the path between two links The departure is in seconds, and we need to provide both the links and their directions (0 for AB and 1 for BA) If we don’t provide the link directions, they default to 0

path = router.route_links(link_origin=678, link_destination=656, origin_dir=1, destination_dir=0, departure_time=72000)

# Then we can see the total travel time
print(path.travel_time)

# Then we can see the list of links we traversed
print(path.links)

# And the direction we traversed them through
print(path.link_directions)

# And the cumulative travel time at each link
print(path.cumulative_time)
1686.0
[678 677 426 425 127 498 497 496 495 494  35  36  38 297 298 299 300 301
 302 303 304 305  59  57 356 357 358 625 626 627 628 629 630 640 641 642
 643 655 656]
[1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0]
[  52.  104.  160.  216.  265.  330.  395.  440.  485.  530.  568.  597.
  626.  659.  692.  725.  758.  791.  824.  857.  890.  923.  984. 1000.
 1045. 1090. 1135. 1177. 1219. 1261. 1301. 1341. 1381. 1444. 1507. 1570.
 1633. 1686. 1739.]

We can also compute the path between two locations In this case, the link and direction associated with the locations will be used as Origin and destination If we don’t provide a departure time, 8AM (28800) is used

new_path = router.route(90, 740)

We can compute the multi-modal travel time between two locations. We do need to input the mode, however

multimodal_path = router.multimodal(90, 740, 1)
print(f"Multimodal travel time: {multimodal_path.travel_time}")
print(f"Car travel time: {new_path.travel_time}")
Multimodal travel time: 1632.0
Car travel time: 1632.0

And we can plot this path on the network

data = project.network.tables
with read_and_close(project.supply_file, spatial=True) as conn:
    links_layer = data.get("Link", conn).to_crs(4326)
    loc_layer = data.get("Location", conn).to_crs(4326)

loc_layer = loc_layer[loc_layer.location.isin([90, 740])]
path_layer = links_layer.query(f"link in @new_path.links")

centr = path_layer.union_all().centroid

map = links_layer.explore(color="blue", style_kwds={"weight": 2}, tool_tip="link", name="links")
map = path_layer.explore(m=map, color="red", style_kwds={"weight": 5}, tool_tip="link", name="path")

map = loc_layer.explore(
    m=map,
    color="black",
    tool_tip="location",
    style_kwds={"radius": 6, "fillOpacity": 1.0, "fillColor": "black", "fill": True},
    name="Locations",
)
folium.LayerControl().add_to(map)  # Add a layer control button to our map

map
Make this Notebook Trusted to load map: File -> Trust Notebook


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

Gallery generated by Sphinx-Gallery