Hey guys,
hopefully you all started the new year well!
My problem is as follows (by the way I am new in programming with python in GIS):
There are 5 different slope-classes for my GIS analysis. The attribute table shows how many points per area belong to a certain slope-class (Example: Area 1: Slope 1 - 10 points, Slope 2 - 20 points, Slope 3 - 30 pints, Slope 4 - 10 points, Slope 5 - 0 points). In the table there are a hundred different areas, each row represents one area.
The goal is to determine a slope tendency for each area. The idea was to calculate the maximum of the different counted slope points for each area and then say that for example area 1 has a slope tendency of 3.
I tried to make an ifelse statement with integrated max function in the field calculator
Pre-logic script code:
def Ergebnis (!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5):
if Join_Count_1 = max([!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5!]):
return "1"
elif Join_Cou_2 = max([!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5!]):
return "2"
elif Join_Cou_3 = max([!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5!]):
return "3"
elif Join_Cou_4 = max([!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5!]):
return "4"
elif Join_Cou_5 = max([!Join_Count_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!, !Join_Cou_5!]):
return "5"
else
return "No Data"
Expression:
max([!Join_Count!, !Join_Cou_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!])
When I am running the field calculator I always get a python syntax/parsing error. In a different version of this code I also got the indentation error in python.
Could somebody please help me and state where my errors are respectively show me where my way of thinking is wrong?? Maybe there are alternatives how to solve this problem????
Thank you very much in advance!!
In your pre-logic code, don't use exclamations around your variable definitions. The exclamations are for when you are passing the fields as arguments to your function, not as variables within your function. For example:
def Ergebnis (JC1, JC2, JC3, JC4, JC5):
if JC1 = max(JC1, JC2, JC3, JC4, JC5):
return "1"
elif JC2 = max(JC1, JC2, JC3, JC4, JC5):
return "2"
# fill in remaineder code
else
return "No Data"
Also, for your expression, you want to call your function, otherwise your code will never be executed:
Ergebnis(!Join_Count!, !Join_Cou_1!, !Join_Cou_2!, !Join_Cou_3!, !Join_Cou_4!)
Thank you very much for your help! It worked nicely.
def Ergebnis(a, b, c, d, e):
"""return the maximum of the 5 columns"""
m = max(a, b, c, d, e)
return a.index(max(a)) + 1
#expression
Ergebnis(!Join_Count!,!Join_Cou_1!,!Join_Cou_2!,!Join_Cou_3!,!Join_Cou_4!)
sample on a list
a = [1,3,2,5,4]
a.index(max(a)) + 1
4 # --- index of the max, counting from 1
Thank you very much for your help!