< All
...
Export

10 Field calculator

Introduction

The field calculator in QGIS is a versatile tool that allows you to perform complex calculations and data transformations directly in the attribute table. With the field calculator, you can create new fields, update existing fields and calculate values based on expressions that include mathematical operations, text manipulation, date/time functions and more.

1. open the field calculator

The field calculator can be used both for creating new fields and for editing existing fields in a level.

A. Opening the field calculator

  1. Open attribute table:
  • Right-click on the layer and select Open attribute table.
  1. Call up the field calculator:
  • Click on the Field calculator button in the attribute table toolbar. Alternatively, you can call up the field calculator via the Edit > Field calculator menu.

2. create new fields

The field calculator offers the option of creating new fields based on the calculations or expressions.

A. Add new field

  1. Activate Create new field:
  • In the field calculator dialog box, activate the Create new field checkbox.
  1. Field name and type:
  • Enter a name for the new field and select the data type (e.g. integer, decimal number, text, date).
  1. Field length and accuracy:
  • For numeric fields, you can specify the field length and decimal places. This determines how many digits in total and how many decimal places the field can contain.

B. Define expression

  1. Expression window:
  • The expression window is located in the lower part of the field calculator window. Here you can define the expression that calculates the values for the new field.
  1. Fields and values:
  • On the left-hand side of the window you will find a list of available fields and functions. You can select a field and insert it into the expression, e.g. [field name] * 2.
  1. Mathematical operations and functions:
  • Use mathematical operators (+, -, *, /) and functions such as sum(), mean(), sqrt() to perform complex calculations.
  1. Preview:
  • Before you apply the printout, you can preview the results to make sure the printout is correct.
  1. Perform calculation:
  • Click OK to create the new field and perform the calculation. The new field is added to the attribute table and the calculated values are inserted.

3. update existing fields

The field calculator can also be used to change the values in an existing field.

A. Update field

  1. Activate the Update field:
  • In the field calculator dialog box, select the Update an existing field option.
  1. Select target field:
  • Select the field you want to update from the drop-down list.
  1. Enter printout:
  • Define the expression that calculates the new values for the field. This can be a simple operation such as [field name] + 100 or a more complex function.
  1. Perform calculation:
  • Click OK to update the field with the new values.

4. extended functions of the field computer

The field calculator offers a variety of functions that help you to analyze and transform data:

A. Mathematical functions

  • Basic arithmetic operations: Perform simple mathematical operations such as addition, subtraction, multiplication and division.
  • Trigonometric functions: Calculate sine, cosine, tangent and their inverse functions.
  • Logarithms and exponential functions: Use log(), exp() and similar functions for mathematical calculations.

B. Text functions

  • String operations: Join texts with the concat() function, cut off parts of character strings or convert them to upper or lower case letters.
  • Length calculation: Use length() to determine the number of characters in a text field.

C. Date/time functions

  • Calculate date: Use functions such as now() or today() to get the current date and time.
  • Date differences: Calculate the difference between two dates using the age() function.

D. Geometry functions

  • Lengths and areas: Calculate lengths, areas and other geometric properties of your features.
  • Geometric transformations: Use functions to change or analyze geometric data.

5. best practices

  • Test before use: Test your expressions on a small amount of data or use the preview function to ensure that the results are correct.
  • Undo: Remember that changes to existing fields cannot be undone without further ado. Save your project or create a backup before making extensive changes.
  • Use documentation: Rely on QGIS documentation and online resources to learn more about complex functions and expressions.

The field calculator in QGIS is an indispensable tool for manipulating and analyzing attribute data. Thanks to its flexibility and the wide range of functions available, it enables you to process and enrich data efficiently.