Iterate through columns in polygon

839
5
12-19-2017 11:36 AM
EginaMalaj
New Contributor

Hello,

I'm trying to iterate through several columns in a polygon file and generate a raster for each column with the Polygon2Raster. Unfortunately, I'm new to Python and I'm not able to generate a new raster for each column. There is a similar question here: Iterate Through Fields in Model Builder However that solution does not work entirely. My code is below. Currently that produces only one raster. 

Thank you very much in advance for you help!

import arcpy

arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'D:\_Work\Output_vs1'

prov = 'prov_outln.shp'
outras = 'rastrat'

field_names = [f.name for f in arcpy.ListFields(prov)]

count = 2 
while count < len(field_names):
    arcpy.PolygonToRaster_conversion(prov,field_names[count],outras,"MAXIMUM_AREA","NONE",5800)
    count += 1
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

what is the form of your data?  PolygontoRaster uses a polygon featureclass, shapefile or coverage, which only has one geometry field

0 Kudos
EginaMalaj
New Contributor

Thank you Dan for your reply!

The data type is Shapefile Feature Class. I want to iterate through each column of the attribute table of the polygon. The attribute table has 10 columns and the end result should be 10 raster files.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Oh I see, you want to use the columns as the attribute,  The problem appears that 'outras' will get overwritten each time through the loop.  You should be providing a full filename to the output  (ie outras2, outras3 ...) with a file extension (ie tif).  And are you really wanting a 5800 meter/foot cell size?

0 Kudos
EginaMalaj
New Contributor

Yes, you are correct. Any idea how I can automatically generate those rasters?

Cell size is ok. Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

the simplest way would be to modify the script in the help topic and add a line with how you want to label your filenames, assuming you are familiar with python

0 Kudos