com.esri.arcgis.geoprocessing.tools.datamanagementtools
Class CalculateValue
java.lang.Object
com.esri.arcgis.geoprocessing.AbstractGPTool
com.esri.arcgis.geoprocessing.tools.datamanagementtools.CalculateValue
- All Implemented Interfaces:
- GPTool
public class CalculateValue
- extends AbstractGPTool
Calculate Value tool returns a value based on a specified Python expression.
The Calculate Value tool is contained in the Data Management Tools tool box.
Usage tips:
- This tool is intended for use in ModelBuilder and not in Python scripting.
- The Data Type parameter is used in ModelBuilder to help chain the output of 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.
- Variables created in ModelBuilder can be used by this tool, but variables desired for use in the expression parameter cannot be connected to the Calculate Value tool. To use them in the expression, enclose the variable name in percent signs (%). For example, if you want to divide a variable named 'Input' by 100, your expression would be %Input%/100.Note: in the previous expression, if Input = 123, the expression will return 1. To get decimal places, add decimals to the values in the expression. For example: %Input%/100.00 will return 1.23. The illustration below shows another example of using variables in the expression.In-line variable of type string should be enclosed within quotes ("%string variable%") in an expression. In-line variables of type numbers (double, long) do not require quotes (%double%).
- Expressions can be created in a standard Python format ONLY. Other scripting languages are not supported.
- The Calculate Value tool can evaluates 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:
- The arcgis.rand() function is supported. The arcgis.rand() function has been created for ArcGIS tools and should not be confused with the Python Rand() function. Examples of using the arcgis.rand() are as follows:
- Calculate a random value derived from a uniform distribution of integers between 0 and 10: arcgis.rand("Integer 0 10")
- Calculate a random value derived from a normal distribution with a mean of 10 and standard deviation of 3: arcgis.rand("Normal 10 3")
The expression "arcgis.rand" must be entered in lowercase characters.
- Generally, you will type the expressions in the Expression parameter. More complicated expressions, such as multiline calculations or logical operations (if, then), 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 parameter and called from the expression. In the example below, the function returns a wind direction string 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 getWind and has one parameter, wind. Values are returned from a function using the return keyword.
- To use Calculate Value with tools like Buffer that require a buffer distance value as well as a linear unit you have to return the linear unit in the code block along with the distance value as shown in the example below:
- In Python, part of syntax is proper indentation. Indentation level (two spaces or four spaces) does not matter as long as it is consistent throughout the code block.
- 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 box.
Constructor Summary |
CalculateValue()
Creates the Calculate Value tool with defaults. |
CalculateValue(Object expression)
Creates the Calculate Value tool with the required parameters. |
CalculateValue
public CalculateValue()
- Creates the Calculate Value tool with defaults.
Initializes the array of tool parameters with the default values specified when the tool was created.
CalculateValue
public CalculateValue(Object expression)
- Creates the Calculate Value tool with the required parameters.
Initializes the array of tool parameters with the values as specified for the required parameters and with the default values for the other parameters.
- Parameters:
expression
- the Python expression to be evaluated.
getExpression
public Object getExpression()
- Returns the Expression parameter of this tool .
This parameter is the Python expression to be evaluated.
This is a required parameter.
- Returns:
- the Expression
setExpression
public void setExpression(Object expression)
- Sets the Expression parameter of this tool .
This parameter is the Python expression to be evaluated.
This is a required parameter.
- Parameters:
expression
- the Python expression to be evaluated.
getCodeBlock
public String getCodeBlock()
- Returns the Code Block parameter of this tool .
This parameter is additional Python code. Code in the code block can be referenced in the Expression parameter.
This is an optional parameter.
- Returns:
- the Code Block
setCodeBlock
public void setCodeBlock(String codeBlock)
- Sets the Code Block parameter of this tool .
This parameter is additional Python code. Code in the code block can be referenced in the Expression parameter.
This is an optional parameter.
- Parameters:
codeBlock
- additional Python code. Code in the code block can be referenced in the Expression parameter.
getDataType
public String getDataType()
- Returns the Data type parameter of this tool .
This parameter is 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.
This is an optional parameter.
- Returns:
- the Data type
setDataType
public void setDataType(String dataType)
- Sets the Data type parameter of this tool .
This parameter is 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.
This is an optional parameter.
- Parameters:
dataType
- 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.
getValue
public Object getValue()
- Returns the output_value parameter of this tool (Read only).
This is an derived parameter.
- Returns:
- the output_value
getToolName
public String getToolName()
- Returns the name of this tool.
- Returns:
- the tool name
getToolboxName
public String getToolboxName()
- Returns the name of the tool box containing this tool.
- Returns:
- the tool box name
getToolboxAlias
public String getToolboxAlias()
- Returns the alias of the tool box containing this tool.
- Returns:
- the tool box alias