The “field calculator” is one of the most used tools by users when editing the attributes of a layer. This is because its versatility, and it saves time when editing several registers at the same time.
It allows to make different type of calculations on the table fields. This tool can be executed on every register of a table or only on the selected ones.
We are going to see how it works with simple exercises, using our Game of Thrones data. But firstly we are going to see its interface.
- Information. It shows the information about the selected “Field” or “Commands”.
- Field. List of fields of the Table. Double-clicking on a field it’s added to the expression to apply.
- Type. The list of “Commands” available is updated depending on the selected type.
- Commands. List of commands available depending on the selected “Type”. Double-clicking on a command it’s added to the expression.
- Expression. Operation to be applied on the selected field. The expression can be written directly.
After viewing the theoretical part we are going to start our practical exercise.
Firstly we are going to open the attribute table of the “Locations” layer, that will have 7 columns after the last exercises. One of the existing fields is “type”, that contains the types of location (city, castle, ruin, town, other).
Now, for example, we want to add a new column where we will add the type of location in Spanish language. We would be able to do it manually, such as we saw at the “Editing table” post, but thanks to the “Field calculator” we can do this exercise in a faster way.
Following the steps that we learned in the “Editing table” post, we put the table in editing mode, and we add a column, with “String” type, and keeping the characters by default (50). We will call it “Tipo”. We would be able to keep the “Value by default” empty, but to save time when filling it out we are going to write “Otro” (without quotation marks). At this way all the registers will be filled out with this value. Now we only have to fill out the rest of the values.
At this moment the table will be like this one:
Now we are going to use the “Select by attribute” tool to select the different values of the “Type” field, and the field calculator to fill in the rows selected with the corresponding value in an automatic way.
At this point, if you don’t know how to use the “Select by attribute” tool you can review the post where we explained its working.
We are going to start selecting the rows the “type” of which is “Castle”:
Once they are selected, we press on the header of the “Tipo” field (it’s shown in dark grey colour).
Then we run the “Field calculator” tool, available from the “Table/Field calculator” menu or from its corresponding button.
A new window will be opened, where we can write the “Castillo” expression that we want to fill out the registers with. It’s important to point out that the text will have to be between double marks.
When we press “Accept” the cells of the “Tipo” field that are selected will be filled out:
We repeat the same operation with the rest of the values of the “type” field. Firstly we will select the rows and then wit the field calculator we will fill out the data:
- Type “City” = Tipo “Ciudad”
- Type “Ruin” = Tipo “Ruina”
- Type “ Town” = Tipo “Pueblo”
Once we finish our task, we finish editing and we save the changes. Our table will be like this one:
The “Field calculator” is very powerful and it allows to use complex expressions. We recommend you to test it and learn all its functionalities.
See you in the next post…