AnsweredAssumed Answered

Update Cursor Issue: Multiple nested If Elif Else

Question asked by kmerry on Sep 6, 2016
Latest reply on Sep 7, 2016 by dkwiens

I am trying to work through an Update cursor issue. I have a forest stands shapefile with the following fields: Age, Historical Community (Hist_Comm), Pine Basal Area (PINE_BA), Fire Return Interval (FIRE_FRQ), Time Since Last Fire (LAST_YR), and then a Class (ECC). The goal is to use the AGE, Hist_Comm, FIRE_FRQ, and LAST_YR fields along with several user inputs to assign an ECC value. 

 

This is how it would work: 

If the Historical Community field (Hist_Comm) is greater than 3:

      and If the Age (Min_Age1) is greater than 80:

         and if the Pine Basal Area (Min_PineBA1) is greater than or equal to 40  or Pine Basal Area (Max_PineBA1) is less

          than or equal to 60:          

             and if the Fire Frequency (Max_FI1) is less than 3  and the Time Since Last Fire (Max_FY1) is less than 3:          

                then ECC = 1

             or elif the Fire Frequency (Min_FI1) is greater than or equal to 3  but less than or equal to 6 (Max_FI1) and the Time

             Since Last Fire (Max_FY1) is less than or equal to 6: 

                  then ECC = 3

 

It should search through the attribute table selecting the rows from the fields Hist_Comm, AGE, PINE_BA, FIRE_FRQ, and LAST_YR that fit the above criteria and assign then assign a value to the ECC field.

 

I think (stress think) I am having an issue with the user inputs working within multiple levels of nested if statements. 

 

The script has been added to a toolbox. Each input variable Data Type is defined as a string. Each input variable will be an integer value. 

 

Here's the code (also attached):

 

# Import arcpy module
import arcpy

# Set Workspace
arcpy.env.workspace = 'C:\\ECC_Test\\Output'

# Set Input Parameters
Min_Age1 = arcpy.GetParameterAsText(0)
Min_PineBA1 = arcpy.GetParameterAsText(1)
Max_PineBA1 = arcpy.GetParameterAsText(2)
Min_FI1 = arcpy.GetParameterAsText(3)
Max_FI1 = arcpy.GetParameterAsText(4)
Max_FY1 = arcpy.GetParameterAsText(5)

# Define Variables
fc = "C://ECC_Test//Output//Stands_2_t.shp"

#Update Cursor
with arcpy.da.UpdateCursor(fc, ["Hist_Comm", "FIRE_FRQ", "LAST_YR", "AGE", "PINE_BA", "ECC"]) as cursor:
    for row in cursor:
       if (row[0] >= 3) and (Min_Age1 >= 80) and (row[3] >= 80):
         if (Min_PineBA1 >= 40) and (Max_PineBA1 <= 60) and(row[4] >= 40) and (row[4] <= 60):
             if (Max_FI1 < 3) and (row[1] < 3) and (Max_FY1 < 3) and (row[2] < 3):
                row[5] = 1
             elif (Min_FI1 >= 3) and (row[1] >= 3) and (Max_FI1 <= 6) and (row[1] >= 3) and (Max_FY1 <= 6) and (row[2] <= 6):
                row[5] = 3
             else:
                row[5] = 4
          cursor.updateRow(row)


del cursor, row

Attachments

Outcomes