.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_from_layer.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_layer.py: Project from a link layer ========================= On this example we show how to create an empty project and populate it with a network coming from a link layer we load from a text file. It can easily be replaced with a different form of loading the data (GeoPandas, for example) We use Folium to visualize the resulting network .. GENERATED FROM PYTHON SOURCE LINES 13-14 # Imports .. GENERATED FROM PYTHON SOURCE LINES 14-25 .. code-block:: python from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae import Project from shapely.wkt import loads as load_wkt import pandas as pd import folium import requests import urllib.request from string import ascii_lowercase .. GENERATED FROM PYTHON SOURCE LINES 26-27 We create an empty project on an arbitrary folder .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: python fldr = join(gettempdir(), uuid4().hex) project = Project() project.new(fldr) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/parameters.py:53: UserWarning: No pre-existing parameter file exists for this project. Will use default warn('No pre-existing parameter file exists for this project. Will use default') .. GENERATED FROM PYTHON SOURCE LINES 31-34 Now we obtain the link data for our example (in this case from a link layer we will download from the AequilibraE website) With data, we load it on Pandas .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: python dest_path = join(fldr, "queluz.csv") urllib.request.urlretrieve('https://aequilibrae.com/data/queluz.csv', dest_path) df = pd.read_csv(dest_path) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Let's see if we have to add new link_types to the model before we add links The links we have in the data are: .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: python link_types = df.link_type.unique() .. GENERATED FROM PYTHON SOURCE LINES 45-46 And the existing link types are .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: python lt = project.network.link_types lt_dict = lt.all_types() existing_types = [ltype.link_type for ltype in lt_dict.values()] # We could also get it directly from the project database # existing_types = [x[0] for x in project.conn.execute('Select link_type from link_types')] .. GENERATED FROM PYTHON SOURCE LINES 55-59 We add the link types that do not exist yet The trickier part is to choose a unique link type ID for each link type You might want to tailor the link type for your use, but here we get letters in alphabetical order .. GENERATED FROM PYTHON SOURCE LINES 59-67 .. code-block:: python types_to_add = [ltype for ltype in link_types if ltype not in existing_types] for i, ltype in enumerate(types_to_add): new_type = lt.new(ascii_lowercase[i]) new_type.link_type = ltype # new_type.description = 'Your custom description here if you have one' new_type.save() .. GENERATED FROM PYTHON SOURCE LINES 68-69 We need to use a similar process for modes .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: python md = project.network.modes md_dict = md.all_modes() existing_modes = {k: v.mode_name for k, v in md_dict.items()} .. GENERATED FROM PYTHON SOURCE LINES 75-77 Now let's see the modes we have in the network that we DON'T have already in the model .. GENERATED FROM PYTHON SOURCE LINES 77-86 .. code-block:: python # We get all the unique mode combinations and merge into a single string all_variations_string = ''.join(df.modes.unique()) # We then get all the unique modes in that string above all_modes = set(all_variations_string) # This would all fit nicely in a single line of code, btw. Try it! .. GENERATED FROM PYTHON SOURCE LINES 87-88 Now let's add any new mode to the project .. GENERATED FROM PYTHON SOURCE LINES 88-100 .. code-block:: python modes_to_add = [mode for mode in all_modes if mode not in existing_modes] for i, mode_id in enumerate(modes_to_add): new_mode = md.new(mode_id) # You would need to figure out the right name for each one, but this will do new_mode.mode_name = f'Mode_from_original_data_{mode_id}' # new_type.description = 'Your custom description here if you have one' # It is a little different, because you need to add it to the project project.network.modes.add(new_mode) new_mode.save() .. GENERATED FROM PYTHON SOURCE LINES 101-103 We cannot use the existing link_id, so we create a new field to not loose this information .. GENERATED FROM PYTHON SOURCE LINES 103-112 .. code-block:: python links = project.network.links link_data = links.fields # Create the field and add a good description for it link_data.add('source_id', 'link_id from the data source') # We need to refresh the fields so the adding method can see it links.refresh_fields() .. GENERATED FROM PYTHON SOURCE LINES 113-114 # We can now add all links to the project! .. GENERATED FROM PYTHON SOURCE LINES 114-128 .. code-block:: python for idx, record in df.iterrows(): new_link = links.new() # Now let's add all the fields we had new_link.source_id = record.link_id new_link.direction = record.direction new_link.modes = record.modes new_link.link_type = record.link_type new_link.name = record.name new_link.geometry = load_wkt(record.WKT) new_link.save() # # .. GENERATED FROM PYTHON SOURCE LINES 129-130 We grab all the links data as a Pandas dataframe so we can process it easier .. GENERATED FROM PYTHON SOURCE LINES 130-146 .. 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 147-148 We get the center of the region we are working with some SQL magic .. GENERATED FROM PYTHON SOURCE LINES 148-152 .. 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 153-158 .. 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 159-161 .. code-block:: python project.close() # .. GENERATED FROM PYTHON SOURCE LINES 162-163 **Don't know Queluz? Here is a picture of its most impressive urban structure** .. GENERATED FROM PYTHON SOURCE LINES 163-178 .. code-block:: python from PIL import Image import matplotlib.pyplot as plt pic = 'https://upload.wikimedia.org/wikipedia/commons/2/2c/Ponte_Governador_Mario_Covas_01.jpg' pic_local = join(fldr, 'queluz.jpg') headers = {'User-Agent': "AequilibraE (https://aequilibrae.com/; contact@aequilibrae.com) python-library/0.7"} response = requests.get(pic, headers=headers) if response.status_code == 200: with open(pic_local, 'wb') as f: f.write(response.content) img = Image.open(pic_local) plt.imshow(img) .. image-sg:: /_auto_examples/images/sphx_glr_plot_from_layer_001.png :alt: plot from layer :srcset: /_auto_examples/images/sphx_glr_plot_from_layer_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 21.534 seconds) .. _sphx_glr_download__auto_examples_plot_from_layer.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_from_layer.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_from_layer.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_