Code will not update cursor value

491
5
10-19-2011 07:40 AM
LindaVasil
New Contributor III
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 occured 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.  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 tie_LR > fi:
            row.setValue(fi, LR_Value)
            uc.updateRow(row)
    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))
    #arcpy.AddMessage(gridList)
    del row
    del uc
    row = cursor.next()
Tags (2)
0 Kudos
5 Replies
ChristopherStorer
New Contributor
If I'm reading this correctly, the first parameter of your setValue is fi, which is set to a GRIDCODE value.  The first parameter should be a field name, I believe, probably "GRIDCODE".  That may be causing the trouble.
0 Kudos
LindaVasil
New Contributor III
Yeah, I tried "GRIDCODE" as well as the first argument/parameter, but that didn't work either.  Not getting any errors, but the cursor is not updating.

Thanks
0 Kudos
ChristopherStorer
New Contributor
Can you prove that it's getting to the if statement?  I'm curious how many rows the loops is iterating through.  That will help pinpoint the error.
0 Kudos
LindaVasil
New Contributor III
It is making it's way into the if statement.  I put an "addmessage" statement in the if statement to print LR_Value and fi.  It is printing those two values along with the first addmessage statement in the while loop.  It goes through the loop multiple times, I usually cancel the operation once I get enough to analyze if the cursor is updating or not.  I did correct the if statement, instead of tie_LR > fi, it should be LR_Value > fi.  Here's the code now.  Still not updating though...weird.

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()
0 Kudos
DarrenWiens2
MVP Honored Contributor
I assume it's getting confused at your use of "row" for both cursors. Change one to something else (e.g. "row2").
0 Kudos