Create Elements and add them to the Moma tree

For this example we want to create a MoMa tree like in the following image.

../_images/example_moma_tree.png

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)

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 and its owner. We create the following elements.

name

owner

Augsburg

current_user

InnerCity

current_user

Area1

current_user

model = ni.create_element("Augsburg", owner)
unit_inner_city = ni.create_element("InnerCity", owner)
unit_area_1 = ni.create_element("Area1", owner)

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)

# Elements and Links need an owner, we use the public current user
owner = ni.get_current_gst_user()

model = ni.create_element("Augsburg", owner)
unit_inner_city = ni.create_element("InnerCity", owner)
unit_area_1 = ni.create_element("Area1", owner)
# 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)