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.
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:
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:
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!