3

Using ArcGIS 10.1 for Desktop, I try to calculate a field using an 'if' statement in the field calculator. Here is my code :

"
#Expression
MyField_2 = def Reclass(!MyField_1!)

#Parser
Python 9.3

#Code Block
def Reclass(MyField1) :
    if MyField1 == -1 :
        return 0
    else :
        return !shape.area!
"

But I have an error returning !shape.area!. How can I fix that ?

  • I think !shape.area! also needs to be an argument in your Reclass function. But can you please post the error? – ustroetz Nov 6 '13 at 23:50
  • I have a 00089 error "Python parsing error line 5" – MerlinLelutin Nov 6 '13 at 23:56
4

shape_area has to be an argument in your Reclass function.

Your expression should look like this:

Reclass( !MyField_1!,!shape.area!)

And your script code like this:

def Reclass(MyField1,shape_area):
 if MyField1 == -1:
   return 0
 else:
   return (shape_area)
6

Instead of using a code block, you should be able to just do...

0 if !MyField_1! == -1 else !shape.area!
2

You are receiving an error because shape isn't being referenced.

One way to solve it would be to pass two parameters to your reclass function, like @ustroetz mentioned. The following code does this:

#Expression
Reclass(!MyField_1!, !shape.area!)

#Code block
def Reclass(test_field, shape):
  if test_field == -1:
    return 0
  else:
    return shape

#Or alternatively,
def Reclass(test_field, shape):
  return 0 if test_field == -1 else shape

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.