ljvasil

Repost:  Code is not updating cursor cell value

Discussion created by ljvasil on Oct 30, 2011
Latest reply on Oct 31, 2011 by ljvasil
Hi all,
I've posted this problem before, but I still haven't resolved the error. I'm writing a cellular automata code that changes the grid_code of a point based on the adjacent neighbor grid_codes. The point file is reclassified into likelihood of conversion. The code runs fine until it hits a point that needs to be converted. I'm using setValue to change the current grid code to the new one. The code and the error are shown below. Thanks for any suggestions...I'm at a loss of why it is not updating.

import sys, string, os, arcpy

arcpy.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")

ifc = sys.argv[1]

ily = "Input Layer"

desc = arcpy.Describe(ifc)

arcpy.MakeFeatureLayer_management(ifc,ily, "", "", "")
oid = desc.OIDFieldName

uc = arcpy.UpdateCursor(ifc)
line = uc.next()

for line in uc:   
    ci = line.getValue(oid)
    arcpy.AddMessage("The current FID value is " + str(ci))
    fi = line.getValue("GRID_CODE")
    arcpy.AddMessage("The current GridCode value is " + str(fi))
    sql = oid + " = " + str(ci)
    arcpy.SelectLayerByAttribute_management(ily,"NEW_SELECTION",sql)
    result = arcpy.SelectLayerByLocation_management(ily, "WITHIN_A_DISTANCE", ily, "45 meters", "NEW_SELECTION")
    sc = arcpy.SearchCursor(result)
    gridList = []
    OIDList = []
    for row in sc:
        item = row.getValue("GRID_CODE")
        id = row.getValue(oid)
        OIDList.append(id)
        gridList.append(item)
                       
    del row
    del sc
    ###This section will iterate through the list and pop the cursor Grid Code out of list depending on its position in the file, i.e. how many neighbors it has.
    ###It will have either 6 neighbors if the cursor is located along the boundary, or 9 if in the middle of the point file.  If less than 6, then there wouldn't
    ###be enough for a conversion.
    if len(OIDList) == 9:
        OIDList.pop(4)
        gridList.pop(4)
        gridList.sort()
        LR_Value = gridList[-1]
        tie_LR = gridList.count(LR_Value)
        Sec_lrValue = gridList[-2]
        tie_sec = gridList.count(Sec_lrValue)

        Thr_lrValue = gridList[-3]
        tie_thr = gridList.count(Thr_lrValue)

        Frt_lrValue = gridList[-4]
        tie_frt = gridList.count(Frt_lrValue)

        Fve_lrValue = gridList[-5]
        tie_fve = gridList.count(Fve_lrValue)
        ###This section reads the largest Grid_Code in list and decides whether to convert cursor cell to another land use.
        if tie_LR >= 4 and LR_Value > fi:
            ni = LR_Value  #Just naming the variable a different name.  I tried fi = LR_Value and that didn't work either.
            uc.setValue("GRID_CODE",ni)  #This is what is giving the error.  "No attribute called setValue"
            uc.updateRow(line)
        ###If the largest cell wasn't larger than fi or greater than a count of 4, it keeps going through the list to the second largest, third largest, etc.
        elif tie_sec >= 4 and Sec_lrValue > fi:
            ni = Sec_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_thr >= 4 and Thr_lrValue > fi:
            ni = Thr_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_frt >= 4 and Frt_lrValue > fi:
            ni = Frt_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_fve >= 4 and Fve_lrValue > fi:
            ni = Fve_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)

    elif len(OIDList) == 6:
        OIDList.pop(1)
        gridList.pop(1)
        gridList.sort()
   
        LR_Value = gridList[-1]
        tie_LR = gridList.count(LR_Value)
   
        Sec_lrValue = gridList[-2]
        tie_sec = gridList.count(Sec_lrValue)

        Thr_lrValue = gridList[-3]
        tie_thr = gridList.count(Thr_lrValue)

        Frt_lrValue = gridList[-4]
        tie_frt = gridList.count(Frt_lrValue)

        Fve_lrValue = gridList[-5]
        tie_fve = gridList.count(Fve_lrValue)
       
        ###This is a duplicate of the condition statement in the loop above this.  It's messy and I probably can pull it out of a loop, but I haven't tried it yet.
        if tie_LR >= 4 and LR_Value > fi:
            ni = LR_Value
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_sec >= 4 and Sec_lrValue > fi:
            ni = Sec_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_thr >= 4 and Thr_lrValue > fi:
            ni = Thr_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_frt >= 4 and Frt_lrValue > fi:
            ni = Frt_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
        elif tie_fve >= 4 and Fve_lrValue > fi:
            ni = Fve_lrValue
            uc.setValue("GRID_CODE",ni)
            uc.updateRow(line)
    else:
        arcpy.AddMessage("Not enough points to make conversion.")
       
    #elif len(OIDList) == 4:
        #OIDList.pop(0)
        #gridList.pop(0)
       
          
    arcpy.AddMessage(OIDList)
    arcpy.AddMessage(gridList)
    #arcpy.AddMessage("The large value in list is " + str(LR_Value))
    #arcpy.AddMessage("This is the tie value " + str(tie_LR))
    arcpy.AddMessage("This is the updated value " + str(fi))
   
       
del line
del uc


Here are some of the print results from the message window when I run the code. I'm not sure why setValue is causing a problem.

The current GridCode value is 2.0
[7, 8, 9, 16, 18, 29, 30, 31]
[1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
This is the updated value 2.0
The current FID value is 18
The current GridCode value is 1.0
<type 'exceptions.AttributeError'>: 'Cursor' object has no attribute 'setValue'
Failed to execute (CellAutoPnt).
Failed at Sun Oct 30 17:04:04 2011 (Elapsed Time: 12.00 seconds)

Thanks for any help!

Outcomes