Field Calculator

Field Calculator

Note:

The Field Calculator task is no longer supported in ArcGIS Online as of March 2021 or in versions of ArcGIS Enterprise 10.9 or higher.

Learn more about how to calculate field values

The Field Calculator task updates values in one or more fields based on an expression you provide. The fields to update can be existing fields or a new fields that you create as part of the task request. The expression can use values from other fields. The result of this task is a new layer with all the fields from the input layer as well as any new fields you create. Field values are updated according to the expressions you provide.

Request URL

http://<analysis url>/FieldCalculator/submitJob

Request parameters

ParameterDescription

inputLayer

(Required)

The layer containing the field to be updated.

The layer can be one of the following:

  • A URL to a feature service layer with an optional filter to select specific features
  • A feature collection

Examples:

  • {"url": <feature service layer url>, "filter": <where clause>}
  • {"layerDefinition": {}, "featureSet": {}, "filter": <where clause>}

If a <where clause> is provided, only those records that match the WHERE clause will be written to the resultLayer.

expressions

(Required)

A list of fields and expressions. The general form is as follows:

[ "field": { <field_description> }, "expression": <expression>}, ...]

Field

To update an existing field, supply the name of the field in <field_description>. For example, to update values in a field named POP2014, use the following:

  • "field": {"name": "POP2014"}

To create a new field, supply the following in the <field_description>:

  • "name"—The name of the new field. When the task executes, the named will be checked for illegal characters. If any illegal characters exist, they will be replaced with underscores in the resultLayer.
  • "alias"—The alias name of the field, usually text that is more descriptive than the field name. The alias can contain spaces and other characters that cannot be used in field names.
  • "type"—One of four types: "String", "Integer", "Double", or "Date".
  • "length"—Required for type "String"; optional otherwise.

Example: add a new field named POPDEN:

  
"field":{
  "name":"POPDEN",
  "alias":"Population Density per Square Mile",
  "type":"Double"}

Expression

To use the value of an existing field in an expression, surround the field name with exclamation points. For example, to calculate a population density value based on the value of the field POP divided by the field AREA, use the following:

  • "expression": "!POP! / !AREA!"

Python is used to interpret expressions. The Field Calculator task uses the ArcGIS geoprocessing tool Calculate Field to perform all calculations. For more examples of expressions, see the Calculate Field tool reference documentation. As you read this tool reference page, keep the following in mind:

  • The syntax for the Calculate Field tool states that expressions can be based on VB (Visual Basic) or Python. Python syntax is used by Field Calculator.
  • You cannot use code blocks, as they present a security risk.
  • The Calculate Field tool can only calculate one field at a time. You can calculate multiple fields with Field Calculator by supplying a list of fields and expressions.

Working with strings

When assigning strings to text fields, values must be in single quotation marks. For example, to assign the string VACANT to a field, use the following: "expression":" 'VACANT' ". Strings can be concatenated using the plus sign (+).

Example: Create a field named FULL_NAME and calculate its value to be the concatenation of STREET_NAME and STREET_TYPE.

 
{"field": {
  "name":"FULL_NAME", 
  "type":"String", 
  "alias":"Street name", 
  "length":"80"},
"expression":"!STREET_NAME! + !STREET_TYPE!"}

You can use Python string functions on field values. For example, to uppercase values in the field LotStatus, use the following:

"expression": "!LotStatus!.upper()"

Working with numbers

Functions in Python's math module can be used. For example, to calculate the square root of the values in the POP field, use the following:

"expression": "math.sqrt(!POP!)"

The math module provides number-theoretic and representation functions, power and logarithmic functions, trigonometric functions, angular conversion functions, hyperbolic functions, and mathematical constants. To learn more about the math module, see the Python help.

Working with dates

Functions in Python's datetime module can be used.

Example: Use the datetime model to calculate the current date and time into an existing field EDITDATE.

{"field": {"name":"EDITDATE"},
 "expression":"datetime.datetime.now()"}

The syntax for date and time constants is YYYYMMDD hh:mm:ss.

Working with geometries

