It is possible to perform operations in the attribute tables of QGIS 3, e.g. queries, calculation of geometric properties, concatenation and extraction of texts, mathematical and date operations.
The field calculator consists of three panels, the middle shows the expressions that are the functions that can be used (in this article was introduced the use of expressions to create symbology), in the right panel writes the operation and the left panel shows a brief explanation with examples of what the selected expression does. In the upper part the output options are configured, either in a new field, virtual field or updating an existing field.
Operations on Text Fields
Text fields are called Strings, referring to strings of characters, the Fields Calculator has expressions to do operations with text, concatenate, extract characters or phrases, convert to upper or lower case, convert numbers to texts or vice versa, among others.
2.- Concatenate fields: we can attribute text, if one of the fields is numeric it is automatically converted into text. In the following example we have the data of a supply network, a field with the diameter in inches (decimal) and another one with the material (text), a new field will be created that one denominated “Type”, for it the function “concat” is used:
concat(“Material”, ‘ of ‘ , “Diameter”, ‘ ” ‘)
Calculations in numeric fields:
1.- Population density: To calculate the population density for 1990 the following expression is used “Pop1990” / “Area”, a new field called “Density90” is configured, decimal type with 12 characters and 2 decimals:
2. Population increase: In order to know the population increase between 1990 and 1997, we simply subtract the fields “Pop1997” – “Pop1990”.
Determining geometric properties
In a GIS, generally three types of vector entities are handled, with geometric properties defined by their dimensions:
- Points: X,Y coordinate
- Lines: length
- Polygon: area, perimeter
1.- Calculate the coordinates of points: Open the field calculator, in the dialog box that displays leaves the default option, “Create a New Field”, in the tab “Name” enter “This”.
In the central part of the dialog box look for the functions, select Geometry and then double click on $X, the function is written in the left panel and at the bottom of the table we can see the preview of the result. Click on the Ok button and create the field. Repeat the procedure using the $Y function for the North coordinate.
Why is this function used?
The $ sign means that the function will be executed on the entities of the layer (on which we are working), so $X returns the X coordinate of each entity of the layer.
2.- Calculate the length of a line: In this case in the Field Calculator select the function $lenght and put an appropriate name to the field, for example: Length.
3.- Calculate the area and perimeter of a polygon: Following the same procedure described above, with the $Area and $Perimeter functions you can calculate the area and perimeter respectively.
Note: it is also possible to consult the geometric properties of an entity without having to perform an operation in the attribute table, simply by using the identify button a dialog box is displayed showing the attributes, then click on the “derived” tab.