Note
Go to the end to download the full example code.
Create project from a link layer#
In 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.
References
See also
Several functions, methods, classes and modules are used in this example:
aequilibrae.project.network.links()aequilibrae.project.network.nodes()aequilibrae.project.network.modes()aequilibrae.project.network.link_types()
# Imports
import urllib.request
from os.path import join
from string import ascii_lowercase
from tempfile import gettempdir
from uuid import uuid4
import geopandas as gpd
import pandas as pd
from shapely.wkt import loads as load_wkt
from aequilibrae import Project
# We create an empty project on an arbitrary folder
fldr = join(gettempdir(), uuid4().hex)
project = Project.create(fldr)
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
dest_path = join(fldr, "queluz.csv")
urllib.request.urlretrieve("https://aequilibrae.com/data/queluz.csv", dest_path)
df = pd.read_csv(dest_path)
df = gpd.GeoDataFrame(df.drop(columns=["fid", "WKT"]), geometry=gpd.GeoSeries.from_wkt(df["WKT"]))
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:
link_types = df.link_type.unique()
And the existing link types are
lt = project.tables.link_types
existing_types = [link_type.link_type for link_type in lt]
We could also get it directly from the project database
# with project.db_connection as conn:
# existing_types = [x[0] for x in conn.execute('Select link_type from link_types')]
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.
types_to_add = [ltype for ltype in link_types if ltype not in existing_types]
for i, ltype in enumerate(types_to_add):
lt.insert(
link_type_id=ascii_lowercase[i],
link_type=ltype,
# description='Your custom description here if you have one',
)
We need to use a similar process for modes
md = project.tables.modes
existing_modes = {mode.mode_id: mode.mode_name for mode in md}
Now let’s see the modes we have in the network that we DON’T have already in the model.
We get all the unique mode combinations and merge them 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!
Now let’s add any new mode to the project
modes_to_add = [mode for mode in all_modes if mode not in existing_modes]
for mode_id in modes_to_add:
# You would need to figure out the right name for each one, but this will do
md.insert(mode_id=mode_id, mode_name=f"Mode_from_original_data_{mode_id}")
# description='Your custom description here if you have one'
We cannot use the existing link_id, so we create a new field to not loose this information
links = project.tables.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 can now add all links to the project!
links.insert_from(df.assign(source_id=df["link_id"]))
[15, 16, 17, 18, 22, 23, 24, 25, 26, 27, 29, 32, 33, 34, 35, 36, 76, 77, 97, 98, 99, 100, 101, 102, 103, 104, 109, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 131, 132, 133, 146, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 204, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 440, 441, 443, 503, 504, 505, 522, 523, 524, 525, 526, 527, 528, 529, 530, 534, 539, 540, 541, 561, 562, 563, 564, 565, 569, 570, 571, 572, 573, 649, 684, 685, 688, 689, 690, 691, 692, 693, 694, 695, 696, 699, 700, 701, 702, 755, 756, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 987, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1028, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1100]
We grab all the links data as a geopandas GeoDataFrame so we can process it easier
links = project.tables.links.data
Let’s plot our network!
links.explore(color="blue", style_kwds={"weight": 2}, tooltip="link_type")
Total running time of the script: (0 minutes 0.565 seconds)