Calculate Value (Data Management) (ArcInfo only) |
|
Release 9.3
Last modified March 8, 2012 |
![]() ![]() ![]() Print all topics in : "Tools" |
NOTE: This topic was updated for 9.3.1.
NOTE: This tool is available with an ArcView or ArcEditor license if the Spatial Analyst extension or the 3D Analyst extension is installed.
Returns a value based on a user-specified Python expression
Usage tips
Expressions can be created in a standard Python format only. Other scripting languages are not supported.
The Calculate Value tool will evaluate simple mathematical expressions. For example:
The Calculate Value tool allows the use of the Python "math" module to perform more complex mathematical operations. The math module is accessed by preceding the desired function with "math.". For example:
Constants are also supported through the math module. For example:
Variables created in either ModelBuilder or at the command line can be used by this tool. Simply enclose the variable name in percent signs (%). For example, if you want to divide the variable named Var1 by 100, your expression would be %Var1% / 100. Note: In the previous expression, if Var1 = 123, the expression will return 1. To get decimal places, add decimals to the values in the expression. For example: %Var1% / 100.00 will return 1.23.
In ModelBuilder, variables desired for use in the expression parameter cannot be connected to the Calculate Value tool. They should simply be enclosed in percent signs (see above).
When running the Calculate Value tool at the command line, the expression parameter is treated as a string. Therefore, it must be enclosed within quotes. For example, if you wanted to add 2 and 2 at the command line, you would type
CalculateValue "2 + 2"
The arcgis.rand() function is supported by the Calculate Value 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. For a description of the distributions and how they are generally used, see Distributions for assigning random values.
Examples of using arcgis.rand() are
See the tip above for further options and a full description of each distribution supported. Note: "arcgis.rand" should always be entered in lowercase characters.
Generally, users will type their expressions in the Expression parameter. More complicated expressions, such as multiline calculations or logical operations (if, then, etc.), will require the use of the Code Block parameter. The Code Block parameter cannot be used on its own and must be used in conjunction with the Expression parameter.
Variables defined in the Code Block parameter can be referenced from the expression.
Functions can be defined in the Code Block and called from the Expression. In the example below, our function will return a string wind direction based on a random input value. In Python, functions are defined using the "def" keyword followed by the name of the function and the function's input parameters. In this case, the function is called "getWind" and has one parameter called "wind". Values are returned from a function using the "return" keyword.
In Python, part of the syntax is white space. The amount of white space does not matter as long as it is consistent throughout the code block.
It is even possible to create the geoprocessor in the code block and use it to run other tools.
You cannot access model variables from the code block. Such variables must be passed to the code block from the expression. This can be achieved by creating a definition in the code block and referencing the definition in the expression.
The Data Type parameter should be used in ModelBuilder to help chain the Calculate Value tool with other tools. For example, if you use the Calculate Value tool to calculate a distance for use as input to the buffer distance parameter of the buffer tool, specify Linear Unit for the Data Type parameter.
See examples of calculating statistics with Calculate Value.
The following environment settings affect this tool: Extent, workspace, and scratch workspace.
Command line syntax
An overview of the Command Line window
CalculateValue_management <expression> {code_block} {data_type}
Parameter | Explanation | Data Type |
<expression> |
The Python expression to be evaluated. |
SQL Expression |
{code_block} |
Additional Python code. Code in the code block can be referenced in the expression parameter. |
String |
{data_type} |
The data type of the output returned from the Python expression. This parameter should be used in ModelBuilder to help chain Calculate Value with other tools. |
String |
CalculateValue "arcgis.rand('Integer 0 100')"
Scripting syntax
About getting started with writing geoprocessing scripts
CalculateValue_management (expression, code_block, data_type)
Parameter | Explanation | Data Type |
expression (Required) |
The Python expression to be evaluated. |
SQL Expression |
code_block (Optional) |
Additional Python code. Code in the code block can be referenced in the expression parameter. |
String |
data_type (Optional) |
The data type of the output returned from the Python expression. This parameter should be used in ModelBuilder to help chain Calculate Value with other tools. |
String |
import arcgisscripting gp = arcgisscripting.create() for x in range(0, 50): print gp.CalculateValue("arcgis.rand('Integer 0 100')")