I'm writing a conditional in the Field Calculator and it's giving me an error of syntax in line 2. Can someone see it?
The Code Block is this:
def ZFactor(LAT_CENTR) :
if (10.000 < math.fabs(LAT_CENTR) and math.fabs(LAT_CENTR) => 00.000) :
return 0.00000898
elif (math.fabs(LAT_CENTR) => 10.000 and math.fabs(LAT_CENTR) < 20.000):
return 0.00000912
elif (math.fabs(LAT_CENTR) => 20.000 and math.fabs(LAT_CENTR) < 30.000):
return 0.00000956
elif (math.fabs(LAT_CENTR) => 30.000 and math.fabs(LAT_CENTR) < 40.000):
return 0.00001036
elif (math.fabs(LAT_CENTR) => 40.000 and math.fabs(LAT_CENTR) < 50.000):
return 0.00001171
elif (math.fabs(LAT_CENTR) => 50.000 and math.fabs(LAT_CENTR) < 60.000):
return 0.00001395
elif (math.fabs(LAT_CENTR) => 60.000 and math.fabs(LAT_CENTR) < 70.000):
return 0.00001792
elif (math.fabs(LAT_CENTR) => 70.000 and math.fabs(LAT_CENTR) < 80.000):
return 0.00002619
elif (math.fabs(LAT_CENTR) => 80.000 and math.fabs(LAT_CENTR) <= 90.000):
return 0.00005156
else:
return 1
-
1Try removing the space prior to : at the first if statementBera– Bera2017年05月23日 19:59:16 +00:00Commented May 23, 2017 at 19:59
-
2The greater-than-or-equal operator is ">=" not "=>". Given the presence of "return"s, your "elif"s could be just "if" and the space before the colons is bad form. Finally, best practice is four spaces.Vince– Vince2017年05月23日 19:59:19 +00:00Commented May 23, 2017 at 19:59
2 Answers 2
The following function should provide equivalent results:
def ZFactor(LAT_CENTR):
x = math.fabs(LAT_CENTR)
if x < 10.0:
return 0.00000898
elif x < 20.0:
return 0.00000912
elif x < 30.0:
return 0.00000956
elif x < 40.0:
return 0.00001036
elif x < 50.0:
return 0.00001171
elif x < 60.0:
return 0.00001395
elif x < 70.0:
return 0.00001792
elif x < 80.0:
return 0.00002619
elif x <= 90.0:
return 0.00005156
else:
return 1
math.fabs(LAT_CENTR)
is moved out of the if statement to eliminate redundant calculations of the same value. Depending on the distribution of that value, an average of 10 calculations per function call. Since the lower bound for each elif has already been tested in the previous test, you don't need to test for it in the next.
I'm guessing at what you were trying to do in the opening if statement; as written it is logically impossible, or always false. An absolute value will always be greater than zero, so a compound test there is not necessary either.
The syntax error will be due to your greater than or equal to being written back-to-front. Should be >=
not =>
. Also I think you want to test that 10
is greater than and not less than here.
if (10.000 > math.fabs(LAT_CENTR) and math.fabs(LAT_CENTR) >= 00.000) :
Occasionally python doesn't like to handle if: return, elif: return, elif: return etc., so I like to put a single return
at the end. Also, as math.fabs()
returns an absolute value, so there's no need to check that the value is not negative.
def ZFactor(LAT_CENTR):
x = math.fabs(LAT_CENTR)
y = 1
if (x < 10):
y = 0.00000898
elif (x < 20.000):
y = 0.00000912
elif (x < 30.000):
y = 0.00000956
elif (x < 40.000):
y = 0.00001036
elif (x < 50.000):
y = 0.00001171
elif (x < 60.000):
y = 0.00001395
elif (x < 70.000):
y = 0.00001792
elif (x < 80.000):
y = 0.00002619
elif (x <= 90.000):
y = 0.00005156
return y
Explore related questions
See similar questions with these tags.