Field Calculator

Field Calculator

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:

  • a URL to a feature service layer with an optional filter to select specific features, or
  • 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:

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

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:

  • "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 marks. For example, to calculate a population density value based on the value of the field POP divided by the field AREA:

  • "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 single-quoted. For example, to assign the string VACANT to a field: "expression":" 'VACANT' ". Strings can be concatenated using +.

Example: Create a new 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 upper-case values in the field LotStatus:

"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 field POP:

"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: "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 outputName is not supplied, the task will return a feature collection.

Syntax:
{
  "serviceProperties": {
    "name": "<service name>"
  }
}

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 topic Checking job status. Once the job has successfully completed, you 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>

Accessing results

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": 
"http://<analysis url>/FieldCalculator/jobs/<jobId>/results/resultLayer"}

The result has properties for parameter name, data type, and value. The contents of value depends upon 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.