Hi there,
I got a table called "testtabelle" which has only one field "BerechneterWert" (data type: short).
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
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
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:
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:
The For iterator increases a value every time the tool is executed, until it reaches the target value (X).
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.
The actual model looks like this:
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:
This is my Input Table (Input Field is IntegerFIeld1):
The tool:
And the generated output:
Note that it inserted 3 features, because that is the first selected row in the Input Table. Without selection, it would have inserted 5.
Hi Johannes,
thank you so much for your detailed reply. I'll try to translate that into our project.
Have a nice one!
Luca