Tuesday 21 May 2019

Creating Conditional Statement in QGIS Field Calculator


I've looked up some of the other Q&As on this topic, however, none have addressed what I am seeking to do.


I have a large data set and I would like to search one field for any values over a certain amount (in this case 20) and if the value is larger than 20, I would like to return a value of 1 in another field, otherwise return a value of 0.


I have tried writing something with no luck.


I am using QGIS 2.8.



Answer





The most simple way to do this is to create a new field with the expression


"cat" > 20

This expression will evaluate to a boolean True/False which will be represented as an integer 1 or 0.



You can also create a virtual field, which will automatically return an updated value in case the values in cat change (e.g. you edit the layer). Remember that the values of virtual fields will not be saved in the dataset and are only visible inside this QGIS project.



If you have more than a simple "greater than", you need to use


CASE 
WHEN "cat" > 100 THEN 2

WHEN "cat" > 10 THEN 1
ELSE 0
END

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...