As shown in the Calculate Field documentation, you can access properties of feature geometry, including type, extent, centroid, firstPoint, lastPoint, area, length, isMultipart, and partCount. The syntax to access geometry properties is !shape.<property>!. For example, to access the length property, use the following: "expression": "!shape.area!"

Expressions can use the geometry area and length properties with an areal or linear unit to convert the value to a different unit of measure (for example, !shape.length@kilometers!). If inputLayer is stored in a geographic coordinate system and a linear unit is supplied (for example, miles), the length will be calculated using a geodesic algorithm. Using areal units on geographic data will yield questionable results as decimal degrees are not consistent across the globe.

  • Areal unit of measure keywords:
    • ACRES | ARES | HECTARES | SQUARECENTIMETERS | SQUAREDECIMETERS | SQUAREINCHES | SQUAREFEET | SQUAREKILOMETERS | SQUAREMETERS | SQUAREMILES | SQUAREMILLIMETERS | SQUAREYARDS | SQUAREMAPUNITS | UNKNOWN
  • Linear unit of measure keywords:
    • CENTIMETERS | DECIMALDEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICALMILES | POINTS | UNKNOWN | YARDS

Expressions can be used to calculate the geodesic area or length of a feature by using geodesicArea or geodesicLength properties combined with areal or linear units of measure. For example:

  • !shape.geodesicArea@hectares!
  • !shape.geodesicLength@miles!

outputName

If provided, the task will create a feature service of the results. You define the name of the service. If an outputName value is not provided, the task will return a feature collection.

Syntax:

{
  "serviceProperties": {
    "name": "<service name>"
  }
}
In ArcGIS Online or ArcGIS Enterprise 10.9.1 and later, you can overwrite an existing feature service by providing the itemId value of the existing feature service and setting the overwrite property to true. Including the serviceProperties parameter is optional. As described in the Feature output topic, you must either be the owner of the feature service or have administrative privileges to perform the overwrite.

Syntax:

{

  "itemProperties": {
			"itemId": "<itemID of the existing feature service>",
			"overwrite": true
	}
}
or
{
"serviceProperties": {
    "name": "<existing service name>"
  },
"itemProperties": {
				"itemId": "<itemID of the existing feature service>",
				"overwrite": true
	}
}

context

Context contains additional settings that affect task execution. For Field Calculator, there are two settings.

  1. Extent (extent)—A bounding box that defines the analysis area. Only those features in the inputLayer that intersect the bounding box will have the field values updated.
  2. Output Spatial Reference (outSR)—The output features will be projected into the output spatial reference.

Syntax

{
"extent" : {extent}
"outSR" : {spatial reference}
}

f

The response format. The default response format is html.

Values: html | json

Response

When you submit a request, the service assigns a unique job ID for the transaction.

Syntax:

{
  "jobId": "<unique job identifier>",
  "jobStatus": "<job status>"
}

After the initial request is submitted, you can use the jobId to periodically check the status of the job and messages as described in the Checking job status topic. Once the job has successfully completed, use the jobId to retrieve the results. To track the status, you can make a request of the following form:

http://<analysis url>/FieldCalculator/jobs/<jobId>

Results access

When the status of the job request is esriJobSucceded, you can access the results of the analysis by making a request of the following form:

http://<analysis url>/FieldCalculator/jobs/<jobId>/results/<output parameter name>?token=<your token>&f=json

ParameterDescription

resultLayer

Request example:

{"url": "https://<analysis url>/FieldCalculator/jobs/<jobId>/results/resultLayer"}

The result has properties for parameter name, data type, and value. The contents of the value depend on the outputName parameter provided in the initial request.

  • If outputName was provided, value contains the URL to the feature service layer.
    
    {
      "paramName":"resultLayer",
      "dataType":"GPString",
      "value": {"url":"<hosted featureservice layer url>"}
    }
  • If outputName was not provided, value contains a feature collection.
    
    {
      "paramName":"resultLayer",
      "dataType":"GPString",
      "value":{"layerDefinition": {}, "featureSet": {}}
    }

See Feature Output for more information about how the result layer or collection is accessed.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.