Components =========== An AequilibraE project helds geometric information that can be accessed by the user in three different classes: ``Links``, ``Nodes``, and ``Zones``. We'll first cover these classes, and then we'll go over the project components without geo-spatial information. .. testsetup:: from pathlib import Path from tempfile import TemporaryDirectory from aequilibrae.examples import create_example _component_docs_tmp = TemporaryDirectory() my_folder_path = Path(_component_docs_tmp.name) project_path = my_folder_path / "project" ``project.tables.links`` ------------------------- This method allows you to access the API resources to manipulate the 'links' table. Each item in the 'links' table is an immutable ``LinkRecord``. .. code-block:: python >>> from shapely.geometry import LineString >>> project = create_example(project_path, "coquimbo") >>> project_links = project.tables.links # Let's add a new field to our 'links' table >>> project_links.fields.add("my_field", "This is an example", "TEXT") # Let's add a new link to our project >>> new_link_id = project_links.insert(modes="bctw", geometry=LineString([(-71.304754, -29.955233), (-71.304863, -29.954049)])) # The 'links' table has three fields which cannot be empty (i.e. with `NULL` values): # `link_id`, `direction`, and `modes`. `insert()` generates a `link_id`, defaults # direction to 0, and derives endpoint nodes from the geometry. Thus, the modes # information must be provided. # To delete one link from the project, you can use the following >>> project_links.delete(21332) # The `copy` function creates a copy of a specified link and returns its ID. # It is very helpful when you want to split a link. # You can check this in one of the usage examples. >>> link_copy_id = project_links.copy(10972) .. admonition:: References * :ref:`modifications_on_links_layer` .. seealso:: * :func:`aequilibrae.project.network.links.Links` Class documentation * :ref:`project_from_link_layer` Usage example * :ref:`editing_network_splitting_link` Usage example ``project.tables.nodes`` ------------------------- This method allows you to access the API resources to manipulate the 'nodes' table. Each item in the 'nodes' table is an immutable ``NodeRecord``. .. code-block:: python >>> from shapely.geometry import Point >>> project_nodes = project.tables.nodes # To get one node record >>> node = project_nodes.get(10070) # We can check the existing fields for each node in the 'nodes' table >>> project_nodes.columns ('node_id', 'is_centroid', 'modes', 'link_types', 'geometry', 'osm_id', 'other_attributes') # Let's renumber this node >>> project_nodes.renumber(node_id=10070, new_id=1000) # A node can also be used to add a special generator # `new_centroid` inserts a centroid and returns its ID. # A geometry is required for a new centroid # This centroid corresponds to the Port of Coquimbo! >>> centroid_id = project_nodes.new_centroid(2000, geometry=Point(-71.32, -29.94)) >>> centroid = project_nodes.get(centroid_id) # As this centroid is not associated with a zone, we must tell AequilibraE the initial area around # the centroid to look for candidate nodes to which the centroid can connect. >>> project_nodes.connect_mode(node_id=centroid_id, area=centroid.geometry.buffer(0.01), mode_id="c") # Last but not less important, you can check your project nodes # `project_nodes.data` returns a geopandas GeoDataFrame. >>> nodes_data = project_nodes.data >>> # or if you want to check the coordinate of each node in the shape of >>> # a Pandas DataFrame >>> coords = project_nodes.lonlat >>> coords.head(3) # doctest: +NORMALIZE_WHITESPACE node_id lon lat 0 10037 -71.315117 -29.996804 1 10064 -71.336604 -29.949050 2 10065 -71.336517 -29.949062 .. admonition:: References * :ref:`modifications_on_nodes_layer` .. seealso:: * :func:`aequilibrae.project.network.nodes.Nodes` Class documentation * :ref:`editing_network_nodes` Usage example .. _project_zones: ``project.tables.zones`` ------------------------- This method allows you to access the API resources to manipulate the 'zones' table. Each item in the 'zones' table is an immutable ``ZoneRecord``. .. code-block:: python >>> from shapely.geometry import Point, Polygon >>> project_zones = project.tables.zones # Let's start this example by adding a new field to the 'zones' table >>> project_zones.fields.add("parking_spots", "Number of public parking spots", "INTEGER") # We can check if the new field was indeed created >>> sorted(project_zones.fields.all_fields()) # doctest: +ELLIPSIS ['area', 'employment', 'geometry', 'name', 'parking_spots', 'population', 'zone_id'] # Now let's get a zone and modify it >>> zone = project_zones.get(40) # By disconnecting the transit mode >>> project_zones.disconnect_mode("t") # Connecting the bicycle mode >>> project_zones.connect_mode("b") # And adding the number of public parking spots in the field we just created >>> project_zones.update(zone.zone_id, parking_spots=30) # The changes connecting / disconnecting modes reflect in the zone centroids # and can be seen in the 'nodes' table. # To return a dictionary with all zone records in the model >>> zones_by_id = {zone.zone_id: zone for zone in project_zones} # If you want to delete a zone >>> project_zones.delete(38) # Or to add a new one >>> zone_extent = Polygon([(-71.3325, -29.9473), (-71.3283, -29.9473), (-71.3283, -29.9539), (-71.3325, -29.9539)]) >>> new_zone_id = project_zones.insert(zone_id=38, geometry=zone_extent) # We can add a centroid to the zone we just created by specifying its location or # pass `None` to use the geometric center of the zone >>> project_zones.add_centroid(new_zone_id, Point(-71.33, -29.95)) # Finally, to return a geopandas GeoDataFrame with the project zones >>> zones = project_zones.data # To get a Shapely Polygon or Multipolygon with the entire zoning coverage >>> boundaries = project_zones.coverage() # And to get the nearest zone to a given geometry >>> project_zones.get_closest_zone(Point(-71.3336, -29.9490)) 57 >>> project.close() .. seealso:: * :func:`aequilibrae.project.network.zones.Zones` Class documentation * :ref:`create_zones` Usage example ``project.about`` ----------------- This table object provides access to project metadata. Use the standard table API to add metadata records or update existing values. .. doctest:: >>> project = create_example(my_folder_path / "about") >>> project.about.insert(infoname="my_new_field") 'my_new_field' >>> project.about.update("my_new_field", infovalue="add some useful information about the field") # We can update data in an existing field >>> project.about.update("author", infovalue="Your Name") # Metadata is available as table data >>> "my_new_field" in project.about.data.infoname.values True >>> project.close() .. seealso:: * :func:`aequilibrae.project.about.About` Class documentation * :ref:`tables_about` Table documentation ``project.FieldEditor`` ----------------------- The ``FieldEditor`` allows the user to edit the project data tables, and it has two different purposes: * Managing data tables by adding fields * Editing the tables' metadata (aka the description of each field) This class is directly accessed from within the corresponding module one wants to edit. .. doctest:: >>> project = create_example(my_folder_path / "field_editor", "nauru") # We'll edit the fields in the 'nodes' table >>> node_fields = project.tables.nodes.fields # To add a new field to the 'nodes' table >>> node_fields.add("my_new_field", "this is an example of AequilibraE's functionalities", "TEXT") # To edit the description of a field >>> node_fields.set_description("osm_id", "number of the osm node_id") # Or just to access the description of a field >>> node_fields.get_description("modes") 'Modes connected to the node' # One can also check all the fields in the 'nodes' table. >>> node_fields.all_fields() # doctest: +ELLIPSIS [..., 'my_new_field'] # Add two directional columns that share one description >>> link_fields = project.tables.links.fields >>> link_fields.add("sample_cost", "Cost of traversing the link", "REAL", directional=True) >>> link_fields.get_description("sample_cost_ab") 'Cost of traversing the link' >>> link_fields.get_description("sample_cost_ba") 'Cost of traversing the link' # Add several fields and edit several descriptions >>> link_fields.add_many([ ... ("survey_note", "Survey note", "TEXT"), ... ("survey_year", "Year of survey", "INTEGER"), ... ]) >>> link_fields.set_descriptions([ ... ("sample_cost_*", "Cost per vehicle"), ... ("survey_note", "Note from the field survey"), ... ]) >>> project.close() Field additions and description edits take effect immediately. All field descriptions are kept in the table 'attributes_documentation'. To refer to a shared directional description, use a directional column name or ``name_*``. The base name also works when there is no unsuffixed column of that name. Each ``add_many()`` or ``set_descriptions()`` call applies the entire batch in one transaction. .. seealso:: * :func:`aequilibrae.project.field_editor.FieldEditor` Class documentation ``project.log`` --------------- Every AequilibraE project contains a log file that holds information on all the project procedures. It is possible to access the log file contents, as presented in the next code block. .. doctest:: >>> project = create_example(my_folder_path / "log", "nauru") >>> project_log = project.log() # Returns a list with all entries in the log file. >>> print(project_log.contents()) # doctest: +ELLIPSIS ['2021-01-01 15:52:03,945;aequilibrae;INFO ; Created project on D:/release/Sample models/nauru', ...] # If your project's log is getting cluttered, it is possible to clear it. # Use this option wisely once the deletion of data in the log file can't be undone. >>> project_log.clear() >>> project.close() .. seealso:: * :meth:`aequilibrae.Project.log` Project log access * :ref:`useful-log-tips` Usage example ``project.matrices`` -------------------- This manager provides access to all the matrix files available in the model. Matrices are identified by their filenames, and their metadata is stored in the OMX files. ``get_matrix()`` returns a lazy ``MatrixStore`` by default, so OMX data can be inspected without loading every matrix into memory. Pass ``subset`` to load the selected matrices as an in-memory ``AequilibraeMatrix`` for computation: .. code-block:: python project = create_example(my_folder_path / "matrices") matrices = project.matrices with matrices.get_matrix("demand_mc.omx") as store: matrix_names = list(store) demand = matrices.get_matrix("demand_mc.omx", subset=["car", "trucks"]) If ``index_name`` is omitted, the only OMX index is selected automatically. When an OMX file has multiple indices, ``main_index`` is preferred if present, otherwise, provide ``index_name`` explicitly. .. doctest:: >>> project = create_example(my_folder_path / "matrices") >>> matrices = project.matrices # One can also check all the project matrices as a Dictionary >>> matrices.get_available_matrices_and_metadata() # doctest: +SKIP # We can add a new matrix >>> demand = matrices.get_matrix("demand_mc.omx", subset=["car", "trucks"]) >>> matrices.save_matrix("demand_copy.omx", demand) # If you're unsure if you have a matrix in your project, you can check if it exists. # This function will return `True` or `False`. >>> matrices.file_exists("demand_copy.omx") True # To delete a matrix from the project, we can delete the file directly >>> (matrices.folder / "demand_copy.omx").unlink() # Or open a matrix file to edit its metadata >>> with matrices.get_matrix("demand_mc.omx", mode="r+") as matrix: ... matrix.metadata["description"] = "Multi-class demand" >>> project.close() .. seealso:: * :attr:`aequilibrae.Project.matrices` Project matrix manager ``project.tables.link_types`` ------------------------------ This method allows you to access the API resources to manipulate the 'link_types' table. Each item in the 'link_types' table is an immutable ``LinkTypeRecord``. .. doctest:: >>> project = create_example(my_folder_path / "link_types", "coquimbo") >>> link_types = project.tables.link_types # Create a new LinkType with ID 'A' >>> link_types.insert(link_type_id="A", link_type="Arterial") 'A' # We can update information for the LinkType we just created >>> link_types.update("A", description="This is a description", speed=35) # To create a new field in the 'link_types' table, you can call the function `fields` # to return a FieldEditor instance, which can be edited >>> link_types.fields.add("my_new_field", "this is an example of AequilibraE's functionalities", "TEXT") # You can also remove a LinkType from a project using its `link_type_id` >>> link_types.delete("A") # To collect all LinkType records by their IDs >>> link_types_by_id = {link_type.link_type_id: link_type for link_type in link_types} # There are two ways to get a LinkType from the 'link_types' table # using the `link_type_id` >>> get_link = link_types.get("p") # or using the `link_type` >>> get_link = link_types.get_by_name("primary") >>> project.close() .. seealso:: * :func:`aequilibrae.project.network.link_types.LinkTypes` Class documentation * :ref:`tables_link_types` Table documentation ``project.tables.modes`` ------------------------- This method allows you to access the API resources to manipulate the 'modes' table. Each item in 'modes' table is an immutable ``ModeRecord``. .. doctest:: >>> project = create_example(my_folder_path / "modes", "coquimbo") >>> modes = project.tables.modes # We create a new mode >>> modes.insert(mode_id="k", mode_name="flying_car") 'k' # We can continue editing the mode after adding it >>> modes.update("k", description="Like the one in the cartoons") # You can also remove a Mode from a project using its ``mode_id`` >>> modes.delete("k") # To collect all Mode records by their IDs >>> modes_by_id = {mode.mode_id: mode for mode in modes} # There are two ways to get a Mode from the 'modes' table # using the ``mode_id`` >>> get_mode = modes.get("c") # or using the ``mode_name`` >>> get_mode = modes.get_by_name("car") >>> project.close() .. seealso:: * :func:`aequilibrae.project.network.modes.Modes` Class documentation * :ref:`tables_modes` Table documentation ``project.tables.periods`` --------------------------- This method allows you to access the API resources to manipulate the 'periods' table. Each item in the 'periods' table is a ``Period`` object. .. doctest:: >>> project = create_example(my_folder_path / "periods", "coquimbo") >>> periods = project.tables.periods # Let's add a new field to our 'periods' table >>> periods.fields.add("my_field", "This is field description", "TEXT") # To see all periods data as a Pandas' DataFrame >>> all_periods = periods.data # To add a new period >>> new_period_id = periods.new_period(2, 21600, 43200, "6AM to noon") # We can update the new period with a value for the new field >>> periods.update(new_period_id, my_field="hello world") # It is also possible to renumber a period >>> periods.renumber(new_period_id, 9) # And check the existing data fields for each period >>> periods.columns ('period_id', 'period_start', 'period_end', 'period_description', 'my_field') >>> project.close() .. seealso:: * :func:`aequilibrae.project.network.periods.Periods` Class documentation * :ref:`tables_period` Table documentation