AnsweredAssumed Answered

Data Access Module: Update Cursor (Not Updating Records)

Question asked by Playa on Jul 13, 2016
Latest reply on Jul 13, 2016 by Playa


I've written a Python function that is meant to update a File Geodatabase Table (Landuse) based on a Feature Class (Watershed). I'm using a Update Cursor  to update each field value where it finds a match within a Python Dictionary of HydroID and in return I use the "Shape_Area" as part of my calculations to update each field value.

 

There are eight landuse fields that I want to update using the same formula for each HydroID match:

  • Commercial_Forestry
  • Cultivated
  • Indigenous
  • Mines
  • Natural_Vegetation_Forest
  • Urban
  • Waterbodies
  • Wetlands

 

So instead of specifying each field through its index position manually, I step into a for loop and loop through each field updating the field value based on the formula. The problem that I'm having is that the Update Cursor is not updating the rows, although I'm calling updateRow() afterwards.

 

Is there anyway I can get around the following without having to specify each field through its index position manually?

 

# calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
    watershed_fields = ["HydroID", "Shape_Area"]
    valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)}  # @UndefinedVariable
    landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
    print(landuse_fields)
    with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur:  # @UndefinedVariable
        for row in upcur:
            keyvalue = row[0]
            if keyvalue in valuedict:
                print(keyvalue)
                for landuse in row[1:]:
                    landuse = (landuse*(30*30))/(valuedict[keyvalue][0])*100
                    upcur.updateRow(row)
                    print(landuse)

landuse_percentage(watershed, output_pivot)

Python Code: for loop over each field

 

12366 (HydroID)

  • 0.962788133178 - (Landuse percentage based on Shape Area)
  • 62.0189203295
  • 31.8703202065
  • 0.123498273168
  • 13.8522293032
  • 1.24419729078
  • 1.02530804735
  • 2.19975286437

Python Console: Correct Values printed but table not updated.

 

# calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
    watershed_fields = ["HydroID", "Shape_Area"]
    valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)}  # @UndefinedVariable
    landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
    with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur:  # @UndefinedVariable
        for row in upcur:
            keyvalue = row[0]
            if keyvalue in valuedict:
                row[1] = (row[1]*(30*30))/(valuedict[keyvalue][0])*100
                row[2] = (row[2]*(30*30))/(valuedict[keyvalue][0])*100
                row[3] = (row[3]*(30*30))/(valuedict[keyvalue][0])*100
                row[4] = (row[4]*(30*30))/(valuedict[keyvalue][0])*100
                row[5] = (row[5]*(30*30))/(valuedict[keyvalue][0])*100
                row[6] = (row[6]*(30*30))/(valuedict[keyvalue][0])*100
                row[7] = (row[7]*(30*30))/(valuedict[keyvalue][0])*100
                row[8] = (row[8]*(30*30))/(valuedict[keyvalue][0])*100
                upcur.updateRow(row)

landuse_percentage(watershed, output_pivot)

 

Python Code: specifying index position of each field manually.

 

Landuse: Updated Table using second Python Function

 

So any help how to loop through each each instead of having to manually specify the index position will be appreciated.

Outcomes