.. 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-21 .. code-block:: Python # Imports 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 23-27 .. code-block:: Python # We create an empty project on an arbitrary folder fldr = join(gettempdir(), uuid4().hex) .. GENERATED FROM PYTHON SOURCE LINES 28-29 Let's use the Nauru example project for display .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python project = create_example(fldr, "nauru") .. GENERATED FROM PYTHON SOURCE LINES 32-33 We grab all the links data as a geopandas GeoDataFrame so we can process it easier .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: Python links = project.network.links.data nodes = project.network.nodes.data .. GENERATED FROM PYTHON SOURCE LINES 37-38 And if you want to take a quick look in your GeoDataFrames, you can plot it! .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python # links.plot() .. GENERATED FROM PYTHON SOURCE LINES 42-43 We create our Folium layers .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python 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] .. GENERATED FROM PYTHON SOURCE LINES 52-54 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 .. GENERATED FROM PYTHON SOURCE LINES 54-99 .. code-block:: Python 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 100-101 We get the center of the region we are working with some SQL magic .. GENERATED FROM PYTHON SOURCE LINES 101-105 .. 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 106-107 We create the map .. GENERATED FROM PYTHON SOURCE LINES 107-117 .. code-block:: Python 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 118-119 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.752 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_display.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_