Outdated version of the documentation. Find the latest one here.

Docs for ‘QGIS testing’. Visit http://docs.qgis.org/2.8 for QGIS 2.8 docs and translations.

Field Calculator

The calculateField Field Calculator button in the attribute table allows you to perform calculations on the basis of existing attribute values or defined functions, for instance, to calculate length or area of geometry features. The results can be written to a new attribute field, a virtual field, or they can be used to update values in an existing field.

A virtual field is a field based on an expression calculated on the fly, meaning that its value is automatically updated as soon as the underlying parameter changes. The expression is set once; you no more need to calculate again the field if underlying values change. For example, you may want to use a virtual field if you need area values to be calculated during a digitizing process (creating, merging, spliting features) or to calculate a duration that needs to be updated from time to time.

Tip

Virtual Fields

  • Virtual fields are not permanent and thus not saved in the layer attributes. They are however saved in the project they were created.
  • To make a field virtual it must be set at the creation of the field and the expression used can’t be changed later.

The field calculator is now available on any layer that supports edit. When you click on the field calculator icon the dialog opens (see figure_attributes_3). If the layer is not in edit mode, a warning is displayed and using the field calculator will cause the layer to be put in edit mode before the calculation is made.

The quick field calculation bar on top of the attribute table is only visible if the layer is editable.

In quick field calculation bar, you first select the existing field name then open the expression dialog to create your expression or write it directly in the field then click on Update All button.

Expression tab

In the field calculator dialog, you first must select whether you want to only update selected features, create a new attribute field where the results of the calculation will be added or update an existing field.

Figure Attributes 3:

../../../_images/fieldcalculator.png

Field Calculator

If you choose to add a new field, you need to enter a field name, a field type (integer, real or string), the total field width, and the field precision (see figure_attributes_3). For example, if you choose a field width of 10 and a field precision of 3, it means you have 6 digits before the dot, then the dot and another 3 digits for the precision.

A short example illustrates how field calculator works when using the Expression tab. We want to calculate the length in km of the railroads layer from the QGIS sample dataset:

  1. Load the shapefile railroads.shp in QGIS and press openTable Open Attribute Table.
  2. Click on toggleEditing Toggle editing mode and open the calculateField Field Calculator dialog.
  3. Select the checkbox Create a new field checkbox to save the calculations into a new field.
  4. Add length as Output field name and real as Output field type, and define Output field width to be 10 and Precision, 3.
  5. Now double click on function $length in the Geometry group to add it into the Field calculator expression box.
  6. Complete the expression by typing ‘’/ 1000’’ in the Field calculator expression box and click [Ok].
  7. You can now find a new field length in the attribute table.

The available functions are listed in Expressions chapter.

Function Editor tab

With the Function Editor you are able to define your own Python custom functions in a comfortable way. See also Expressions for more informations.