1

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')
2
  • This is just a guess, but try removing the "+ '.tif'" from your outRaster string. If that doesn't work, try creating an entirely new feature class from your selection to pass to "inFeatures", instead of just passing your selection. You can programatically delete the feature class afterwards.
    – Conor
    Commented Aug 28, 2013 at 16:43
  • Also, you may want to check your folder privileges where you have your shapefiles stored. I sometimes encounter that error when trying to run raster operations either reading or storing in folders that have limited read/write privileges or are subfolders of folders with limited read/write privileges. Does the problem still persist if you move your folders to the desktop?
    – Conor
    Commented Aug 28, 2013 at 16:52

1 Answer 1

1

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) )
2
  • It works, but the raster files created are all the same, which means that there is something wrong with the layers selected ..
    – Jing Lu
    Commented Aug 29, 2013 at 5:08
  • Have updated main answer
    – Hornbydd
    Commented Aug 29, 2013 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.