Iterate Through Fields in Model Builder

7133
4
07-30-2012 09:58 AM
shaynadenny
New Contributor
Hi,

Is there a way to iterate through fields in an attribute table?  I have a polygon shapefile with hundreds of columns.  I am trying to turn each column's values into a separate raster with the Polygon to Raster tool in Model Builder.  Is there a way to streamline this with either the iterate tool or a loop?  Or anything else?  Thanks!
0 Kudos
4 Replies
AlanTabbernor
Emerging Contributor
This may or may not help but I can show you the python code to accomplish this...

#START OF CODE
# Import system modules
import arcpy
from arcpy import env

# Set environment settings - make this where your output will be going
env.workspace = "C:/data"

# Set local variables - make these whatever you need them to be
inFeatures = "ca_counties.shp"
valField = "NAME" # This is the field that must change to represent each of your fields
                        # so instead of using this we will use fieldList[count] so that were using EVERY field name in the table
outRaster = "c:/output/ca_counties"
assignmentType = "MAXIMUM_AREA"
priorityField = "MALES"
cellSize = 0.5

fieldList = arcpy.ListFields(filename) #creates a list that contains all of the field names from the
attribute table of 'filename'
#change filename to the file you're using
count = 0                                  
while count < len(fieldList):
____arcpy.PolygonToRaster_conversion(inFeatures, fieldList[count], outRaster, assignmentType, priorityField, cellSize)
____#perform the conversion using every field name contained in fieldList
____count = count+1
#END OF CODE

For some reason its not showing the indents so I had to put in underscores to represent the indents.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000030000000
This will give you specifics on what each parameter means for the PolygonToRaser_conversion tool
PolygonToRaster_conversion (in_features, value_field, out_rasterdataset, {cell_assignment}, {priority_field}, {cellsize})
0 Kudos
RichardFairhurst
MVP Honored Contributor
There are Code tags that perserve indentation for your code (the word CODE is bracketed inside straight brackets to begin the code block and the code block is ended with /CODE being enclosed in straight brackets). Also the counter can be incremented in a more Pythonic way by using the += operator (highlighted in red). See below:

#START OF CODE
# Import system modules
import arcpy
from arcpy import env

# Set environment settings - make this where your output will be going
env.workspace = "C:/data"

# Set local variables - make these whatever you need them to be
inFeatures = "ca_counties.shp"
valField = "NAME" # This is the field that must change to represent each of your fields
                        # so instead of using this we will use fieldList[count] so that were using EVERY field name in the table
outRaster = "c:/output/ca_counties"
assignmentType = "MAXIMUM_AREA"
priorityField = "MALES"
cellSize = 0.5

fieldList = arcpy.ListFields(filename) #creates a list that contains all of the field names from the 
attribute table of 'filename'
#change filename to the file you're using
count = 0                                   
while count < len(fieldList):
    arcpy.PolygonToRaster_conversion(inFeatures, fieldList[count], outRaster, assignmentType, priorityField, cellSize)
    #perform the conversion using every field name contained in fieldList
    count += 1
#END OF CODE
0 Kudos
samahtermos1
Deactivated User
I have tried this code, but in the polygon to raster function, it is giving an error when using the fielditeration in the field value.
see the code below
>>> import arcpy
>>> from arcpy import env
>>> outras = "F:\\samar\\pyth_try.gdb\\outras"
>>> inpoly = "F:\\samar\\pyth_try.gdb\\nov2005"
>>>
>>> fieldlist = arcpy.ListFields(inpoly)
>>> count = 7 #  to start iteration at field number 7
>>> while count < len(fieldlist):
...     arcpy.PolygonToRaster_conversion(inpoly,fieldlist[count],outras,"MAXIMUM_AREA","NONE",1)
...     count = count + 1
...     # end of code
...    
Runtime error  Traceback (most recent call last):   File "<string>", line 2, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 2581, in PolygonToRaster     raise e ExecuteError: ERROR 000622: Failed to execute (Polygon to Raster). Parameters are not valid. ERROR 000623: Invalid value type for parameter value_field. 
# it worked when trying the code without the field list
>>> arcpy.PolygonToRaster_conversion(inpoly,"F11_10_2005",outras,"MAXIMUM_AREA","NONE",1)
<Result 'F:\\samar\\pyth_try.gdb\\outras'>
>>>
0 Kudos
JoshColston
New Contributor

Hi,

Did anyone ever come up with a solution to this problem using Model Builder? I have the exact same issue, but all the solutions proposed here involved ArcPy and I'm not familiar with that program. I've been trying to do it in Model Builder but have not been able to get it to work.

Thanks,

Josh

0 Kudos