Skip to main content
Back to Articles
ArticleTips and Workflows
April 21, 2026/8 min read

Writing Raster Calculator and Field Calculator Formulas with AI

Learn how to use AI in iXGIS to draft Raster Calculator and Field Calculator formulas, including suitable use cases, prompt writing tips, review points, and practical examples.

RasterRaster DataVector Data

Many users understand the calculation logic clearly when they first work with expressions, but run into problems as soon as they need to write the actual formula.

  • Band numbers are easy to mistype.
  • Nested where(...) expressions make it easy to miss a parenthesis.
  • Once conditions become complex, it is hard to tell whether the logic has been reversed.
  • The desired result may be "population density", "risk level", or "elevation zone", but the business rule is clearer in your head than the formula syntax.

The Raster Calculator and Field Calculator in iXGIS now support AI-assisted formula writing. In practical terms, you can describe the requirement in plain language first, then let AI turn it into an expression that can run in the current window.

This article focuses on three questions:

  1. Which scenarios are suitable for AI-generated formulas?
  2. How can you write prompts that are more likely to produce correct results on the first try?
  3. How do you use this workflow in the Raster Calculator, Field Calculator, and attribute table field calculation?

When AI Formula Writing Is Useful

If the expression is as simple as a + b, writing it manually is usually faster.

AI saves the most time in cases like these:

  • You know the business rule, but do not want to translate it into formula syntax by hand.
  • You need to write multi-level where(...) classification logic.
  • You need to handle several conditions at once and are worried about parentheses or operator precedence.
  • You already have a formula but want to rewrite it into a clearer multi-line structure.
  • You need to use area, length, bands, or null checks, which are easy to mix up.

A simple rule is: once you start wondering whether you wrote something incorrectly, it is a good time to let AI draft a first version.

How to Use AI to Generate Formulas

1. State What You Want to Calculate

Do not only say "write a formula for me".

Better prompts include:

  • "Calculate NDVI."
  • "Classify elevation into four classes."
  • "Calculate population density from population and area."
  • "Classify roads by length."

2. Specify the Fields, Rasters, or Bands to Use

The more specific you are, the better.

For example:

  • image_a.tif.band4 and image_a.tif.band3
  • Population and AREA
  • slope, rain

If the names are vague or incorrect, the generated result is likely to be off as well.

3. Define Boundary Conditions Clearly

This is the part most likely to go wrong.

Instead of saying only "divide it into four classes", write something like:

Values less than or equal to 1610 are 1; values greater than 1610 and less than or equal to 2415 are 2; values greater than 2415 and less than or equal to 3468 are 3; all other values are 4.

This helps AI write complete interval boundaries and reduces overlaps or gaps.

4. Explain How Null Values Should Be Handled

This is important and often overlooked.

You can tell AI directly:

  • "If any input is null, output null."
  • "Treat null values as 0."
  • "Output nan when the denominator is 0."

If this is not stated clearly, the result may run successfully but have the wrong business meaning.

Using AI in the Raster Calculator

The Raster Calculator is suitable for cell-by-cell operations, such as indices, masks, reclassification, weighted overlay, and multi-band combinations.

Common use cases include:

  • Remote sensing indices such as NDVI and NDBI
  • Threshold extraction from elevation, slope, rainfall, and similar rasters
  • Reclassification by value range
  • Combining multiple rasters into a weighted evaluation result

You can load a template that already contains prepared data.

Example 1: Ask AI to Write NDVI

You can say:

Use band4 and band2 from air-survey-multiband-raster-100m.tif to calculate NDVI.

AI will likely return a formula close to this:

result = (air-survey-multiband-raster-100m.tif.band4 - air-survey-multiband-raster-100m.tif.band2) / (air-survey-multiband-raster-100m.tif.band4 + air-survey-multiband-raster-100m.tif.band2)

With null-value handling:

# prompt
Use band4 and band2 from air-survey-multiband-raster-100m.tif to calculate NDVI. If the denominator is 0 or any input is null, output nan.
# formula
nir = air-survey-multiband-raster-100m.tif.band4
red = air-survey-multiband-raster-100m.tif.band2
num = nir - red
den = nir + red
result = where(logical_or(den == 0, logical_or(isnan(nir), isnan(red))), nan, num / den)

