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 == 1: row = "A" if row == 2: row = "B" if row == 3: row = "C" if row == 4: row = "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) + "HSG" + str(row)) DAmask = arcpy.sa.SetNull(DAras, DAras, "VALUE <> " + str(row)) arcpy.env.mask = DAmask arcpy.env.snapRaster = DAmask arcpy.AddField_management(BMPFold + "\\" + "DA" + str(row), "MOD", "TEXT", "", "", 14) for r in arcpy.da.UpdateCursor(BMPFold + "\\" + "DA" + str(row) + "HSG" + str(row), fields): if row not in r: del(row)
The problem: I get an error saying that "NameError: name 'row' is not defined" for line 41, "if row not in r:". 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.