Calculate Value (Data Management) (ArcInfo only) |
|
Release 9.2
Last modified November 29, 2010 |
![]() ![]() ![]() Print all topics in : "Tools" |
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 CalculateValue tool will evaluate simple mathematical expressions. For example:
The CalculateValue tool allows the use of the python "math" module to perform more complex mathematical operations. The math module is accesses 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 wish 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 CalculateValue tool. They should simply be enclosed in percent signs (see above).
When running the CalculateValue 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 CalculateValue 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 the arcgis.rand() are:
to calculate a random value derived from a uniform distribution of integers between 0 and 10, use
to calculate a random value derived from a normal distribution with a mean of 10 and standard deviation of 3, use
see tip above for further options and a full description of each distribution supported. Note: "arcgis.rand" should always be entered in lower case 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, it 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 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 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}
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 |
CalculateValue "arcgis.rand('Integer 0 100')"
Scripting syntax
About getting started with writing geoprocessing scripts
CalculateValue_management (expression, code_block)
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 |
import arcgisscripting gp = arcgisscripting.create() for x in range(0, 50): print gp.CalculateValue("arcgis.rand('Integer 0 100')")