Field calculator and model builder

7077
6
06-08-2010 11:54 AM
akikoelders
Emerging Contributor
Hi,
I am working on a project where I need to use the calculate field tool within model builder.

What I have done so far is:
to create two feature layers (created by selecting 2 different attributes) and I have identified where the layers intersect (use select by location where layer 1 and layer 2 intersect).

goal:
The select by location tool then creates a selection (where intersection occurs), which I then need to do a field calculate on to change all the values that are selected.

The problem:
when I do the field calculation, the values in the attribute table change, but not permanently. If I were doing this manually I would just right click the field and do a field calculation and all the selected values would change. When I do this same sort of thing in model builder, the values only change until I need to move on to the next step or I clear the selection.

Is there anyway to solve this problem? Am I just using the wrong tool?

thanks for the help!

UMDproject21
0 Kudos
6 Replies
MatthewToro
Deactivated User
Hi,

Just a shot in the dark, but why not add a new field first (with Add Field) and then calculate that field (with Calculate Field) for those selected using the same SQL syntax you used for the selection?
0 Kudos
akikoelders
Emerging Contributor
This is a really great suggestion.  I have gotten far using this... although I bet there is something even simpler that I am missing (if anyone knows it :D).  Thanks for the helpful suggestion it def. has helped me to move forward on my project!
0 Kudos
ShitijMehta
Esri Regular Contributor
You layer should retain the calculated value as the calculate field only modifies the input and does not create a new output. But I do not know what going on. Try this:

You can use the Copy tool in the end or after the Calculate Field tool. Connect your input feature classes (blue oval you are making the selection on) not the selected features in a layer and set a precondition on this copy tool to only run after the calculate field has been run.
0 Kudos
akikoelders
Emerging Contributor
Okay so if you are having this same problem I think I found a pretty simply answer.  If you use VBA script and enter an if then statement you don't even have to do a selection at all (which is where I was having the problem-- the selection was not changing the attribute table after a field calculation)

so (in model builder)
if you have a few values in the attribute table that need to be changed, normally you would do select by attribute then field calculator and be done.  This method did not work for me.  So one way I found is go right into field calculator and enter an if then statement.

ex: say I have values 1-10 in a field called NUM in my table and I want all the 1 to become 10 this is what I would do
1 select the field calculator tool and select advanced to get the VBA script box (make sure the only for selected record is not on)
2 create a random variable to hold you number as whatever your field (double, float...) is then start the if then statment
  dim number as double
if [NUM]=1 then
number=10
elseif  [NUM]=2 then
number=2
[NUM]=3then
number=3
.
.                       and so on until you hit 10 or whatever your end value is.
. end if


make sure you end the if then statement if endif
also make sure you include all of your values for example DO NOT just say if num=1 change to 10 and end the if because arc will most likely change all other values to 0 which you do not want.  

hope this helps people who are in my position because it took me many days to find this short cut.
0 Kudos
RickWahl
Emerging Contributor
I'm having a similar problem with my if statement in the field calculator.  I've joined two tables for point features and want to copy the street address (Street_Number) from the joined table to the existing field (StreetNumber) only if the existing field is blank.

Here's what I have so far:

#Copy Street Number to Curbstop from Parcel if Streetnumber is blank
#This code does not include the spatial join this is based off of, verify
#that CurbStop_SpatialJoin1 is available


#Import modules
import arcpy
from arcpy import env

#Set environment
#Location for gisintern pc local copy of NR Municipal DB
#Change to use elsewhere
env.workspace = "C:\Users\gisintern\Documents\NewRichmondMunicipal.mdb"

#Set variables for field calculations
inTable = "Water\CurbStop"
fieldname =  "StreetNumber"
expression = "copystreetno(str(!CurbStop_SpatialJoin1.Street_Number!))"

codeblock = """def copystreetno(CurbStop_SpatialJoin1.Street_Number):
    parcelstnum = CurbStop_SpatialJoin1.Street_Number
    CSstnum = CurbStop.StreetNumber

    if CSstnum = "":
        CSstnum = parcelstnum

    return CSstnum """

#Calculate Field
arcpy.CalculateField_management(inTable, fieldname, expression, "PYTHON", codeblock)



This returns this error in the Python window in ArcMap 10
"Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)"

and this error if run in the python shell
"Traceback (most recent call last):
  File "C:/Users/gisintern/Documents/parcel_st_num_to_CS", line 30, in <module>
    arcpy.CalculateField_management(inTable, fieldname, expression, "PYTHON", codeblock)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 2727, in CalculateField
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)
Failed to execute (CalculateField)."

Any ideas where I'm going wrong?
0 Kudos
RickWahl
Emerging Contributor
Looks like the indentation in the codeblock didn't copy correctly.  I'm pretty sure my indentations below the codeblock heading and the if statement are correct.

If anyone can catch any syntax issues outside of that I'd love to know.
0 Kudos