[GIS] Iterating field values and using output in ArcMap ModelBuilder

arcgis-10.2arcgis-desktopmodelbuilder

I am using ArcGIS 10.2 ModelBuilder with the Field Values iterator to select unique field values. I then want to forward these values to the Add Field tool, to create a new field for each unique value.

The problem I have is that the iterator's output value appears to have a temporary starter value of "1" by default, which is then replaced by the actual values once the model runs. However, the issue is that the Add Field tool throws an error before the model even starts, because it cannot use numbers as inputs for field names, so it remains out of action once the "real" values become available.

How fo I flush-out or replace this starting value of "1"?

The Iterate Field Values tool is correctly set to "String" data type, yet the temporary starter / filler value remains "1".

I've attached a picture of my model and submodel.


I was unable to get it working in ModelBuilder, it is presumably a bug. But I was successful in getting it working via a python script along the lines of the answer listed below.

Sub-model called "sifter"
Model

Best Answer

This is not exactly the answer you are looking for, but you could use "pivot table" to do what you want.

Concerning ModelBuilder problem, take a look at the type of input. For instance, you will not be able to create a field based on a value that is not of type "text", and some characters are not allowed in the name of the fields (depending on the type of output data)


Using Python, you can use the search cursor

import arcpy
rows = arcpy.SearchCursor(yourfeatureclass)
for row in rows:
   try:
        arcpy.AddField_management(yourfeatureclass, str(row.getValue("yourfield"), "LONG")
   except:
        print "field already exists" 
Related Question