.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_display.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_display.py: Exploring the network on a notebook =================================== On 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 12-13 **What we want is a map that looks a little like this** .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: python from PIL import Image import matplotlib.pyplot as plt img = Image.open('plot_network_image.png') plt.imshow(img) .. image:: /_auto_examples/images/sphx_glr_plot_display_001.png :alt: plot display :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 22-23 # Imports .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. 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 30-31 We create an empty project on an arbitrary folder .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. 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 37-38 We grab all the links data as a Pandas dataframe so we can process it easier .. GENERATED FROM PYTHON SOURCE LINES 38-86 .. 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.to_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 87-88 We get the center of the region we are working with some SQL magic .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. 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 93-106 .. 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 107-107 .. code-block:: python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 13.036 seconds) .. _sphx_glr_download__auto_examples_plot_display.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_display.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_display.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_