Field calculator

768
4
Jump to solution
03-10-2022 11:17 PM
Labels (3)
FOUADHAMDAN_SAAD
New Contributor II

Hi community....

I need your help to create python script  to calculate the value of  the field that called "code" type short integer according to following condition:

def reclass(industrial_area):

if (industrial_area > two_third_neigh_area):

return 13

elif (industrial_area > one_third_neigh_area and industrial_area< two_third_neigh_area):

return 16

elif (industrial_area < one_third_neigh_area):

return unclassified 

 I tried to apply the above if statement but I got this message:

FOUADHAMDAN_SAAD_0-1646982956226.png

 

0 Kudos
1 Solution

Accepted Solutions
JayantaPoddar
MVP Esteemed Contributor

In the reclass() function you have defined a single variable "industrial_area", whereas you have called three variables in the code "industrial_area", "one_third_neigh_area", "two_third_neigh_area".

You should add three variables in the function, and also define the appropriate fields in the expression.

 

Moreover, I don't understand "unclassified"? If this is a value, it won't fit in a Short integer type. You could replace it with values like 0 or 99.

 

e.g.: Assuming the layer has three numeric fields viz., "industrial_area", "one_third_neigh_area", "two_third_neigh_area"

Expression:

reclass(!industrial_area!, !one_third_neigh_area!, !two_third_neigh_area!)

Code Block:

def reclass(industrial_area, one_third_neigh_area, two_third_neigh_area):
    if (industrial_area > two_third_neigh_area):
        return 13
    elif ((industrial_area > one_third_neigh_area) and (industrial_area< two_third_neigh_area)):
        return 16
    elif (industrial_area < one_third_neigh_area):
        return 99

 



Think Location

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

The def needs to have the other field names passed to it since it has no values for them.  You only pass the function 1 field name (industrial_area).  And I would check ArcMap's syntax for passing field names to functions eg do they need to be enclosed in square brackets, exclamation marks like in ArcGIS Pro)


... sort of retired...
KimGarbade
Occasional Contributor III

You didn't include the other fields you were comparing in the function definition.

KimGarbade_0-1646994150351.png

 

JayantaPoddar
MVP Esteemed Contributor

In the reclass() function you have defined a single variable "industrial_area", whereas you have called three variables in the code "industrial_area", "one_third_neigh_area", "two_third_neigh_area".

You should add three variables in the function, and also define the appropriate fields in the expression.

 

Moreover, I don't understand "unclassified"? If this is a value, it won't fit in a Short integer type. You could replace it with values like 0 or 99.

 

e.g.: Assuming the layer has three numeric fields viz., "industrial_area", "one_third_neigh_area", "two_third_neigh_area"

Expression:

reclass(!industrial_area!, !one_third_neigh_area!, !two_third_neigh_area!)

Code Block:

def reclass(industrial_area, one_third_neigh_area, two_third_neigh_area):
    if (industrial_area > two_third_neigh_area):
        return 13
    elif ((industrial_area > one_third_neigh_area) and (industrial_area< two_third_neigh_area)):
        return 16
    elif (industrial_area < one_third_neigh_area):
        return 99

 



Think Location
FOUADHAMDAN_SAAD
New Contributor II

Thank you Sir.

I applied that and it is work perfectly. 

I appreciate all replays.  

 

0 Kudos