.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_from_osm.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_from_osm.py: Project from Open-Street Maps ============================= On this example we show how to create an empty project and populate with a network from Open-Street maps But this time we will use Folium to visualize the network .. GENERATED FROM PYTHON SOURCE LINES 11-12 # Imports .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: python from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae import Project import folium .. GENERATED FROM PYTHON SOURCE LINES 19-20 We create an empty project on an arbitrary folder .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: python fldr = join(gettempdir(), uuid4().hex) project = Project() project.new(fldr) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Now we can download the network from any place in the world (as long as you have memory for all the download and data wrangling that will be done) .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: python # We can create from a bounding box # or from a named place. For the sake of this example, we will choose the small nation of Nauru project.network.create_from_osm(place_name="Nauru") .. GENERATED FROM PYTHON SOURCE LINES 32-33 We grab all the links data as a Pandas dataframe so we can process it easier .. GENERATED FROM PYTHON SOURCE LINES 33-50 .. code-block:: python links = project.network.links.data # We create a Folium layer network_links = folium.FeatureGroup("links") # 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)] line = folium.vector_layers.PolyLine( points, popup=f"link_id: {row.link_id}", tooltip=f"{row.link_type}", color="blue", weight=10 ).add_to(network_links) .. GENERATED FROM PYTHON SOURCE LINES 51-52 We get the center of the region we are working with some SQL magic .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. 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 57-62 .. code-block:: python map_osm = folium.Map(location=[lat, long], zoom_start=14) network_links.add_to(map_osm) 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 63-65 .. code-block:: python project.close() .. GENERATED FROM PYTHON SOURCE LINES 66-67 **Don't know Nauru? Here is a map** .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: python from PIL import Image import matplotlib.pyplot as plt img = Image.open("nauru.png") plt.imshow(img) .. image-sg:: /_auto_examples/images/sphx_glr_plot_from_osm_001.png :alt: plot from osm :srcset: /_auto_examples/images/sphx_glr_plot_from_osm_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 19.666 seconds) .. _sphx_glr_download__auto_examples_plot_from_osm.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_from_osm.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_from_osm.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_