TransWikia.com

Calculating new attribute based on condition in QGIS 3 Field Calculator

Geographic Information Systems Asked on January 7, 2021

I have a shape with different city areas that contains the average building heights ("HHO-mean") in meters. Now I want to categorize the average bulding heights in a new column in five different categories (1-5).

I tried the following:

CASE

    WHEN "HHO_mean" > 5.7 THEN 1
    WHEN "HHO_mean" > 6.4 THEN 2
    WHEN "HHO_mean" > 7.1 THEN 3
    WHEN "HHO_mean" > 8.0 THEN 4
    WHEN "HHO_mean" > 10 THEN 5

END 

and

if( "HHO_mean" > 5.7, '1', if( "HHO_mean" < 6.4, '2',  if( "HHO_mean" < 7.1, '3', if( "HHO_mean" < 8.0, '4', if( "HHO_mean" < 10, '5', '0')))))

In both cases the new column only contains 1 in every field. Any suggestions?

2 Answers

In your query you forgot to include the upper edge for your range of values. It is possible with using an AND-Operator.

So, try:

CASE
   WHEN "HHO_mean" > 5.7 AND "HHO_mean" <= 6.4 THEN 1
   WHEN "HHO_mean" > 6.4 AND "HHO_mean" <= 7.1 THEN 2
   WHEN "HHO_mean" > 7.1 AND "HHO_mean" <= 8.0 THEN 3
   WHEN "HHO_mean" > 8.0 AND "HHO_mean" <= 10 THEN 4
   WHEN "HHO_mean" > 10 THEN 5
   ELSE 0
END

The same issue appears in if(), therefore it has to be if("HHO_mean" > 5.7 and "HHO_mean" < 6.4, '1', ....

Nore: As far as I know the BETWEEN-Operator unfortunately is not implemented in QGIS's Field Calculator.


References:

Correct answer by Taras on January 7, 2021

the order of the condition check is value_if_true then value_if_false

so in your second test, it should be (note the >= instead of > on the first condition):

If( "HHO_mean" <= 5.7, '0', if( "HHO_mean" <= 6.4, '1', if( "HHO_mean" <= 7.1, '2', if( "HHO_mean" <= 8.0, '3', if( "HHO_mean" <= 10, '4', '5')))))

Answered by radouxju on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP