1

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

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 23, 2017 at 19:43
2
  • 1
    Try removing the space prior to : at the first if statement Commented May 23, 2017 at 19:59
  • 2
    The 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. Commented May 23, 2017 at 19:59

2 Answers 2

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.

Midavalo
30k11 gold badges53 silver badges108 bronze badges
answered May 23, 2017 at 21:09
0
1

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
answered May 24, 2017 at 1:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.