Field Calculator in Model Builder

1363
4
Jump to solution
07-05-2021 02:05 AM
ScottyMartin
New Contributor

I'm currently running analysis for Cross-Country mobility and need to use model builder to create a tool to run the whole process for my module. I've been playing around with the def Reclass(fld): script but I cant seem to get it to do what I'm after. Basically I want to add a Field Calculator that replaces Go, Restricted and Severely Restricted with 0, 1 & 2 so I don't have to mess around with the Reclass tool after converting the polygons to Raster as I have to manually reclass it every time I switch the weather field.

Any advice or help would be greatly appreciated!

ScottyMartin_0-1625475389411.png

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Just a bit of an understanding error of the syntax, you are also returning integer values into a string field, but I think arcmap copes with that ok in field calculator.

The 'Code Block' should look more like this:

 

def Reclass(field):
  if field =='Go':
    return 0
  elif field == 'Restricted':
    return 1
  elif field == 'Severly Restricted':
    return 2
  elif field == 'Unknown':
    return 2

 

and in 'Expression'

 

Reclass(!MOIST_GO!)

 

Although there's a few issues, you would only be updated a string field with new string numbers not actual integers which would be interpreted properly by the polygon to raster tool.  You would first have to create new integer fields to store these and then run calculate field on those.  Pro can directly create a new field from calculate field, but arcmap can't (can you use Pro?).

You could also iterate over fields in Pro ModelBuilder, but I don't think you can in arcMap.

Are you open to scripting?

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

Can you share the Reclass() code you tried? What were the issues with it? Changing the attribute values using that before it goes into a tool/model seems like the simplest method.

0 Kudos
ScottyMartin
New Contributor

Yeah just reclassifying them manually would be simple, but for the module the extra marks come from using automation to complete as much as possible from the base data input level, and showing innovation to the standard practices we are shown.

This is the code I have tried using, from what I understand of it I'm asking it to reclassify the Moist_Go Field, when it finds a value of "Go" it replaces it with a value of 0 which would then be readable via the polygon to raster tool, and does this for the other values.

def Reclass(!MOIST_GO!):
if !MOIST_GO! ==‘Go':
return 0
elif !MOIST_GO! == ‘Restricted':
return 1
elif !MOIST_GO! == 'Severly Restricted':
return 2
elif !MOIST_GO! == 'Unknown':
return 2

ScottyMartin_0-1625487464873.png

 

0 Kudos
DavidPike
MVP Frequent Contributor

Just a bit of an understanding error of the syntax, you are also returning integer values into a string field, but I think arcmap copes with that ok in field calculator.

The 'Code Block' should look more like this:

 

def Reclass(field):
  if field =='Go':
    return 0
  elif field == 'Restricted':
    return 1
  elif field == 'Severly Restricted':
    return 2
  elif field == 'Unknown':
    return 2

 

and in 'Expression'

 

Reclass(!MOIST_GO!)

 

Although there's a few issues, you would only be updated a string field with new string numbers not actual integers which would be interpreted properly by the polygon to raster tool.  You would first have to create new integer fields to store these and then run calculate field on those.  Pro can directly create a new field from calculate field, but arcmap can't (can you use Pro?).

You could also iterate over fields in Pro ModelBuilder, but I don't think you can in arcMap.

Are you open to scripting?

ScottyMartin
New Contributor

Ahh I think I see, so I should not of been overwriting the "Field" part with the actual field name and instead using the expression to define the field. Yes I had planned on using Iterator, I think you can use them in ArcMap but are limited to 1 per model as opposed to pro where you can use multiple per model.

Yes I do have access to ArcPro, however my course is the last course running through the modules on ArcMap, I assume I could just build my model in pro and use the final feature classes in ArcMap? or will I be stuck to using only Pro then? sounds like its better if it can automatically create a new field as that would make things loads easier for me.

0 Kudos