QGIS Desktop provides powerful field-calculation functionality. In the field calculator, advanced mathematical, geometry, string, date and time, type conversion, and conditional functions are available for use. Leveraging these advanced functions along with standard operators allows for some powerful field calculations.
This section will explain the field calculator interface in detail, followed by multiple examples of advanced field calculations from a variety of functional areas. It is assumed that you know the basics of field calculations and common operators.
The field calculator can be opened in three ways, which are as follows:
calculator button ()
on the attribute table toolbarcalculator button ()
on the attributes toolbarThe Field calculator window, shown in the following screenshot, has five sections:
The expression must meet strict syntax guidelines, otherwise the field calculator will report a syntax error instead of an output preview. The following are common syntax rules for expressions:
+
."State_Name"
.'Washington'
.153.27
.log(base, value)
.$
) followed by the function name. For example, $area
.If this is a little confusing, don't worry, you can rely on the field calculator to enter a portion of the syntax for you correctly. To add an operator, field, or function to the expression, double-click on the desired item in the function list and it will be added to the cursor location in the expression.
In addition to adding functions through the function list, the field calculator can also add to the expression any value that currently exists in any field. To do this, expand the Fields and Values branch of the function list tree. A list of the fields in the attribute table will be listed. When you select a field, a Field values area will appear to the right underneath the function help (as shown in the following screenshot). Click on all unique to load the Field values area with all unique values found in the selected field. Then, click on 10 samples to load 10 samples that are found in the selected field into the Field values area. You can also load values by right-clicking on the field name and selecting it from the contextual menu (the contextual menu is shown in following screenshot). Double-click on a value to add it to the cursor location in the expression:
Let's put what we learned previously into practice. This section will walk you through creating three advanced field calculations. The first calculation will insert the current date into a field as a formatted string. The second calculation will insert a geometry value. The third calculation will calculate a label string that differs depending on the state's population.
The first example of an advanced field calculation uses two functions to calculate and format the current date. For this example, we will format the current date as dd/mm/yyyy:
Updated
Text (string)
10
now()
and enter 'dd/MM/yyyy'
, followed by a closed parenthesis. The now()
function returns a string representation of the current time and date. The following screenshot shows the completed calculation:The second example of an advanced field calculation uses two functions to insert the centroid x coordinate of a geometry object and the number of vertices that compose a geometry object. First, we will calculate the x coordinate of the centroid. To do this, perform the following steps:
XCoord
Decimal number (real)
10
7
$geometry
represents the geometry of the row being calculated. Lastly, close the parenthesis by typing ) on your keyboard. The completed expression should be x($geometry)
. This function returns the x coordinate of a point geometry or the centroid x coordinate of a non-point geometry (ex. line or polygon). The following screenshot shows the completed calculation:NumVerticies
Whole number (integer)
4
.$geometry
to add it to the expression. Close the parenthesis by typing ) on your keyboard. The completed expression should read num_points($geometry)
.This third example populates a new field with a string that is used for labeling states. States that have a population of over five million will have a label with the state name and population. All other states will simply have a label with the state name. The basic logic of our calculation is if a state has a population of over five million, then create a label that lists the state name and population; otherwise, create a label that lists the state name.
Since we have two cases of possible labels, we will need to use the CASE ELSE
conditional function. The purpose of the CASE ELSE
function is to direct the field calculator to a calculation block when a condition is met. So, we will have one calculation block for states over five million in population and one for all other states.
For this example, the states48.shp
sample data is being used. The POP1996
field contains the states' population values as of 1996 and is the field used to determine whether a state's population is over or under five million:
StateLabel
Text (string)
35
This will add CASE WHEN expression THEN result END
to the Expression area. We will replace expression
with the test for populations greater than five million. The result
after THEN
will be replaced with the label we wish to create when expression
is true. We will then add the ELSE
and will add the label we wish to create when expression
is false.
END
, type ELSE "STATE_NAME"
."POP1996" > 5000000
. The following screenshot shows the expression with optional formatting to make it easier to read:<state name> Population: <population>
, with the state name on the first line and the population on the second line. As this is a complex string, it will be constructed in three parts, and then concatenated together using the concatenation operator, ||
(two vertical bars).result
with the "STATE_NAME"
field."STATE_NAME"
by either typing two vertical bars ( ||
) or by clicking the concatenation operator button (). This allows the following text to be concatenated with the contents of the "STATE_NAME"
field.
'
Population: '
and keep a space between the colon and closing single quote. The
is interpreted as a new line and starts a new line in the string.The last item to add to the string is the population value stored in the POP1996
field. However, the population is stored as an integer and an integer (or any other number) cannot be concatenated to a string. Therefore, we need to convert the integer to a string so that we can concatenate. Luckily for us, the format_number()
function converts a number to a string and adds thousands separators and rounds the number (although rounding is not needed in this case).
format_number()
function by expanding String in Function list and double-click on format_number
. You can also manually type in the function.format_number()
function, enter "POP1996"
, 0
where "POP1996"
is the first parameter containing the population value, the comma separates the function parameters, and 0
is the number of decimal places to round the number. The following screenshot shows the completed expression, which is formatted across multiple lines for easy reading:3.134.90.44