AnsweredAssumed Answered

Updating the rows in one table based on values in another table or using nested cursors

Question asked by octaffle on Apr 21, 2015
Latest reply on Apr 21, 2015 by octaffle

I am finding it hard to articulate what I am trying to do, so here's what I have and here's where I'm going.

 

I have: 4 raster datasets.  One is a set of drainage zones, one is the soil type, one is slope, and one is depth below surface. I also have a large table of stormwater practices and their associated physical constraints.

 

Desired outcome: For each drainage zone, I want a list of stormwater practices that can go on the site.

 

What I've done:  For each drainage zone, I have found the median slope and median depth below surface using Zonal Statistics.  I have combined the median slope, median depth, drainage zone, and soil type into a single raster dataset using the Combine function.  I have used the Build Raster Attribute Table to have all of those values available to me in a single table without converting it back to a shapefile.  I have a Cursor set up to duplicate the constraint table for each unique combination of drainage zone and soil type.

 

What I want to do:  I want to compare the Combination table to the table of constraints and delete rows from the constraint table when certain conditions are met.  For example, some stormwater practices only work on soil types A and B, so if the soil type in the constraint table is not found in the row for the combination table, I want the row in the constraint table to be deleted.  Here is the code I have come up with to help you understand my goal.

 

# Combine rasters to give unique combinations
combo = arcpy.sa.Combine([DAras, EffHSG, med_WT, med_slope])
combo.save(WorkPath + r"\combo")
combo_table = arcpy.BuildRasterAttributeTable_management(combo)

# Convert integer HSG code to string type
arcpy.AddField_management(combo_table, "HSG", "TEXT", "", "", 6)
with arcpy.da.UpdateCursor(combo_table, ["EFFECTIVEHSG", "HSG"]) as cursor:
     for row in cursor:
          if row[0] == 1:
            row[1] = "A"
        if row[0] == 2:
            row[1] = "B"
        if row[0] == 3:
            row[1] = "C"
        if row[0] == 4:
            row[1] = "D"
        cursor.updateRow(row)

### _____________COMPARE CRITERIA_____________________ ###
print("Creating BMP output folder...")
# Define location of constraint database and establish new output location
CRIT = TabPath + "constraints.xlsx"
BMPFold = ProjFolder + r"\BMP__Tables"
if not os.path.exists(BMPFold):
  os.makedirs(BMPFold)
print("Loading constraint database...")
# Convert Excel constraint file to GIS table
compare = arcpy.ExcelToTable_conversion(CRIT, ProjFolder + r"\constraints")
fields = ["SOIL", "MAX_SLOPE", "MIN_CDA", "MAX_CDA", "WT_SEP", "WT_RELAX",
"COAST_SEP", "MIN_DEPTH", "DEPTH_RELA", "COAST_MIN_"]

# Create output table for each drainage area
for row in arcpy.da.SearchCursor(combo_table, ["DARAS", "HSG", "MEDSLOPE", "MEDWT"]):
       arcpy.Copy_management(compare, BMPFold + "\\" + "DA" + str(row[0]) + "HSG" + str(row[1]))
       DAmask = arcpy.sa.SetNull(DAras, DAras, "VALUE <> " + str(row[0]))
       arcpy.env.mask = DAmask
       arcpy.env.snapRaster = DAmask
       arcpy.AddField_management(BMPFold + "\\" + "DA" + str(row[0]), "MOD", "TEXT", "", "", 14)
       for r in arcpy.da.UpdateCursor(BMPFold + "\\" + "DA" + str(row[0]) + "HSG" + str(row[1]), fields):
             if row[1] not in r[0]:
                del(row)

 

 

 

The problem:  I get an error saying that "NameError: name 'row' is not defined" for line 41, "if row[1] not in r[0]:".  Once the code enters the nested cursor, "row" gets wiped out and is no longer a valid parameter.  What is a workaround for this?

 

EDIT:  The python syntax converter works very poorly on my computer for some reason.  Sorry.  I tried to update it so the indentations are in the right place.  If they do not appear in the correct spot, I promise they are correct in my actual code and that is not the problem.

Outcomes