The advantage of this approach is that AI does not only write the formula; it also helps account for exceptional cases.

Note: Boolean functions cannot rely on default null handling. Null handling must be written explicitly.

Example 2: Ask AI to Write Reclassification Logic

Classify dem.tif into four classes: values less than or equal to 100 are 1; values greater than 100 and less than or equal to 500 are 2; values greater than 500 and less than or equal to 1500 are 3; all other values are 4. Keep null values as null.

Possible result:

result = where(isnan(dem.tif), nan, where(dem.tif <= 100, 1, where(logical_and(dem.tif > 100, dem.tif <= 500), 2, where(logical_and(dem.tif > 500, dem.tif <= 1500), 3, 4))))

This is especially suitable for AI because nested where formulas are where people most often miss a parenthesis in the second or third layer.

Example 3: Ask AI to Write a Risk Mask

Generate a risk mask from slope.tif and rain.tif: cells with slope greater than 15 and rainfall greater than 500 should be 1; all other cells should be 0. If either input is null, output null.

Possible result:

result = where(logical_or(isnan(slope.tif), isnan(rain.tif)), nan, where(logical_and(slope.tif > 15, rain.tif > 500), 1, 0))

Using AI in the Field Calculator

The Field Calculator focuses on attribute calculation. It is suitable for generating or updating field values in batches for vector features.

Common scenarios include:

  • Calculating indicators such as population density, output density, and coverage rate
  • Generating level fields based on rules
  • Converting values with geometry keywords such as AREA and LENGTH_GEODESIC
  • Calculating an intermediate field first, then deriving a second field from it

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 geographic coordinate system, use the geodesic area keyword AREA_GEODESIC to calculate each polygon's geodesic area. The unit is square meters.

Example 2: Generate Two Fields at Once

Set density = Population / Area, with the unit as people per square kilometer. Then add a density_level field: values less than or equal to 1000 are 'Low', values from 1000 to 3000 are 'Medium', and values greater than 3000 are 'High'.

Possible result:

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

This kind of multi-step logic is well suited to AI because it can automatically split the calculation into more readable lines.

Example 3: Convert Length to Kilometers

Add a length_km field and calculate length in kilometers.

Possible result:

length_km = PERIMETER_LENGTH_GEODESIC / 1000

Calculate Field in the Attribute Table

In addition to the Field Calculator in the toolbox, the Calculate Field command in a vector layer's attribute table also works well with AI-assisted formula writing.

It is especially useful when:

  • You have already opened the attribute table and want to add a business field immediately.
  • You do not want to switch away and open the toolbox.
  • You only need to run one attribute calculation on the current layer.

For example, if you are in the attribute table and want to add a risk level field, you can describe it in natural language:

Add a risk_level field: when Population is greater than 700000 and Rainfall is greater than 700, set it to 'High'; when Population is greater than 600000 or Rainfall is greater than 600, set it to 'Medium'; otherwise set it to 'Low'.

AI may return:

risk_level = where(logical_and(Population > 700000, Rainfall > 700), 'High', where(logical_or(Population > 600000, Rainfall > 600), 'Medium', 'Low'))

When calculating fields in the attribute table, pay extra attention to two things:

  • Field names must exactly match the current attribute table.
  • If area or length is involved, confirm that the current layer's coordinate system and geometry units match the business requirement.

Human Review

AI can save a lot of repetitive typing, but it is not recommended to copy and run a formula without reading it. Keep a human in the loop.

At minimum, check the following:

  • Whether raster names, band numbers, and field names are correct
  • Whether condition boundaries include the intended equality signs
  • Whether intervals overlap or leave gaps
  • Whether null values are handled as expected
  • Whether area and length use the correct geometry keywords
  • Whether output field names and result values match your naming and business rules

For classification tasks in particular, review each class yourself even if the AI-generated formula looks smooth.

Tip

If you already have a formula but it is hard to read, you can ask AI to rewrite it:

Rewrite the following one-line formula into a multi-line version that is easier to check, and give the intermediate variables clearer names.

This is usually more reliable than asking AI to generate everything from scratch, because you have already controlled the core logic. AI is only helping you organize the structure.

Topic

Tips and Workflows

Practical tips for analysis, automation, and daily GIS work

Continue Browsing

Back to Articles

Browse all articles, switch topic filters, or continue from the home page.

Browse All Articles