1

Using ArcGIS 10.4. I'm trying to reclassify values from a field (ECOREGION_DIVISION), which is in string format, into new values using the following Python syntax:

def Reclass(ECOREGION_DIVISION):
 if (ECOREGION_DIVISION == "Desert"):
 return "Not forest"
 else:
 return "Forest"
 
 Reclass(!ECOREGION_DIVISION!)

The result is that all the values in the new_class field are returned as "Forest" (the if statement does not return "Not forest" when value = "Desert"). The new_class field is also string format. Does this syntax seem correct? enter image description here

menes
1,4272 gold badges8 silver badges25 bronze badges
asked May 16, 2020 at 2:52
8
  • Why are you passing Code_new to the Reclass function? You should be passing Code1. Furthermore, what type is Code1? If it happens to be text the if statement will always return 2. Commented May 16, 2020 at 3:19
  • Hi Marcelo: I've revised the syntax for a more realistic version of the code I'm trying to construct. The initial field is in string format and I want to rename certain records for a simpler classification field. I've switched out the input argument for the field I want to reclass and I still get a similar result. Commented May 16, 2020 at 6:08
  • 1
    Your syntax seems fine. Do the values happen to have a whitespace at the end? Commented May 16, 2020 at 6:14
  • 3
    if (ECOREGION_DIVISION.strip() == "Desert"): Commented May 16, 2020 at 6:39
  • That does not appear to be the issue, there are no spaces from what I can tell, and the .strip() function doens't work. I think that there may be something wrong with the field calculator since built in functions like .replace() don't work. I've read that I may need to reinstall arcgis and delete the registry and c folders? Commented May 16, 2020 at 19:31

1 Answer 1

0

Solution:

I ended up creating a field of the class codes rather than the Ecoregion text as the input (as either a string or integer to test out). I made sure to create syntax according to the new fields (string vs integer). The Reclass function now classifies as either a text or number field (important to use quotes for the string).

Here is the python syntax for string reclass:

def Reclass(new_class):
 if new_class == "240":
 return "Marine"
 else:
 return "Not Marine"
Reclass( !new_class!)

enter image description here

And this is the syntax for a number reclass:

def Reclass(new_code):
 if new_code == 240:
 return 1
 else:
 return 2
Reclass( !new_code!)

enter image description here

I'm not exactly sure if the reason this works now is because I am using the class code field rather than the class field itself, or due to some other tweaking such as setting up correct field types.

answered May 17, 2020 at 19:18

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.