Create centroid connectors#

We use Coquimbo example to show how we can create centroids and connect them to the existing network efficiently.

We use Folium to visualize the resulting network.

See also

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

# Imports
import folium
from uuid import uuid4
from tempfile import gettempdir
from os.path import join

from aequilibrae.utils.create_example import create_example
# Let's create an arbitrary project folder
fldr = join(gettempdir(), uuid4().hex)

# And create our Coquimbo project
project = create_example(fldr, "coquimbo")

As Coquimbo already has centroids and centroid connectors, we should remove them for the sake of this example.

with project.db_connection as conn:
    conn.execute("DELETE FROM links WHERE name LIKE 'centroid connector%'")
    conn.execute("DELETE FROM nodes WHERE is_centroid=1;")
    conn.commit()

    centroids = conn.execute("SELECT COUNT(node_id) FROM nodes WHERE is_centroid=1;").fetchone()[0]

If you want to make sure your deletion process has worked, you can check the number of centroids!

print("Current number of centroids: ", centroids)
Current number of centroids:  0
zoning = project.zoning

This centroid connector creation is effective because it uses the existing zone layer to create the centroids and connect them to the existing network.

First thing to do is add the centroids to all zones that doesn’t have a centroid at the geographic centroid of the zone, using add_centroids(), which has a robust argument set to True as default. This means that it will automatically move the centroid location around to avoid conflicts with existing nodes.

zoning.add_centroids()
Connecting zones                                  :   0%|          | 0/133 [00:00<?, ?it/s]
Connecting zones                                  :  16%|█▌        | 21/133 [00:00<00:00, 202.49it/s]
Connecting zones                                  :  32%|███▏      | 42/133 [00:00<00:00, 196.56it/s]
Connecting zones                                  :  47%|████▋     | 62/133 [00:00<00:00, 189.81it/s]
Connecting zones                                  :  61%|██████    | 81/133 [00:00<00:00, 185.11it/s]
Connecting zones                                  :  75%|███████▌  | 100/133 [00:00<00:00, 185.07it/s]
Connecting zones                                  :  89%|████████▉ | 119/133 [00:00<00:00, 181.31it/s]

Let’s connect the mode c, that stands for car.

mode = "c"

Then we connect the centroid to the network, by selecting the desired mode, the number of connectors, the allowed link types for connection, and if one wants to allow the connection to links in other zones. By setting limit_to_zone=False, we allow the centroid of one zone to be connected to a link outside the zone itself.

zoning.connect_mode(mode_id=mode, connectors=1, limit_to_zone=False)
Connecting zones                                  :   0%|          | 0/133 [00:00<?, ?it/s]
Connecting zones                                  :   2%|▏         | 2/133 [00:00<00:12, 10.61it/s]
Connecting zones                                  :   5%|▍         | 6/133 [00:00<00:06, 20.66it/s]
Connecting zones                                  :   8%|▊         | 10/133 [00:00<00:04, 25.21it/s]
Connecting zones                                  :  11%|█         | 14/133 [00:00<00:04, 27.68it/s]
Connecting zones                                  :  14%|█▎        | 18/133 [00:00<00:03, 29.10it/s]
Connecting zones                                  :  17%|█▋        | 22/133 [00:00<00:03, 29.80it/s]
Connecting zones                                  :  20%|█▉        | 26/133 [00:00<00:03, 30.77it/s]
Connecting zones                                  :  23%|██▎       | 30/133 [00:01<00:03, 31.15it/s]
Connecting zones                                  :  26%|██▌       | 34/133 [00:01<00:03, 31.10it/s]
Connecting zones                                  :  29%|██▊       | 38/133 [00:01<00:03, 31.06it/s]
Connecting zones                                  :  32%|███▏      | 42/133 [00:01<00:02, 30.98it/s]
Connecting zones                                  :  35%|███▍      | 46/133 [00:01<00:02, 31.07it/s]
Connecting zones                                  :  38%|███▊      | 50/133 [00:01<00:02, 31.27it/s]
Connecting zones                                  :  41%|████      | 54/133 [00:01<00:02, 31.32it/s]
Connecting zones                                  :  44%|████▎     | 58/133 [00:01<00:02, 31.28it/s]
Connecting zones                                  :  47%|████▋     | 62/133 [00:02<00:02, 30.97it/s]
Connecting zones                                  :  50%|████▉     | 66/133 [00:02<00:02, 31.36it/s]
Connecting zones                                  :  53%|█████▎    | 70/133 [00:02<00:01, 31.65it/s]
Connecting zones                                  :  56%|█████▌    | 74/133 [00:02<00:01, 31.97it/s]
Connecting zones                                  :  59%|█████▊    | 78/133 [00:02<00:01, 32.28it/s]
Connecting zones                                  :  62%|██████▏   | 82/133 [00:02<00:01, 32.43it/s]
Connecting zones                                  :  65%|██████▍   | 86/133 [00:02<00:01, 31.88it/s]
Connecting zones                                  :  68%|██████▊   | 90/133 [00:02<00:01, 32.38it/s]
Connecting zones                                  :  71%|███████   | 94/133 [00:03<00:01, 32.16it/s]
Connecting zones                                  :  74%|███████▎  | 98/133 [00:03<00:01, 32.07it/s]
Connecting zones                                  :  77%|███████▋  | 102/133 [00:03<00:00, 31.47it/s]
Connecting zones                                  :  80%|███████▉  | 106/133 [00:03<00:00, 31.16it/s]
Connecting zones                                  :  83%|████████▎ | 110/133 [00:03<00:00, 31.15it/s]
Connecting zones                                  :  86%|████████▌ | 114/133 [00:03<00:00, 31.52it/s]
Connecting zones                                  :  89%|████████▊ | 118/133 [00:03<00:00, 31.62it/s]
Connecting zones                                  :  92%|█████████▏| 122/133 [00:03<00:00, 32.12it/s]
Connecting zones                                  :  95%|█████████▍| 126/133 [00:04<00:00, 31.96it/s]
Connecting zones                                  :  98%|█████████▊| 130/133 [00:04<00:00, 31.76it/s]

It is possible to repeat the process above for a different mode, with different link type, number of connectors and connection allowance, as desired.

Finally, let’s plot our data!

links = project.network.links.data
centroids = links[links["link_type"] == "centroid_connector"]
links = links[links["link_type"] != "centroid_connector"]

nodes = project.network.nodes.data
nodes = nodes[nodes["is_centroid"] == 1]
map = folium.Map(location=[-29.9568, -71.3456], zoom_start=14)
zoning.data.explore(m=map, color="blue", style_kwds={"fillOpacity": 0.05}, name="zones")
centroids.explore(m=map, color="black", style_kwds={"weight": 2.5}, name="centroid_connector")
links.explore(m=map, color="gray", style_kwds={"weight": 1}, name="links")
nodes.explore(m=map, color="red", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="centroid")

folium.LayerControl().add_to(map)

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


project.close()
This project at /tmp/68459fe8061f4b399b9d87c3d5ac8a1b is already closed

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

Gallery generated by Sphinx-Gallery