[GIS] Error when looping Polygon to Raster with python through different fields using ArcGIS Desktop

arcgis-10.0arcpyconverterror-999999loop

I've been trying to create a python script to loop through numerous shapefiles converting various fields to raster outputs.

I've been working off a lot of what has been said in this previous thread on the same issue -> Python loop Polygon to raster not working?

But can still not get it to work. I keep getting the dreaded 999999 error.

The code I've been trying is shown below:

#import arcpy module
import arcpy
from arcpy import env

#set working environment
env.workspace = "C:\\Users\\s175850\\Documents\\GIS\\GIS_Data\\Source_Methodology\\Pesticides\\Application_Term\\2008"
Dir = env.workspace

#List feature classes
fclist = arcpy.ListFeatureClasses()

#loop
for fc in fclist:

  output = Dir + "\\r" + fc + "_ras"
  cell = 5

  #process polygon to raster
  arcpy.PolygonToRaster_conversion(fc, "January", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "February", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "March", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "April", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "May", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "June", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "July", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "August", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "September", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "October", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "November", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "December", output, "CELL_CENTER", "", cell)

print "finished polygon to raster"

I'm not sure if there would be issues with outlining the 12 polygon to raster conversions for the 12 fields like I have done so I tried it with just one polygon to raster run for one field name. I still managed to get the same error.

I am using ArcGIS 10.

Best Answer

  1. Try not to use concatenation for path. Use os.path.
  2. You are trying to create 12 rasters with the same name. Specify different names for rasters.
  3. Do you iterate shape files in folder? If you do, your output path contains "." in name.

You can try this:

for fc in fclist:
    cell = 5
    months =["January","February","March","April","May","June","July","August","September","October","November","December"]
    for month in months:
        output = os.path.join(Dir, ("r_" + fc.rstrip(".shp") + "_" + month + ".tif"))
        arcpy.PolygonToRaster_conversion(fc, month, output, "CELL_CENTER", "", cell)

And if you want to take unique values from field you can try this (field - it's your field from shape file, "MONTH" maybe):

rawValues = [row[0] for row in arcpy.da.SearchCursor(fc, (field))]
months = set(rawValues)