Assigning different classes using ArcGIS Field Calculator

476
3
02-15-2022 10:35 AM
Dastageerkhan
New Contributor

below is the code with what I have come up with. I have two columns, one name First_kopp that have the classes CT, WT, ST ranging from 1-9, and the second column is named First-fros. There are conditions that have to be met based on both columns as expressed in the code below, however, thiS Code does not seem to work:

def reclass(FIRST_KOPP , FIRST_FROS):
   if 'CT' in FIRST_KOPP: 
               return "High frost risk"
   elif 'ST1' or 'ST2' or 'ST3' in FIRST_KOPP:
               return "Frost free"
   elif 'ST1' or 'ST2' or 'ST3' in FIRST_KOPP and 'Moderate frost risk' or 'High frost risk' in FIRST_FROS:
               return "Low frost risk"
   elif 'ST4' or 'ST5' or 'ST6' or 'ST7' or 'ST8' or 'ST9' in FIRST_KOPP:
               return "Frost free"
   elif 'WT' in FIRST_KOPP and 'Frost free' in FIRST_FROS:
               return "Low frost risk"
   else:
               return FIRST_FROS


 

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor
if FIRST_KOPP in ['ST1', 'ST2', 'ST3']:
  etcetera

I am assuming that FIRST_KOPP comes from a field? and if you are doing this for a table, tables are processed row by row and will return 1 value at a time


... sort of retired...
0 Kudos
KimGarbade
Occasional Contributor III

When you say it doesn't work do you mean it generates an error, it doesn't return a value, or it returns an unexpected value? 

When I test the code, it returns a value, just not the one I expected. 

For example I enter "ST1" and "Moderate frost risk" into the function and get "Frost free" when I would expect to get "Low frost risk". 

The reason for this is elif is exiting the function when it find the first match for 'ST1' in FIRST_KOPP (I.E. the first elif in the function) so it never makes it second elif to return 'Low frost risk".  Is this the behavior you are referring  to?

0 Kudos
CodyScott
Occasional Contributor

Two parts are going on here. 

First the in check is going to be True every time as it is written. So regardless of the value given it should return either "High frost risk" "Low frost risk" when using "CT" or anything else respectively. Updating, as dan mentioned, to checking if the value is in a list will solve this check.

 

Second part is based on the elif for ST1. When it sees ST1, ST2 or ST3 it proves True and it is going to step into that block. So it will never reach the next check, which incorporates the FIRST_FROS variable.

Two possible solutions.

1. invert the elif statements so your FIRST_FROS check comes before the check without FIRST_FROS.

2. add a nested if within the ST1... check to see if that FIRST_FROS check is True or not.

Option 1

 

 

def reclass(FIRST_KOPP , FIRST_FROS):
    if FIRST_KOPP in ['CT']: 
        return "High frost risk"
    elif FIRST_KOPP in ['ST1', 'ST2', 'ST3'] and FIRST_FROS in ['Moderate frost risk', 'High frost risk']:
        return "Low frost risk"
    elif FIRST_KOPP in ['ST1', 'ST2', 'ST3']:
        return "Frost free"
    elif FIRST_KOPP in ['ST4', 'ST5', 'ST6', 'ST7', 'ST8', 'ST9']:
        return "Frost free"
    elif FIRST_KOPP in ['WT'] and FIRST_FROS in ['Frost free']:
        return "Low frost risk"
    else:
        return FIRST_FROS

 

 

 

Option 2

 

 

def reclass(FIRST_KOPP , FIRST_FROS):
    if FIRST_KOPP in ['CT']: 
        return "High frost risk"
        
    elif FIRST_KOPP in ['ST1', 'ST2', 'ST3']:
        if FIRST_FROS in ['Moderate frost risk', 'High frost risk']:
            return "Low frost risk"
        else:
            return "Frost free"

    elif FIRST_KOPP in ['ST4', 'ST5', 'ST6', 'ST7', 'ST8', 'ST9']:
        return "Frost free"
        
    elif FIRST_KOPP in ['WT'] and FIRST_FROS in ['Frost free']:
        return "Low frost risk"
    else:
        return FIRST_FROS

 

 

 

0 Kudos