[GIS] Field Calculator – How to evaluate and sum multiple fields

arcgis-desktopconditionalfield-calculatorpython

I'll do my best to describe the problem – my main issue isn't so much the python code but rather the logic I'm using.

The file I'm working on is a polygon shapefile with forest attribute data. In the attribute table, each polygon has 5 fields that describe tree species present (SP1-5) and a percentage that each species represents in each polygon (SP1-5_PER). Below is a picture of the attribute table I'm working with.

Attribute Table

I'm trying to use field calculator to sum the percent of coverage that is composed of coniferous trees, in each polygon. Ie, this polygon has 50% coniferous, whereas that one has 80%. etc etc etc.

Here is what my code in field calculator looks like:

def percvr(sp1,per1,sp2,per2,sp3,per3,sp4,per4,sp5,per5):
    sum = 0
    if (sp1 == "Sw" or "Se" or "Sb" or "P" or "Pl" or "Pj" or "Pa" or "Pf" or "Fb"  or "Fa" or "Fd"):
        sum=sum+per1
    if (sp2 == "Sw" or "Se" or "Sb" or "P" or "Pl" or "Pj" or "Pa" or "Pf" or "Fb" or "Fa" or "Fd"):
        sum=sum+per2
    if (sp3 == "Sw" or "Se" or "Sb" or "P" or "Pl" or "Pj" or "Pa" or "Pf" or "Fb" or "Fa" or "Fd"):
        sum=sum+per3
    if (sp4 == "Sw" or "Se" or "Sb" or "P" or "Pl" or "Pj" or "Pa" or "Pf" or "Fb" or "Fa" or "Fd"):
        sum=sum+per4
    if (sp5 == "Sw" or "Se" or "Sb" or "P" or "Pl" or "Pj" or "Pa" or "Pf" or "Fb" or "Fa" or "Fd"):
        sum=sum+per5
    return sum

esri_field_calculator_splitter

percvr( !SP1!, !SP1_PER!, !SP2!, !SP2_PER!, !SP3!, !SP3_PER!, !SP4!, !SP4_PER!, !SP5!, !SP5_PER!)

The above text may not be overly clear so I included an image to show indenting:

Code

Basically, what I want it to do is, if the SPn field corresponds to one of the above tree codes, add the number from the corresponding percentage field to the sum. If the tree code does not match one of the above, do nothing and move on to the next case. Finally, output the sum to the field.

The issue that seems to come up is that this function seems to be adding all percent cover no matter what so that the resulting values are all 100%

Best Answer

Well, with the help of a friend I got the code to work. Here's what it looked like in the end:

def percvr(sp1,per1,sp2,per2,sp3,per3,sp4,per4,sp5,per5):
  sum = 0
  if (sp1 == "Sw" or sp1== "Se"  or sp1== "Sb" or sp1== "P" or sp1== "Pl" or sp1== "Pj" or sp1== "Pa" or sp1== "Pf" or sp1== "Fb" or sp1== "Fa" or sp1== "Fd"):
    sum=sum+per1
  if (sp2 == "Sw" or sp2== "Se" or sp2== "Sb" or sp2== "P" or sp2== "Pl" or sp2=="Pj" or sp2=="Pa" or sp2== "Pf" or sp2=="Fb" or sp2== "Fa" or sp2== "Fd"):
    sum=sum+per2
  if (sp3 == "Sw" or sp3 == "Se" or sp3 == "Sb" or sp3 == "P" or sp3 == "Pl" or sp3 == "Pj" or sp3 == "Pa" or sp3 == "Pf" or sp3 == "Fb" or sp3 == "Fa" or sp3 == "Fd"):
    sum=sum+per3
  if (sp4 == "Sw" or  sp4 == "Se" or sp4 == "Sb" or sp4 == "P" or sp4 == "Pl" or sp4 == "Pj" or sp4 == "Pa" or sp4 == "Pf" or sp4 == "Fb" or sp4 == "Fa" or sp4 == "Fd"):
    sum=sum+per4
  if (sp5 == "Sw" or sp5 ==  "Se" or sp5 ==  "Sb" or sp5 ==  "P" or sp5 ==  "Pl" or sp5 ==  "Pj" or sp5 ==  "Pa" or sp5 ==  "Pf" or sp5 ==  "Fb" or sp5 ==  "Fa" or sp5 ==  "Fd"):
    sum=sum+per5
  return sum

__esri_field_calculator_splitter__
percvr( !SP1!, !SP1_PER!, !SP2!, !SP2_PER!, !SP3!, !SP3_PER!, !SP4!, !SP4_PER!, !SP5!, !SP5_PER!)

It's not very clean but it works - everything is summed properly. Ultimately, I think achieving the same goal with a list would be much cleaner. Thank you very for your help and the suggestions!