.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/visualization/plot_display.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_visualization_plot_display.py: .. _explore_network_on_notebook: Exploring the network on a notebook =================================== In this example, we show how to use Folium to plot a network for different modes. We will need Folium for this example, and we will focus on creating a layer for each mode in the network, a layer for all links and a layer for all nodes. .. GENERATED FROM PYTHON SOURCE LINES 13-14 Imports .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae.utils.create_example import create_example import folium .. GENERATED FROM PYTHON SOURCE LINES 22-23 We create an empty project on an arbitrary folder .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python fldr = join(gettempdir(), uuid4().hex) # Let's use the Nauru example project for display project = create_example(fldr, "nauru") .. GENERATED FROM PYTHON SOURCE LINES 29-30 We grab all the links data as a Pandas dataframe so we can process it easier .. GENERATED FROM PYTHON SOURCE LINES 30-90 .. 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") car = folium.FeatureGroup("Car") walk = folium.FeatureGroup("Walk") bike = folium.FeatureGroup("Bike") transit = folium.FeatureGroup("Transit") layers = [network_links, network_nodes, car, walk, bike, transit] # 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="gray", weight=2 ).add_to(network_links) if "w" in row.modes: _ = folium.vector_layers.PolyLine( points, popup=f"link_id: {row.link_id}", tooltip=f"{row.modes}", color="green", weight=4 ).add_to(walk) if "b" in row.modes: _ = folium.vector_layers.PolyLine( points, popup=f"link_id: {row.link_id}", tooltip=f"{row.modes}", color="green", weight=4 ).add_to(bike) if "c" in row.modes: _ = folium.vector_layers.PolyLine( points, popup=f"link_id: {row.link_id}", tooltip=f"{row.modes}", color="red", weight=4 ).add_to(car) if "t" in row.modes: _ = folium.vector_layers.PolyLine( points, popup=f"link_id: {row.link_id}", tooltip=f"{row.modes}", color="yellow", weight=4 ).add_to(transit) # 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="black", radius=5, fill=True, fillColor="black", fillOpacity=1.0, ).add_to(network_nodes) .. GENERATED FROM PYTHON SOURCE LINES 91-92 We get the center of the region we are working with some SQL magic .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. 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 97-110 .. code-block:: Python # We create the map map_osm = folium.Map(location=[lat, long], zoom_start=14) # add all layers for layer in layers: layer.add_to(map_osm) # And Add layer control before we display it folium.LayerControl().add_to(map_osm) map_osm .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


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