Create Elements and add them to the Moma tree¶
For this example we want to create a MoMa tree like in the following image.
Aussumptions
We assume that we already have two feature classes with the features like above and that the MoMa tree is currently empty.
Storing some needed information
We store the feature class and features information.
feature_classes = ni.list_feature_classes()
inner_city_feature_class = next((fc for fc in feature_classes
if fc.name == "innercity"))
area_1_feature_class = next((fc for fc in feature_classes
if fc.name == "area1"))
inner_city_features = ni.list_features(inner_city_feature_class)
area_1_features = ni.list_features(area_1_feature_class)
We also store the information of the MoMa levels as follows.
# levels are sorted from lowest depth to heighest depth
#
# in a standard installation:
# levels[0] == Model
# levels[1] == Unit
# levels[2] == Element
levels = ni.list_levels()
assert len(levels) == 3
model_level = levels[0]
unit_level = levels[1]
Lastly we get the current user as owner of our MoMa elements and links.
owner = ni.get_current_gst_user()
Creating the elements
To create an element, we need to supply its name, its owner and the level it should belong to. We create the following elements.
name | owner | level |
---|---|---|
Augsburg | current_user | Model |
InnerCity | current_user | Unit |
Area1 | current_user | Unit |
model = ni.create_element("Augsburg", owner, model_level)
unit_inner_city = ni.create_element("InnerCity", owner, unit_level)
unit_area_1 = ni.create_element("Area1", owner, unit_level)
Creating the element links
Now we want to link our new elements into the MoMa tree.
First we link the “Augsburg” element to the root MoMa node.
model_link = ni.create_element_link_adjacency(model, owner)
Afterwards we link the “Area1” and “InnerCity” elements to the “Augsburg” node.
unit_inner_city_link = ni.create_element_link_adjacency(
unit_inner_city, owner, parent_link=model_link)
unit_area_1_link = ni.create_element_link_adjacency(
unit_area_1, owner, parent_link=model_link)
Notice how we used the parent_link
parameter to link “Area1” and
“InnerCity” to “Augsburg”. To link model elements you should leave
parent_link
unset.
Creating the feature links
Finally we want to add our features to the MoMa tree. In this example we
link features from the area1_feature_class
to “Area1” and the features
from the innercity_feature_class
to “InnerCity”.
for f in inner_city_features:
l = ni.create_feature_link_adjacency(
f, owner, parent_link=unit_inner_city_link)
for f in area_1_features:
l = ni.create_feature_link_adjacency(
f, owner, parent_link=unit_area_1_link)
And we are done.
We have created the MoMa tree as in the image at the top.
Full code sample
import GSTPy
# we already have some features in two features classes named
# innercity and area1
feature_classes = ni.list_feature_classes()
inner_city_feature_class = next((fc for fc in feature_classes
if fc.name == "innercity"))
area_1_feature_class = next((fc for fc in feature_classes
if fc.name == "area1"))
inner_city_features = ni.list_features(inner_city_feature_class)
area_1_features = ni.list_features(area_1_feature_class)
# levels are sorted from lowest depth to heighest depth
#
# in a standard installation:
# levels[0] == Model
# levels[1] == Unit
# levels[2] == Element
levels = ni.list_levels()
assert len(levels) == 3
model_level = levels[0]
unit_level = levels[1]
# Elements and Links need an owner, we use the public current user
owner = ni.get_current_gst_user()
model = ni.create_element("Augsburg", owner, model_level)
unit_inner_city = ni.create_element("InnerCity", owner, unit_level)
unit_area_1 = ni.create_element("Area1", owner, unit_level)
# link the model to the tree root by leaving parent_link unset
model_link = ni.create_element_link_adjacency(model, owner)
# link units to model
unit_inner_city_link = ni.create_element_link_adjacency(
unit_inner_city, owner, parent_link=model_link)
unit_area_1_link = ni.create_element_link_adjacency(
unit_area_1, owner, parent_link=model_link)
# finally add feature_links to the respective units
for f in inner_city_features:
l = ni.create_feature_link_adjacency(
f, owner, parent_link=unit_inner_city_link)
for f in area_1_features:
l = ni.create_feature_link_adjacency(
f, owner, parent_link=unit_area_1_link)