How to create a table with X features using model builder

333
2
04-25-2023 04:51 AM
LucaSchikowski
New Contributor II

Hi there,

I got a table called "testtabelle" which has only one field "BerechneterWert" (data type: short). 

LucaSchikowski_1-1682423183802.png

Now I want to automatically create a new table with features based on the value of "BerechneterWert". (here: 1 table with 12 features). Is this possible using ModelBuilder? I tried to get this done with a for-iteration but couldnt worked it out.

Thanks in advance for your help!

Greetings from Germany,

Luca

 

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

Let me start by saying that this is not a task for ModelBuilder. ModelBuilder can automate chains of geoprocessing tools. As soon as you need user input (like inserting a feature), it's going to get complicated.

 

This is a job for Python.

input_table = "TestLines"
input_field = "IntegerField1"
output_location = "memory"  # default gdb
output_name = "SomeName"

# create ouput
out_table = arcpy.management.CreateTable(output_location, output_name)

# start inserting into the output table
with arcpy.da.InsertCursor(out_table, ["OID@"]) as i_cursor:
    # open the input table to read the field
    with arcpy.da.SearchCursor(input_table, [input_field]) as s_cursor:
        for row in s_cursor:
            # insert x rows
            x = row[0]
            for y in range(x):
                i_cursor.insertRow([None])
            # stop after the first row of the input table
            break

JohannesLindner_0-1682446689595.png

 

 

Now that that's out of the way: You can do it with ModelBuilder, but it's much more complicated...

There are iterators for the ModelBuilder, but they execute the whole model x times. Because we only want to loop a part of the model (add a feature x times), we need a separate model to do that.

There is no geoprocessing tool that inserts a feature into a table. You could create a script tool and use that, but then you could just do it in Python completely. So we're using the Append tool.

For that to work, you need a template table with exactly one feature. The submodel will append that template table x times to the output table. Negative: You need an extra table. Positive: You can easily add/remove fields to/from your output table by just changing the fields of your template table, you don't need to change the model.

 

So, let's create the submodel first:

JohannesLindner_1-1682447252332.png

 

It has three parameters: The input and taget datasets of the Append tool, and X, the number of times we want to execute the tool.

X is just a Long variable: 

JohannesLindner_2-1682447349440.png

 

The For iterator increases a value every time the tool is executed, until it reaches the target value (X).

JohannesLindner_3-1682447444392.png

 

The iterator behaves differently from what I expected. In programming languages, "for(y = 0; y < x; y++)" loops exactly x times. The iterator loops x + 2 times. I haven't worked with ModelBuilder much, and never with the iterators, so I don't know if that is expected and how best to circumvent that. I chose to check if the current iteration value is less than X and hooked the True output to the Append as precondition.

JohannesLindner_4-1682447765964.png

 

 

The actual model looks like this:

JohannesLindner_6-1682447998137.png

 

It has 5 parameters: Output Location and Output Name control the path of the output table. Template Table is the mentioned table with exactly one feature. Input Table and Input Field are the table and field from where the tool gets the number of rows to insert (it only looks at the first row. If you input a layer with a selection, it only looks at the first row of the selection).

 

Output Location, Output Name, and Template Table are used to create an empty copy of the Template Table (Create Table tool).

Get Field Value gets the target number of rows.

And then we execute our submodel with the newly created Output Table as Target Dataset, the Template Table as Input Dataset (that's why it is important that it has exactly one feature), and X as X.

 

This is my Template Table:

JohannesLindner_7-1682448591172.png

 

This is my Input Table (Input Field is IntegerFIeld1):

JohannesLindner_8-1682448658417.png

 

The tool:

JohannesLindner_9-1682448708258.png

 

And the generated output:

JohannesLindner_10-1682448755859.png

 

Note that it inserted 3 features, because that is the first selected row in the Input Table. Without selection, it would have inserted 5.

 

 

 

 

 


Have a great day!
Johannes
0 Kudos
LucaSchikowski
New Contributor II

Hi Johannes,

thank you so much for your detailed reply. I'll try to translate that into our project.

Have a nice one!
Luca

0 Kudos