Skip to main content

Calculate Fields

Overview

The Calculate Fields tool performs batch logical and mathematical operations on attribute fields in a feature dataset. You can write expressions yourself, or use AI to generate a formula from natural language first and then fine-tune the result. For routine tasks such as area conversion, density calculation, class assignment, and field cleanup, this tool can significantly reduce repeated trial and error.

Common examples include:

Use Cases

  • Indicator derivation: Calculate population density from population and area fields.
  • Geometry attribute extraction: Automatically extract the geodesic area of polygon features or the start coordinates of line features.
  • Data classification: Use the where function to generate an evaluation level field based on numeric values.
  • Field cleanup: Convert units, concatenate strings, or replace outliers in batches.

AI Formula Writing

If you know what field you want to create but are not sure how to write the formula, or if you are concerned about mistakes in parentheses, function names, or field names, you can ask AI to draft it first.

When to Use AI

  • You want to generate a new field from one or more existing fields.
  • You need to assign classes based on multiple conditions.
  • You want to calculate spatial indicators using geometry keywords such as AREA, LENGTH, or AREA_GEODESIC.
  • You already have an idea and want AI to convert it into a cleaner, easier-to-check multi-line formula.
  • You want to generate multiple fields at once, such as calculating density first and then assigning a level.

Steps

  1. Start the tool: Open the Geoprocessing Toolbox, go to Vector Tools > Field, and start the Calculate Fields tool pane.
  2. Select input features: Confirm the current target layer first. AI only generates formulas based on the existing fields and available geometry keywords in the current layer.
  3. Open AI formula writing: Enter your natural language request in the formula editing area.
  4. Describe the request clearly: Include the target field name, fields used in the calculation, classification rules, whether null values should be preserved, and whether multiple output fields are needed.
  5. Review the returned formula: Check that field names are correct, geometry keywords apply to the current layer type, and boundary conditions match the business meaning.
  6. Run the calculation: Before running on the full dataset, review results for a small number of records.

What AI Can Help With

  • Generate single-field formulas, such as population density, length conversion, or amount totals.
  • Generate multiple assignment statements at once, such as calculating an intermediate indicator first and then generating a level field.
  • Convert business rules into where(...) conditional expressions.
  • Use suitable geometry keywords based on the current layer type.

Manual Review Checklist

  • Whether field names exactly match the layer attribute table.
  • Whether new field names follow your naming conventions.
  • Whether point, line, and polygon layers use the correct geometry keywords.
  • Whether the units of area and length results are consistent with the current coordinate system.
  • Whether null values should be preserved, outliers should be excluded, or field cleanup should be performed first.
tip

For complex requirements, tell AI: "Split this into a multi-line formula and keep intermediate variable names." This makes later troubleshooting much easier.

Formula Writing Rules

1. Field and Geometry References

  • Field reference: Use the field name directly. If a field name contains special characters, rename it with the Field Editing tool first.
  • Geometry reference:
    • geometry: Used to calculate projected geometry indicators in the current coordinate system.
    • geometry_geodesic: Used to calculate geodesic indicators on the WGS 84 ellipsoid, such as AREA_GEODESIC.

2. Logic and Conditions

  • Logical operators: Use feature-wise logical operators & (and), | (or), and ~ (not), together with parentheses.
  • Conditional function: Use where(condition, x, y).
  • Multi-line assignment:
    tmp = Population / Area
    Density = where(tmp > 100, 'High', 'Low')

3. How to Write Better AI Prompts

  • Clearly state which fields to use, such as "Use Population and AREA to calculate population density."
  • State the target field name directly, such as "Name the new field density."
  • Provide complete classification rules, such as "0-100 is low, 100-300 is medium, and above 300 is high."
  • If null values need special handling, state it explicitly, such as "Output null when Population is null."

Geometry Function Reference

KeywordMeaningApplicable type
AREAProjected areaPolygon features
AREA_GEODESICGeodesic areaPolygon features on the WGS 84 ellipsoid
LENGTHProjected lengthLineString features
LENGTH_GEODESICGeodesic lengthLineString features on the WGS 84 ellipsoid
CENTROID_X / YCentroid coordinatesAll types
PART_COUNTNumber of partsMultipart (Multi*) features

Parameters

ParameterDescriptionNotes
Input featuresThe dataset whose fields will be calculated.Supports points, lines, and polygons.
Calculation formulaA multi-line expression that contains assignment logic.The last assignment determines the output field.
Save intermediate resultsWhether to keep intermediate variables in the output.Default: No.
Output feature datasetThe calculated result.Supports common vector formats.

Steps

  1. Start the tool: Open the Geoprocessing Toolbox, go to Vector Tools > Field, and start the Calculate Fields tool pane.
  2. Set the input: Select Input features.
  3. Configure parameters: Write the formula directly in the Calculation formula text box, or use AI formula writing to generate a draft. Select Save intermediate results if needed.
  4. Configure the output: Set the output path and file name.
  5. Run and review: Click Run and review the result.

AI Prompt Examples

Example 1: Generate a Population Density Field

Add a density field and calculate population density as population divided by area.

Possible result:

density = Population / AREA_GEODESIC

Note: For vector data in a longitude-latitude or geographic coordinate system, use the geodesic area function AREA_GEODESIC to calculate the geodesic area of each polygon. The unit is square meters.

Example 2: Generate Two Fields at Once

density = population / area, in people per square kilometer. Then add a density_level field: less than or equal to 1000 is 'Low', 1000 to 3000 is 'Medium', and greater than 3000 is 'High'.

Possible result:

density = Population * 1000000 / AREA_GEODESIC
density_level = where(density <= 1000, 'Low', where(density <= 3000, 'Medium', 'High'))

This type of multi-step logic is especially suitable for AI because it can split the logic into more readable multiple lines.

Example 3: Convert Length to Kilometers

Add a length_km field and calculate length in kilometers.

Possible result:

length_km = LENGTH_GEODESIC / 1000

Usage Tips

  • When describing a request to AI, use the exact field names from the attribute table to avoid ambiguity from synonyms.
  • If you want to keep intermediate variables for troubleshooting, select Save intermediate results.
  • When the layer uses a longitude-latitude coordinate system, first confirm whether projected or geodesic measurements should be used. Do not mix AREA and AREA_GEODESIC by default.
  • If the first AI-generated result is not ideal, the most effective correction is usually to point out the exact change, such as "change the field name to xxx", "do not convert null values to 0", or "make the class boundary include 100".

Notes

  • The units returned by geometry functions such as AREA depend on the coordinate system of the input data, such as meters or degrees.
  • Expression syntax is strict. Make sure all parentheses are paired correctly.
  • For large datasets, validate the formula on a small number of records first. For complex calculations, running the Fix Geometries tool first is safer.
  • AI-generated formulas are drafts. Do not use them directly on production data without review.