Attribute analysis and statistics

Working with expressions and the field calculator

Write Orbis expressions to calculate new fields, combine conditions and transform text, using the Field calculator and the Expression console.

Intermediate2 min read

You will learn

  • Create a calculated field with the Field calculator.
  • Use operators, functions, CASE and NULL handling in expressions.
  • Test expressions in the Expression console.

Prerequisites

Data for this tutorial

Sample villages (synthetic) Download villages.zip

Format
ESRI Shapefile (ZIP: .shp, .shx, .dbf, .prj, .cpg)
Geometry
Point
CRS
EPSG:4326 (WGS 84 longitude, latitude)
Content
25 villages in 5 districts of 2 states
Fields used in the tutorials
FieldTypeDescription
village_idtextIdentifier V001 to V025.
villagetextVillage name. Common names repeat (Rampur, Shivpur, Madhopur), as they do in real gazetteers.
districttextDistrict: Rampur, Varanasi, Jaunpur, Patna or Gaya.
statetextState: Uttar Pradesh or Bihar.
populationintegerResidents. One village (Amarpur) has no recorded population (NULL).
householdsintegerHouseholds.
has_schooltextyes or no.

Wards (synthetic) Download wards.geojson

Format
GeoJSON
Geometry
Polygon
CRS
EPSG:32643 (WGS 84 / UTM zone 43N)
Content
84 wards (31 peri-urban, 19 rural, 34 urban), 333,497 residents
In Orbis
Browser panel ▸ Tutorial sample data (synthetic) ▸ Wards, or Load complete tutorial pack.
Fields used in the tutorials
FieldTypeDescription
ward_idtextWard code W01 to W84.
ward_nametextWard name.
zonetexturban, peri-urban or rural.
populationintegerResidents.
householdsintegerHouseholds.
area_km2realEllipsoidal area in km².
density_km2realPersons per km².
literacy_raterealLiterate share, per cent.

Licence: CC0 1.0. All tutorial data is synthetic and generated by Orbis. About the datasets

The task

You want to add a field with the average number of persons per household of each village, and a size class (small, medium, large) for each village.

Steps

  1. Load villages.zip. Open the attribute table and click Field calculator in its toolbar (or find Field calculator in the Toolbox).
  1. Set Result field name to persons_per_hh, Result type to Decimal number (real) and type the expression:

    "population" / "households"

    The line under the box shows the value for the first village. Click Run. A new layer with the field is added; the input layer is not changed.

    The Field calculator with the expression population divided by households and the preview of the first feature
    The Field calculator.
  1. Run the Field calculator again on the new layer with Result field name size, Result type Text (string) and:

    CASE
     WHEN "population" IS NULL THEN 'unknown'
     WHEN "population" < 2000 THEN 'small'
     WHEN "population" < 5000 THEN 'medium'
     ELSE 'large'
    END
  1. Try expressions without creating fields in the Expression console (bottom panel). It evaluates an expression for the active layer.

Expression essentials

You writeMeaning
"field"the value of a field (double quotes)
'text'a text value (single quotes)
+ - * / % ^arithmetic; division by zero gives NULL
= != < <= > >=, AND OR NOTcomparisons and logic
IN (…), BETWEEN a AND b, LIKE, ILIKElists, ranges and text patterns (% any text, _ one character)
IS NULL, IS NOT NULL, coalesce(a, b)missing values
upper(), lower(), trim(), replace(), concat()text functions
round(x, 2), abs(), sqrt(), to_real(), to_int()numbers and conversion
$area, $length, $perimetergeodesic area (m²) and lengths (m) of the feature

Expected result

The new layer has persons_per_hh (for example 1450 / 262 ≈ 5.53 for the first Shivpur) and size with small, medium, large or unknown.

Common mistakes

  • Integer result type for a ratio. Whole number (integer) rounds 5.53 to 6. Use Decimal number (real).
  • NULL in arithmetic. Any calculation with NULL gives NULL; Amarpur gets NULL persons per household. Use coalesce("population", 0) only if 0 is really correct.

Troubleshooting

  • "Unknown field". Check the spelling and the double quotes; click a field name below the box to insert it.
  • Text compared with numbers. "code" = 5 never matches a text field that holds "5"; write '5', or convert with to_int("code").