.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_import_from_gmns.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_import_from_gmns.py: Importing network from GMNS =========================== In this example, we import a simple network in GMNS format. The source files of this network is publicly available in the GMNS GitHub repository itself. Here's the repository: https://github.com/zephyr-data-specs/GMNS .. GENERATED FROM PYTHON SOURCE LINES 11-12 # Imports .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: python 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 .. GENERATED FROM PYTHON SOURCE LINES 20-21 We load the example file from the GMNS GitHub repository .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: python link_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/development/Small_Network_Examples/Arlington_Signals/link.csv" node_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/development/Small_Network_Examples/Arlington_Signals/node.csv" use_group_file = "https://raw.githubusercontent.com/zephyr-data-specs/GMNS/development/Small_Network_Examples/Arlington_Signals/use_group.csv" .. GENERATED FROM PYTHON SOURCE LINES 26-27 We create the example project inside our temp folder .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: python fldr = join(gettempdir(), uuid4().hex) project = Project() project.new(fldr) .. GENERATED FROM PYTHON SOURCE LINES 33-36 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. .. GENERATED FROM PYTHON SOURCE LINES 36-50 .. code-block:: python 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": "hoospital 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() .. GENERATED FROM PYTHON SOURCE LINES 51-54 As it is specified in 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: .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: python project.network.create_from_gmns( link_file_path=link_file, node_file_path=node_file, use_group_path=use_group_file, srid=32619 ) .. GENERATED FROM PYTHON SOURCE LINES 59-62 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: https://github.com/zephyr-data-specs/GMNS/blob/development/Small_Network_Examples/Arlington_Signals/README.md .. GENERATED FROM PYTHON SOURCE LINES 62-98 .. code-block:: python 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"link_id: {row.link_id}", 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"link_id: {row.node_id}", tooltip=f"{row.modes}", color="red", radius=5, fill=True, fillColor="red", fillOpacity=1.0, ).add_to(network_nodes) .. GENERATED FROM PYTHON SOURCE LINES 99-100 We get the center of the region .. GENERATED FROM PYTHON SOURCE LINES 100-104 .. code-block:: python curr = project.conn.cursor() curr.execute("select avg(xmin), avg(ymin) from idx_links_geometry") long, lat = curr.fetchone() .. GENERATED FROM PYTHON SOURCE LINES 105-118 .. code-block:: python # 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 .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


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