Calculate Field (Data Management) |
|
Release 9.2
Last modified November 29, 2010 |
![]() ![]() ![]() Print all topics in : "Tools" |
Calculates the values of a field for a feature class, feature layer, or raster catalog.
The input table will be modified; a copy should be made to preserve the original information.
Usage tips
Calculate Field computes and assigns a value to the specified field of the Input table.
Expressions can be created in a standard Visual Basic (VB) format or in a standard Python format. The formatting style of the string used for the expression should be appropriate to the environment (type).
Python expressions can be created using properties from the geometry object (Type, Extent, Centroid, FirstPoint, LastPoint, Area, Length, IsMultipart, and PartCount).
In the tool's dialog, an expression can be entered directly into the Expression parameter, or interactively built using the Field Calculator.
Calculate Field will work with a selected set of features such as those created from a query in Make Feature Layer or Select Layer by Attribute.
The calculation can only be applied to one field per operation.
When calculating new values for a field, existing values will be overwritten. Retain a copy of the input table before using Calculate Field in case an error is made.
Fields are always enclosed in square brackets [ ] for VB.
Fields are always enclosed in exclamation points ! ! for Python.
To calculate strings to text or character fields, the string must be double-quoted (for example, "string") in the dialog box, whereas at the command line and in scripting, the double-quoted string must also be encapsulated in single quotes (for example, '"string"').
Calculate Field can also be used to update character items. Expressions using character string should be wrapped using single quotes, for example: [CHARITEM] = 'NEW STRING'. However, if the character string has embedded single quotes, wrap the string using double quotes. For example: [CHARITEM] = "TYPE'A'".
To calculate a field to be a numeric value, enter the numeric value in the Expression parameter; no quotes around the value are required.
The expression and code block are connected. The code block must relate back to the expression; the result of the code block should be passed into the expression. For example:
CalculateField_management C:\WUTemp\polygon_Copy.shp NEAR_X x PYTHON 'x = 5'
The expression is 'x', and the code block is 'x = 5'.
The Code Block parameter allows you to create complex expressions. You can enter the code block directly on the dialog, or as a continuous string at the command line or in scripting.
The Python math modules and formatting are available for use in the Code Block parameter. Additional modules can be imported. The math module provides number-theoretic and representation functions, power and logarithmic functions, trigonometric functions, angular conversion functions, hyberbolic functions and mathematical constants. To learn more about the math module, see Python's help.
When calculating joined data, you cannot calculate the joined columns directly. However, you can directly calculate the columns of the origin table. To calculate the joined data, you must first add the joined tables or layers to ArcMap. You can then perform calculations on this data separately. These changes will be reflected in the joined columns.
In a stand-alone script, calculations using Expression Type's PYTHON option must use the arcgisscripting module. Stand-alone scripts that use win32com.client module to create the geoprocessor will fail with a "Python expression is blocked when using a dispatch based application" error.
The expression type must be Python when running Calculate Field with ArcGIS Engine or ArcGIS Server. Visual Basic for Applications (VBA) is required for many types of VB expressions and VBA is only available when Calculate Field is run by ArcGIS Desktop. Only use Python as the expression type whenever the tool is included in a model that will be published to an ArcGIS Server.
The arcgis.rand() function is supported by the CalculateField tool. The arcgis.rand() function has been created for ArcGIS tools and should not be confused with the Python Rand() function. The syntax for the available distributions for the arcgis.rand() function can be found at The distribution syntax for random values.
The following environment affects this tool: workspace.
Command line syntax
An overview of the Command Line window
CalculateField_management <in_table> <field> <expression> {VB | PYTHON} {code_block}
Parameter | Explanation | Data Type |
<in_table> |
The table that contains the rows from a specified field that will be calculated. The updated values will be added to this table. |
Table View | Raster Layer |
<field> |
The field that will be updated with the new calculation. |
Field |
<expression> |
The simple calculation expression used to create a value that will populate the selected rows. |
SQL Expression |
{VB | PYTHON} |
Specify the type of expression that will be used.
|
String |
{code_block} |
Allows for a block of code to be entered for complex expressions. |
String |
CalculateField_management c:\test data\harvestable.shp Growth '[age] / [height]' VB
CalculateField_management c:\test data\harvestable.shp NEAR_X "!COUNT_X! / !SHAPE.AREA!" PYTHON #
CalculateField_management c:\test data\harvestable.shp NEAR_X x PYTHON 'x = 5'
CalculateField_management c:\test data\harvestable.shp NEAR_X x PYTHON 'import random;x = random.random()'
Scripting syntax
About getting started with writing geoprocessing scripts
CalculateField_management (in_table, field, expression, expression_type, code_block)
Parameter | Explanation | Data Type |
in_table (Required) |
The table that contains the rows from a specified field that will be calculated. The updated values will be added to this table. |
Table View | Raster Layer |
field (Required) |
The field that will be updated with the new calculation. |
Field |
expression (Required) |
The simple calculation expression used to create a value that will populate the selected rows. |
SQL Expression |
expression_type (Optional) |
Specify the type of expression that will be used.
|
String |
code_block (Optional) |
Allows for a block of code to be entered for complex expressions. |
String |
import arcgisscripting gp = arcgisscripting.create() # Set a default workspace gp.workspace = "c:/test_data" try: # Calculate field to a new value gp.CalculateField_management("harvestable.shp", "growth", "[age] / [height]", "VB") gp.CalculateField_management("harvestable.shp", "NEAR_X", "x", "PYTHON", "'import random;x = random.random()'") except: # If an error occurs when running Addfield, print out the error message. print gp.GetMessages(2)EXAMPLE 2: PYTHON-based Calculation using geometry properties
# Calculate x and y centroid fields using the geometry property Centroid import arcgisscripting, sys gp = arcgisscripting.create() inputFC = sys.argv[1] gp.AddField_management(inputFC, "xCentroid", "DOUBLE", 18, 11) gp.AddField_management(inputFC, "yCentroid", "DOUBLE", 18, 11) # Centroid property returns a string with x and y separated by a space xExpression = "float(!SHAPE.CENTROID!.split()[0])" yExpression = "float(!SHAPE.CENTROID!.split()[1])" gp.CalculateField_management(inputFC, "xCentroid", xExpression, "PYTHON") gp.CalculateField_management(inputFC, "yCentroid", yExpression, "PYTHON")EXAMPLE 3: PYTHON-based Calculation using codeblock parameter
# Calculate an area class based on the size of polygons import arcgisscripting, sys gp = arcgisscripting.create() # An input polygon feature class inputFC = sys.argv[1] gp.AddField_management(inputFC, "areaclass", "short") # Calculation is based on a custom getclass definition expression = "getclass(float(!shape.area!))" codeblock = "def getclass(area): if area <= 1000: return 1 if area > 1000 and area <= 10000: return 2 else: return 3" gp.CalculateField_management(inputFC, "areaclass", expression, "PYTHON", codeblock)