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 ?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 6, 2013 at 23:48
2
  • I think !shape.area! also needs to be an argument in your Reclass function. But can you please post the error? Commented Nov 6, 2013 at 23:50
  • I have a 00089 error "Python parsing error line 5" Commented Nov 6, 2013 at 23:56

3 Answers 3

6

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

0 if !MyField_1! == -1 else !shape.area!
answered Nov 7, 2013 at 1:07
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)
answered Nov 7, 2013 at 0:12
1
  • Great, this works ! Commented Nov 7, 2013 at 17:38
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
answered Nov 7, 2013 at 0:13

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.