Importing network from GMNS#

In this example, we import a simple network in GMNS format. The source files of this network are publicly available in the GMNS GitHub repository itself. Here’s the repository: zephyr-data-specs/GMNS

Imports

from uuid import uuid4
from os.path import join
from tempfile import gettempdir
from aequilibrae.project import Project
from aequilibrae.parameters import Parameters
import folium

We load the example file from the GMNS GitHub repository

link_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/develop/examples/Arlington_Signals/link.csv"
node_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/develop/examples/Arlington_Signals/node.csv"
use_group_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/develop/examples/Arlington_Signals/use_group.csv"

We create the example project inside our temp folder

fldr = join(gettempdir(), uuid4().hex)

project = Project()
project.new(fldr)

In this cell, we modify the AequilibraE parameters.yml file so it contains additional fields to be read in the GMNS link and/or node tables. Remember to always keep the “required” key set to False, since we are adding a non-required field.

new_link_fields = {
    "bridge": {"description": "bridge flag", "type": "text", "required": False},
    "tunnel": {"description": "tunnel flag", "type": "text", "required": False},
}
new_node_fields = {
    "port": {"description": "port flag", "type": "text", "required": False},
    "hospital": {"description": "hospital flag", "type": "text", "required": False},
}

par = Parameters()
par.parameters["network"]["gmns"]["link"]["fields"].update(new_link_fields)
par.parameters["network"]["gmns"]["node"]["fields"].update(new_node_fields)
par.write_back()

As it is specified that the geometries are in the coordinate system EPSG:32619, which is different than the system supported by AequilibraE (EPSG:4326), we inform the srid in the method call:

project.network.create_from_gmns(
    link_file_path=link_file, node_file_path=node_file, use_group_path=use_group_file, srid=32619
)
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/aequilibrae/project/network/gmns_builder.py:199: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[-71.15427901 -71.15136675 -71.15133733 -71.1551403  -71.15469925
 -71.15315227 -71.15214063 -71.15219271 -71.15159845 -71.15143163
 -71.15498211 -71.15514462 -71.15462884 -71.15464758 -71.15317057
 -71.15288507 -71.15323051 -71.15342915 -71.15199735 -71.15216062]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  self.node_df.loc[:, "x_coord"] = np.around(lons, decimals=10)
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/aequilibrae/project/network/gmns_builder.py:200: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[42.41718801 42.41661235 42.41468574 42.41394787 42.41597338 42.41551617
 42.4150759  42.41411134 42.41663501 42.41637699 42.41394184 42.41407387
 42.41604675 42.4158843  42.41569593 42.41552119 42.41531658 42.41543892
 42.41515063 42.41494944]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  self.node_df.loc[:, "y_coord"] = np.around(lats, decimals=10)
Fields not imported from node table: wkt_coord. If you want them to be imported, please modify the parameters.yml file.

Now, let’s plot a map. This map can be compared with the images of the README.md file located in this example repository on GitHub: zephyr-data-specs/GMNS

links = project.network.links.data
nodes = project.network.nodes.data

# We create our Folium layers
network_links = folium.FeatureGroup("links")
network_nodes = folium.FeatureGroup("nodes")
layers = [network_links, network_nodes]

# We do some Python magic to transform this dataset into the format required by Folium
# We are only getting link_id and link_type into the map, but we could get other pieces of info as well
for i, row in links.iterrows():
    points = row.geometry.wkt.replace("LINESTRING ", "").replace("(", "").replace(")", "").split(", ")
    points = "[[" + "],[".join([p.replace(" ", ", ") for p in points]) + "]]"
    # we need to take from x/y to lat/long
    points = [[x[1], x[0]] for x in eval(points)]

    _ = folium.vector_layers.PolyLine(
        points, popup=f"<b>link_id: {row.link_id}</b>", tooltip=f"{row.modes}", color="black", weight=2
    ).add_to(network_links)

# And now we get the nodes

for i, row in nodes.iterrows():
    point = (row.geometry.y, row.geometry.x)

    _ = folium.vector_layers.CircleMarker(
        point,
        popup=f"<b>link_id: {row.node_id}</b>",
        tooltip=f"{row.modes}",
        color="red",
        radius=5,
        fill=True,
        fillColor="red",
        fillOpacity=1.0,
    ).add_to(network_nodes)

We get the center of the region

curr = project.conn.cursor()
curr.execute("select avg(xmin), avg(ymin) from idx_links_geometry")
long, lat = curr.fetchone()

We create the map

map_gmns = folium.Map(location=[lat, long], zoom_start=17)

# Add all layers
for layer in layers:
    layer.add_to(map_gmns)

# And Add layer control before we display it
folium.LayerControl().add_to(map_gmns)
map_gmns
Make this Notebook Trusted to load map: File -> Trust Notebook


project.close()

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

Gallery generated by Sphinx-Gallery