I want to perform a simple calculation with the Field Calculator using this piece of script (note that all the elif statements are attempts for another syntax and non of the worked):
def change(OldFor_Com):
if (OldFor_Com) == "OHWH":
return shape.area@Hectares / 40
elif (OldFor_Com) == "OHWH":
return !shape.area@Hectares / 30!
elif (OldFor_Com) == "OHWH":
return (!shape.area@Hectares / 30!)
elif (OldFor_Com) == "OHWH":
return !shape.area@Hectares! / 30
elif (OldFor_Com) == "OHWH":
return (!shape.area@Hectares! / 30)
if (OldFor_Com == "OTHH"):
return !shape.area@hectares! / 40
If the field is populated with OTHH
I want another field to be populated with the area of the field, divided by 40, or 30, or... and so on.
I modified my syntax a couple of times (as you can see) but it still says Invalid Syntax
.
Is a calculation possible with these if statement or do I need a workaround?
Note that the shape.area@Hectares is an expression to calculate the area of the field, and not another field in my attributetable!
EDIT: 7/15/2014 around 11pm
I modified the script. I added a field to the table and calculate the area of the shape first (with !shape.area@Hectares!) and then I try to run this
def change(OldFor_Com):
if OldFor_Com == "OTHH":
return Area_Ha / 40
if OldFor_Com == "OHWH":
return Area_Ha / 30
if OldFor_Com == "OSFH":
return Area_Ha / 375
if OldFor_Com == "PINE":
return Area_Ha / 10
if OldFor_Com == "OOFH":
return Area_Ha / 375
if OldFor_Com == "None":
return Area_Ha
else:
return 0
It bounces back with the following:
ERROR 000539: Error running expression: change(u"OTHH", 7)
Traceback (most recent call last):
File "<expression>", line 1, in <module>
TypeError: change() takes exactly 1 argument (2 given)
Failed to execute (Calculate Field (10)).
Is it not possible to do any calculations in the if statement?
EDIT: 7/15/14 12:30pm
This how the attributestable looks like before I add the field SizeScore
After I add the field SizeScore
(FLOAT).
And the following screen comes up if I use this code:
def change(OldFor_Com, Area_Ha):
if OldFor_Com == "OTHH":
x = Area_Ha / 40
return x
if OldFor_Com == "OHWH":
x = Area_Ha / 30
return x
if OldFor_Com == "OOFH":
x = Area_Ha / 375
return x
if OldFor_Com == "OSFH":
x = Area_Ha / 375
return x
if OldFor_Com == "PINE":
x = Area_Ha / 10
return x
if OldFor_Com == "None":
return Area_Ha
else:
return 0
It only populates the rows where it does not need to calculate. The only statement that is working is that one.
if OldFor_Com == "None":
return Area_Ha
But for the others are no error messages coming back!
SOLVED, SEE ACCEPTED ANSWERE Thanks a lot to everybody who went through this with me!