Field Calculator
This tool allows to set values for multiple records in no time. It is very useful both for basic and advanced usage. There are variety of results that can be achieved by using the Field Calculator. This documentation covers a number of examples how to make it work. This tool uses JavaScript as the scripting language. The code written in it is being parsed by the Google V8 JavaScript parser, so if something is possible in JavaScript it is also possible in Field Calculator – as long as it returns alpha-numeric value.
Please be careful
Field Calculator replaces existing values of the selected column with the result of the entered js code. By default, it works on all table records, but you can use the 'Limit to selection' option and will only change the records which are currently on the Selection pane.
Field Calculator Window contains 6 sections :
- This drop-down list contains all columns of the layer that are available to edit.
- This is the main part of the Field Calculator where you can set values and write more complex code
- Checkbox which limits the actions of the FC only to the selected records in the layer
- This part contains basic mathematical, comparison and logical operators.
- In this section there are columns that can be used in your code. In order to add one of them, please click on the Add button on the bottom left of the window. $value can be used to query Primary Key column and $geometry will query geometry type column
- In the last section you can list top 500 unique values for a column from the left side. In order to do this, please select the column from the left hand pane and click “Get Unique Values” button on the bottom of the window.
Below you can find a few example tasks that Field Calculator can work out:
How to propagate a value of all/selected records with a same value/text in selected column
Result:
In order to do this please select a column (1) and then enter the value or text. Please note that text must be wrapped in quotes. When you going to enter a value it’s not necessary to wrap it in quotes.- How to propagate a value of all/selected records based on other column
The easiest way is to copy the exact data from a column to another one. In order to do this, please select the column which will be edited (1) and then add the field (2) – the name of the source column which contains the data.
Result:
- How to propagate a column with values based on geometry column
There are few parameters that can be used on the geometry column, they are pretty self-explanatory:- .area
- .length
- .perimeter
- .centroidX
- .centroidY
- .empty
- .valid
Using conditional queries
Please take a look at following Field Calculator configuration. It contains an exemplary usage of simple conditional query.Please note
Please make sure to return the value at the end of the script and place the code inside a function when using such queries. The function should be called at the end of script (like in the example below) or you should use self invoking function.
Extracting year from reference value column
Example column consists of values such as: 88/90003/FSHCME, 01/90001/FSHTAL etc. The first two values represent the year. Below code extracts year name from the text(column named 'refval') and creates 4 digits year value then.
(function(){ var fields = refval.split('/'); var yearNumber = parseInt(fields[0]); // Source table consists of data, which ranges from 1998 to 2017. if (yearNumber < 88) { yearNumber = 2000 + yearNumber; } else { yearNumber = 1900 + yearNumber; } return yearNumber; })();
- Creating one line address from multiple columns
Here we use Field Calculator function to return a concatenated single line address from multiple fields within the dataset. In this case, the fields being concatenated are those from the LLPG DTF 7.3 dataset:Please note
Please make sure to return the value at the end of the script and place the code inside a function when using such queries. The function should be called at the end of script or you should use self invoking function (like in the example below).
(function(){ function toTitleCase(str) { return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); } var address = "", sao = "", pao = ""; if (sao_start_number != null) sao = sao.concat(sao_start_number); if ((sao_start_suffix != null) && (sao_start_suffix.length > 0)) sao = sao.concat(sao_start_suffix); if (sao_end_number != null) sao = sao.concat("-", sao_end_number); if ((sao_end_suffix != null) && (sao_end_suffix.length > 0)) sao = sao.concat(sao_end_suffix); if (pao_start_number != null) pao = pao.concat(pao_start_number); if ((pao_start_suffix != null) && (pao_start_suffix.length > 0)) pao = pao.concat(pao_start_suffix); if (pao_end_number != null) pao = pao.concat("-", pao_end_number); if ((pao_end_suffix != null) && (pao_end_suffix.length > 0)) pao = pao.concat(pao_end_suffix); if ((organisation != null) && (organisation.length > 0)) address = address.concat(toTitleCase(organisation), " "); if ((sao_text != null) && (sao_text.length > 0)) address = address.concat(toTitleCase(sao_text), " "); if ((sao.length != null) && (sao.length > 0)) address = address.concat(sao, " "); if ((pao_text != null) && (pao_text.length > 0)) address = address.concat(toTitleCase(pao_text), " "); if (pao.length > 0) address = address.concat(pao, " "); if ((description != null) && (description.length > 0)) address = address.concat(toTitleCase(description)); if ((locality != null) && (locality.length > 0)) address = address.concat(" ", toTitleCase(locality)); if ((town_name != null) && (town_name.length > 0)) address = address.concat(" ", toTitleCase(town_name)); if (((post_town != null) && (post_town.length > 0)) && (post_town != town_name)) address = address.concat(" ", toTitleCase(post_town)); if ((postcode != null) && (postcode.length > 0)) address = address.concat(" ", postcode); return address; })();
Example result of script presented above:
Example advanced mathematical query
In this scenario I’m working on the layer which consists only from discs. I want to create a column which contains information about the radius of each of them. The geometry parameters that are available in Earthlight doesn’t allow to do this “on the fly”.In order to calculate the radius, I’ve used geometry.area field, area of disc formula and JavaScript Math object. Please see following configuration and the result :
Things to remember
- Enter string values in quotes
- If you are using variables remember to return one of them at the end of the script by using return statement and place the code inside a function.
- Be careful when operating on different column types. There are some scenarios when Field Calculator operation can fail because of invalid script configuration – e.g. you cannot store letters in numeric column.
- Be aware that if you won’t choose Limit to selection button, you are working on all the records in the table. If you going to select column which already contains data – it will be replaced by the new values defined in the Field Calculator workspace.