0

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 as it passing the if statement. I know it's going into the if statement because I get printed results in ArcGIS, but it's 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()
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Gribouillis
0

I think the problem is that you are using the same variable name 'row' for the inner loop. There also seems to be an indentation issue. Use

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 = sorted(subrow.getValue("GRIDCODE") for subrow in uc)
    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)
    del row
    del uc
    row = cursor.next()

Edit: I can't test your code, so...

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.