ljvasil

Code still not updating cursor

Discussion created by ljvasil on Oct 19, 2011
Latest reply on Oct 20, 2011 by ljvasil
Hi all,

I wrote this somewhat simple script that is suppose to change and update polygon GRIDCODES based some rules. The shapefile is reclassified into likelihood of conversion. So the code is set up to get the GRIDCODE value of all adjacent polygons to the cursor polygon. I put these in a list, sort the codes, and assign a variable to the largest value. I then count the number of times this large variable occurs. If it occurred 4 or more times and the large value is greater than the cursor gridcode, I want to update the cursor to the large value. Fore some reason, it's doing almost everything I ask, but updating the values if it passing the if statement. It is passing through the if statement because I put a print statement within and I'm getting values if conditions are met, but it is still not updating.  Can anyone see where I'm going wrong? Thanks.

Here's the code:

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

cursor = arcpy.UpdateCursor(ifc)
row = cursor.next()

while row:
    ci = row.getValue(oid)
    fi = row.getValue("GRIDCODE")
    arcpy.AddMessage("The current GridCode value of this polygon is " + str(fi))
    sql = oid + " = " + str(ci)
    arcpy.SelectLayerByAttribute_management(ily,"NEW_SELECTION",sql)
    result = arcpy.SelectLayerByLocation_management(ily, "BOUNDARY_TOUCHES", ily, "", "NEW_SELECTION")
    uc = arcpy.UpdateCursor(result)
    gridList = []
    for row in uc:
        item = row.getValue("GRIDCODE")
        gridList.append(item)
        gridList.sort()
        LR_Value = gridList[-1]
        tie_LR = gridList.count(LR_Value)
        if tie_LR >= 4 and LR_Value > fi:
            row.setValue("GRIDCODE", LR_Value)
            uc.updateRow(row)
            arcpy.AddMessage(LR_Value)
            arcpy.AddMessage(fi)
    #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 row
    del uc
    row = cursor.next()


Here are the results I???m getting:
The current GridCode value is 11.0
[5.0, 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0, 6.0, 8.0, 8.0, 8.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 12.0, 12.0, 12.0, 12.0]
The large value in list is 12.0
This is the tie value 4
This is the updated value 11.0  (-- not right, should been updated to 12.0)

Outcomes