I have created a model in ArcGIS 10.2 that iterates through some feature classes in order to add a field 'Zmin10' (double) and calculate the field value based on a variable (called "Minimum") to which it adds a different value based on the feature class name ("outName").

Here is the model:

enter image description here

The Calculate Field Tool ('Recalculate Field Zmin10') is set as following:

Expression: shift( !Zmin10! )

Expression type: Python

Code Block:

def shift(Zmin10):
if %outName% == 'toc':
    return (( float(%Minimum%) ) - 20)
elif %outName% == '2016':
    return (( float(%Minimum%) ) - 40)
elif %outName% == '2017':
    return (( float(%Minimum%) ) - 60)
    return (( float(%Minimum%) ) - 80)

The code block does not work at all for the feature class named 'toc', where I get the following error:

Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 2, in shift
NameError: global name 'toc' is not defined

For the other options, it always returns the value according to the "else" part of the statement, as if somehow it does not recognise the other values either.

I do not understand exactly where the code is wrong and why.

For more clarity, the Python script is attached:

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Script arguments
Input_Folder = arcpy.GetParameterAsText(0)
if Input_Folder == '#' or not Input_Folder:
    Input_Folder = "D:\\ArcGIS\\Scripts\\ModelTransects\\test_final\\03_SHP\\Tempor" # provide a default value if unspecified

Minimum = arcpy.GetParameterAsText(1)
if Minimum == '#' or not Minimum:
    Minimum = "-157.3" # provide a default value if unspecified

# Local variables:
path_points_sel__1_ = Input_Folder
path_points_sel2 = path_points_sel__1_
path_points_sel__2_ = path_points_sel2
Name = Input_Folder
outName = Name

# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(Input_Folder, "*points_sel*", "POINT", "RECURSIVE")

# Process: Calculate Value
arcpy.CalculateValue_management("changeName(\"%Name%\")", "def changeName(Name):\\n    return str(Name[16:])", "String")

# Process: Add Field
arcpy.AddField_management(path_points_sel__1_, "Zmin10", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Recalculate Field 'Zmin10'
arcpy.CalculateField_management(path_points_sel2, "Zmin10", "shift( !Zmin10!, %outName% )", "PYTHON", "def shift(Zmin10, outName):\\n    outName = %outName%\\n    if outName == 'toc':\\n        return (( float(%Minimum%) ) - 20)\\n    elif outName == '2016':\\n        return (( float(%Minimum%) ) - 40)\\n    elif outName == '2017':\\n        return (( float(%Minimum%) ) - 60)\\n    else:\\n        return (( float(%Minimum%) ) - 80)")
  • 1
    Just an observation: why does %Minimum% show up blue while %outName% does not? Where does %outName% get defined and is its value being carried into the shift function? The reason it goes right to else is because it can't find a case where %outName% is equal to one of those. So, either the values are wrong or the %outName% variable is. Also, I assume your if and elifs are indented under def and it's just your copy/paste that shows them at the left edge? – J Kelly Jan 26 at 22:50
  • I have no clue about the colours in the post. However, %outName% is sliced from the name of the shapefiles in the iteration and is a string. Indentation in the model is correct, I have double-checked the syntax. – Serena Jan 28 at 8:08
  • Ok, you might need to pass %outname% into the function. def shift(Zmin10, %outname%) or maybe (if you previously defined outname = %outname%) def shift(Zmin10, outname). – J Kelly Jan 28 at 15:20
  • The shift function knows about the field Zmin10 and can go figure things out about it (Minimum). It doesn't maybe know about the shapefiles and therefore can't go fetch the right one's name. – J Kelly Jan 28 at 15:23
  • Thanks for the suggestion and the explanation, I understand the point now. Still the calculation does not work and the iteration again goes directly to else . Thus something else must be wrong. – Serena Jan 29 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.