Calculate Value (ModelBuilder)

Summary

Returns a value based on a specified Python expression.

Usage

  • This tool is intended for use in ModelBuilder, 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.

    Using model variable in Calculate Value tool
    Caution:

    Inline variable of type string should be enclosed within quotes ("%string variable%") in an expression. Inline 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:

    • 3+5
    • 9*8
    • 4+(9/3)
  • 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:

    • math.sqrt(25)
    • math.cos(0.5)
  • Constants are also supported through the math module. For example:

    • math.pi
  • The random module is supported for generating random numbers. Examples of using the random module are as follows:

    • Calculate a random integer between 0 and 10: random.randint(0, 10)
    • Calculate a random value derived from a normal distribution with a mean of 10 and standard deviation of 3: random.normalvariate(10, 3)

    Legacy:

    arcgis.rand() is no longer supported as of ArcGIS Pro 2.0. Comparable functions using Python's random module should be used instead. To use the random module successfully, add it as an import in the Code Block parameter.

  • 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.

    Using Expression and Code Block

  • 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.

    Defining functions in Code Block
  • You can pass variable through the Expression parameter and use if-else logic with inline variables in the code block as shown below. The code block checks to see if the Input Cell Size variable is empty then returns a value based on the condition.

    If-Else example

  • Python methods can be used directly in the Expression parameter of the tool. For example, if you have an input value with a decimal (field value of the input table in this case) and want to use the value in the output name of another tool through inline variable substitution, the decimal can be replaced using Python method replace in the Calculate Value tool expression.

    Python method replace example

  • Python modules can be called and methods such as replace combined or stacked in the code block parameter. In the example below the time module is imported in the code block which returns the current date and time such as Fri Mar 19 2010 09:42:39. This returned value is used as name in Create Folder tool to name the folder. Since the name of the folder cannot contain spaces or punctuation marks, the replace method in Python is used by stacking the method for each element that needs to be replaced. The resulting name of the folder in this example is FriMar192010094239.

    Python time module example.

  • If you are calculating a value in the model and want to use the calculated value with tools such as 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,
    • change the data type of the returned values as shown below, and
    • set the data type parameter to linear unit so that the result of the expression will be converted to a linear unit value.
    Using Calculate Value output as Buffer distance

  • You can use the output of Calculate Value tool directly in any Spatial Analyst tools which accept a raster or a constant value such as Plus, Greater Than, and Less Than (these tools are found in the Spatial Analyst toolbox/Math toolset). To use the output of Calculate Value, change the output data type to Formulated Raster. This output data type format is a raster surface whose cell values are represented by a formula or constant.

    Formulated Raster data type

  • 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.

  • Note:

    When writing Python scripts, use standard Python statements instead of the Calculate Value tool.

Parameters

LabelExplanationData Type
Expression

The Python expression to be evaluated.

SQL Expression
Code Block
(Optional)

A Python code block. Code in the code block can be referenced in the Expression parameter.

String
Data type
(Optional)

