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 ?
3 Answers 3
Instead of using a code block, you should be able to just do...
0 if !MyField_1! == -1 else !shape.area!
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)
-
Great, this works !MerlinLelutin– MerlinLelutin2013年11月07日 17:38:39 +00:00Commented Nov 7, 2013 at 17:38
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
Explore related questions
See similar questions with these tags.
!shape.area!
also needs to be an argument in yourReclass
function. But can you please post the error?