7

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 building 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?

1
  • 4
    If all values are above 5.7, they all fulfill the first condition. The calculator is did his job and goes home. You could either revert the conditions (check for the largest one first) - though I don't know if this works - or include a check for a ceiling, e.g. if ("column" > 5.7 AND "column" <= 6.4).
    – Erik
    Sep 4, 2020 at 12:42

3 Answers 3

8

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', ....

Note: The BETWEEN-operator unfortunately is not implemented in QGIS's Field Calculator.


References:

0
6

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')))))
3

The case will end when first when is true, so with your case expression all values above 5.7 will be classified to 1. You just need to change the order of the whens from highest to lowest:

CASE
    WHEN "HHO_mean" > 10 THEN 5
    WHEN "HHO_mean" > 8.0 THEN 4
    WHEN "HHO_mean" > 7.1 THEN 3
    WHEN "HHO_mean" > 6.4 THEN 2
    WHEN "HHO_mean" > 5.7 THEN 1
END 
1
  • 1
    Thank you for additional information ^_^
    – Taras
    Dec 19, 2022 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.