I am developing a new model for a population profile geoprocessing task. The first step sums a series of variables based on a simple point in polygon routine. I then want to add a new set of field names to the table to create some percentages using Calculate Field.
This is possible using 'Add Field' but I need to use the tool several times and it is cumbersome as I have a high number of fields to add in.
Is there a way to use an iterator to run Add Field?
You could use a For iterator (e.g. "For 1 to 50" to do something, like Add Field, 50 times [or possible 49 times, depending how it counts]). This would add 50 copies of very similar fields, like all fields of the same type, with incrementing numbers in their names, like "My_Field_1", "My_Field_2", etc.
Or, you could populate a table with your Add Field parameters (columns holding info like field name, field type, etc.) and run the Iterate Row Selection to get those values to use in Add Field, so you could have full control over the types and names of all your new fields.
Thanks Darren, the Iterate Row Selection seems to be the way to go. I have set this up and it takes it's values from a table with the required parameters in Add Field. I still seem to be running into trouble with it though.
Would you have the Iterate Row Selection and Add Field working off a secondary Model within the main Model?
A good pattern is to make a numpy array of typed data and use arcpy.da.ExtentTable to add the fields to your table.
The Python snippet would be in a Calculate Value model tool. An example of syntax is in this GP sample:
After some different approaches to this I created a simple .py script that updates the table with all the fields I need. See below.
from arcpy import env
env.workspace = r"C:\X\X.gdb"
for PopSummary in arcpy.ListTables("*"):
arcpy.AlterField_management(PopSummary, "Sum_Pop_T11", "", "TotalPop")
arcpy.AlterField_management(PopSummary, "Sum_Pop_014T11", "", "Pop_014")
arcpy.AlterField_management(PopSummary, "Sum_Pop_1524T1", "", "Pop_1524")
Lot's of other useful way of updating a table using the Fields Toolset within the script - Alter Field, Calculate Field etc