QGIS Tutorials

Editing Fields and Attributes with QGIS Field Calculator

Credit: Youtube Channel “Statistics Canada”

Editing Fields and Attributes with QGIS Field Calculator

QGIS provides a powerful, expression‑based Field Calculator that allows you to create new fields, update existing ones, and manipulate attribute data without leaving the user interface. This post covers everything you need to know to get started – from launching the tool to writing expressions, handling different data types, and avoiding common pitfalls.

Prerequisites

  • QGIS 3.22 or later (the concepts apply to all recent editions)
  • Vector layer loaded in the Layers panel
  • Layer is editable (toggle the edit button or right‑click → Toggle Editing)
  • Basic understanding of the attribute table and field types (text, number, date, etc.)

Launching the Field Calculator

  1. Open the attribute table of the layer (Right‑click → Open Attribute Table).
  2. Click the Field Calculator icon (Σ) in the toolbar.
  3. In the dialog that appears, you can:
    • Choose Create a new field or Update existing field
    • Specify the output field name, type, precision, and length (for new fields)
    • Enter your expression in the Expression box

Understanding Expressions

The Field Calculator uses the same expression engine as the QRScript, so you can employ a wide range of functions:

  • length($fieldname) – the length of a string
  • replace($fieldname, 'old', 'new') – replace substrings
  • to_int($fieldname) – cast to integer
  • format_date($datefield, 'yyyy-MM-dd') – format dates
  • CASE WHEN … THEN … ELSE … END – conditional logic

Example 1: Create a New Population Density Field

Field name: density
Field type: Decimal number (real)
Precision: 2
Expression: "$population" / ($area_km2)

Example 2: Update an Existing Field With String Manipulation

Choice: Update existing field → address
Expression: trim(upper($address))

Example 3: Conditional Value Assignment

Field name: zoning_category
Field type: Text
Expression: CASE
  WHEN $zoning IN ('Residential', 'Commercial') THEN 'Urban'
  WHEN $zoning IN ('Rural', 'Agricultural') THEN 'Rural'
  ELSE 'Other'
END

Working With Different Field Types

  • Numeric fields – Arithmetic operators (+, -, *, /) and functions like round() are available.
  • Text fields – Use string functions (concat(), length(), replace()) and operators (|| for concatenation).
  • Date/Time fields – Functions such as now(), format_date(), date_diff() help in calculations.
  • Boolean fields – These can be updated with expressions that evaluate to true/false.

Expression Builder Tips

  • Click the Functions button to browse functions by category.
  • Use the Variables panel to insert layer attributes automatically.
  • Drag and drop a field name or function into the expression box.
  • Press Test to preview the resulting values for the first few rows.

Common Pitfalls and How to Avoid Them

  • Data type mismatch – Ensure the output field type matches the data you produce. For example, assigning a string to a numeric field will cause errors.
  • Not toggling editing off – Remember to Save Edits and Toggle Editing off after finishing, or changes will be lost.
  • Using $geometry in attribute calculations – Unless explicitly needed, avoid unnecessary geometry calculations because they can be resource‑intensive.
  • Large datasets – When editing thousands of rows, autosave might disable; commit changes in Save edits once at the end.

Batch Updates with Virtual Fields

Virtual fields can be added without altering the underlying data. They recompute on demand and are ideal for temporary calculations.

Field name: year_since_1990
Field type: Integer
Expression: year(now()) - 1990

Saving Your Calculations as a Script

  1. Open the Expression dialog.
  2. Write or paste your expression.
  3. Click Save (floppy disk icon) to store it as a script for future reuse.

Wrapping Up

Whether you’re cleaning up data, calculating new metrics, or automating attribute transformations, QGIS’s Field Calculator gives you a robust and flexible platform. Experiment with the expression engine, explore the available functions, and soon you’ll be creating custom fields and updating attributes with just a few clicks.

Happy mapping!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *