[GIS] Failed to convert polygon to raster

arcgis-10.1polygonpythonraster-conversion

I have two steps here. First select layer by attributes. Second convert polygon to raster. It failed when it goes to second step and kept giving me error 999999: Error executing function. I pasted my codes below. Is there anyone who has the same problem. I am now using Arcgis 10.1 and has SP1 installed.

exec('''
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("spatial")
import string
env.workspace = "G:\\countoverlap"


# Local variables:
FRAP_Fire111 = "G:\\countoverlap\\FRAP_Fire111.shp"

fldName = 'Year'
fcName = 'FRAP_Fire111.shp'
#set creates a unique value iterator from the value field
yearList = list(set([row.getValue(fldName) for row in arcpy.SearchCursor(fcName)]))
print(yearList)

 ''')


env.workspace = "G:\\countoverlap"
env.scratchWorkspace = "G:\\countoverlap\\overlap.gdb"
arcpy.MakeFeatureLayer_management(FRAP_Fire111, "lyr")

for year in yearList[1:len(yearList)]:

    print(str(year))

    # Process: Select Layer By Attribute
    arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION", '"Year" = year' )
    print('Layer_Seclected')

    # Process: Polygon to Raster
    # Set local variables
    inFeatures = "lyr"
    valField = "Year"
    outRaster = "str(env.workspace)+'\\'+str(year)+'.tif'"
    assignmentType = "MAXIMUM_AREA"
    priorityField = "NONE"
    cellSize = 1080
    # Execute PolygonToRaster
    arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster, assignmentType, priorityField, cellSize)
    print('Raster_Created')

Best Answer

I think the problem is this line:

outRaster = "str(env.workspace)+'\\'+str(year)+'.tif'"

It probably needs to be this:

outRaster = env.workspace + '\\' + str(year) + '.tif'

In fact I would avoid starting the name of your file with a number so if it was me it would be something like this so my files are starting with an "r":

outRaster = env.workspace + '\\r' + str(year) + '.tif'

This line is also incorrect (you have enclose your year variable within the string):

arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION", '"Year" = year' )

It should be:

arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION", '"Year" = ' + str(year) )
Related Question