Specifies 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.

  • Address Locator —Address Locator
  • Address Locator Style —Address Locator Style
  • Analysis Cell Size —Analysis Cell Size
  • Annotation Layer —Annotation Layer
  • Any Value —Any Value
  • ArcMap Document —ArcMap Document
  • Areal Unit —Areal Unit
  • BIM File Workspace —BIM File Workspace
  • Boolean —Boolean
  • Building Discipline Layer —Building Discipline Layer
  • Building Discipline Scene Layer —Building Discipline Scene Layer
  • Building Layer —Building Layer
  • Building Scene Layer —Building Scene Layer
  • CAD Drawing Dataset —CAD Drawing Dataset
  • Calculator Expression —Calculator Expression
  • Catalog Root —Catalog Root
  • Cell Size —Cell Size
  • Cell Size XY —Cell Size XY
  • Composite Layer —Composite Layer
  • Compression —Compression
  • Coordinate System —Coordinate System
  • Coordinate Systems Folder —Coordinate Systems Folder
  • Coverage —Coverage
  • Coverage Feature Class —Coverage Feature Class
  • Data Element —Data Element
  • Data File —Data File
  • Database Connections —Database Connections
  • Dataset —Dataset
  • Date —Date
  • dBASE Table —dBASE Table
  • Decimate —Decimate
  • Diagram Layer —Diagram Layer
  • Dimension Layer —Dimension Layer
  • Disk Connection —Disk Connection
  • Double —Double
  • Elevation Surface Layer —Elevation Surface Layer
  • Encrypted String —Encrypted String
  • Envelope —Envelope
  • Evaluation Scale —Evaluation Scale
  • Extent —Extent
  • Extract Values —Extract Values
  • Feature Class —Feature Class
  • Feature Dataset —Feature Dataset
  • Feature Layer —Feature Layer
  • Feature Set —Feature Set
  • Field —Field
  • Field Info —Field Info
  • Field Mappings —Field Mappings
  • File —File
  • Folder —Folder
  • Formulated Raster —Formulated Raster
  • Fuzzy function —Fuzzy function
  • GeoDataServer —GeoDataServer
  • Geodataset —Geodataset
  • Geometric Network —Geometric Network
  • Geostatistical Layer —Geostatistical Layer
  • Geostatistical Search Neighborhood —Geostatistical Search Neighborhood
  • Geostatistical Value Table —Geostatistical Value Table
  • GlobeServer —GlobeServer
  • GPServer —GPServer
  • Graph —Graph
  • Graph Data Table —Graph Data Table
  • Group Layer —Group Layer
  • Horizontal Factor —Horizontal Factor
  • Image Service —Image Service
  • Index —Index
  • INFO Expression —INFO Expression
  • INFO Item —INFO Item
  • INFO Table —INFO Table
  • Internet Tiled Layer —Internet Tiled Layer
  • KML Layer —KML Layer
  • LAS Dataset —LAS Dataset
  • LAS Dataset Layer —LAS Dataset Layer
  • Layer —Layer
  • Layer File —Layer File
  • Layout —Layout
  • Line —Line
  • Linear Unit —Linear Unit
  • Long —Long
  • M Domain —M Domain
  • Map —Map
  • Map Server —Map Server
  • Map Server Layer —Map Server Layer
  • Mosaic Dataset —Mosaic Dataset
  • Mosaic Layer —Mosaic Layer
  • Neighborhood —Neighborhood
  • Network Analyst Class FieldMap —Network Analyst Class FieldMap
  • Network Analyst Hierarchy Settings —Network Analyst Hierarchy Settings
  • Network Analyst Layer —Network Analyst Layer
  • Network Data Source —Network Data Source
  • Network Dataset —Network Dataset
  • Network Dataset Layer —Network Dataset Layer
  • Network Travel Mode —Network Travel Mode
  • Parcel Fabric —Parcel Fabric
  • Parcel Fabric for ArcMap —Parcel Fabric for ArcMap
  • Parcel Fabric Layer for ArcMap —Parcel Fabric Layer for ArcMap
  • Parcel Layer —Parcel Layer
  • Point —Point
  • Polygon —Polygon
  • Projection File —Projection File
  • Pyramid —Pyramid
  • Radius —Radius
  • Random Number Generator —Random Number Generator
  • Raster Band —Raster Band
  • Raster Calculator Expression —Raster Calculator Expression
  • Raster Catalog —Raster Catalog
  • Raster Catalog Layer —Raster Catalog Layer
  • Raster Data Layer —Raster Data Layer
  • Raster Dataset —Raster Dataset
  • Raster Layer —Raster Layer
  • Raster Statistics —Raster Statistics
  • Raster Type —Raster Type
  • Record Set —Record Set
  • Relationship Class —Relationship Class
  • Remap —Remap
  • Report —Report
  • Route Measure Event Properties —Route Measure Event Properties
  • Scene Layer —Scene Layer
  • Semivariogram —Semivariogram
  • ServerConnection —ServerConnection
  • Shapefile —Shapefile
  • Spatial Reference —Spatial Reference
  • SQL Expression —SQL Expression
  • String —String
  • String Hidden —String Hidden
  • Table —Table
  • Table View —Table View
  • Terrain Layer —Terrain Layer
  • Text File —Text File
  • Tile Size —Tile Size
  • Time configuration —Time configuration
  • Time Unit —Time Unit
  • TIN —TIN
  • TIN Layer —TIN Layer
  • Tool —Tool
  • Toolbox —Toolbox
  • Topo Features —Topo Features
  • Topology —Topology
  • Topology Layer —Topology Layer
  • Trace Network —Trace Network
  • Trace Network Layer —Trace Network Layer
  • Transformation function —Transformation function
  • Utility Network —Utility Network
  • Utility Network Layer —Utility Network Layer
  • Variant —Variant
  • Vector Tile Layer —Vector Tile Layer
  • Vertical Factor —Vertical Factor
  • Voxel Layer —Voxel Layer
  • VPF Coverage —VPF Coverage
  • VPF Table —VPF Table
  • WCS Coverage —WCS Coverage
  • Weighted Overlay Table —Weighted Overlay Table
  • Weighted Sum —Weighted Sum
  • WMS Map —WMS Map
  • WMTS Layer —WMTS Layer
  • Workspace —Workspace
  • XY Domain —XY Domain
  • Z Domain —Z Domain
String

Derived Output

LabelExplanationData Type
Value

The calculated value.

Data Element; Layer; Table View

Environments

This tool does not use any geoprocessing environments.

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes