I am using the Rule-based styling properties (QGIS 2.2) to determine the colour of grid cells for a merged shapefile. Each grid cell defined by its ID may be overlapped by several layers as defined by Layer and each Layer has its own Score (a single column).

Focusing on ("ID" = 80) as shown in the image, I want to create a case statement where this ID has an overall Score of 6 or above which will make that cell become red.

I have tried something similar to the following:

case when "LAYER" IN ( 'Site_11' , 'Site_12' , 'Site_18' )
then "SCORE" +"SCORE" + "SCORE" >=6

This is wrong but it's the kind of logic I'm following at the moment.

Any advice welcome!

enter image description here

enter image description here

  • Many thanks for your answers as they clearly describe how to create expressions. I've added an image of the rules I used. I am close to what I want to achieve and apologies if I wasn't crystal clear - if 1 grid cell ("ID" = 80) is overlapped by 3 layers (Site_11, Site_12 & Site_18) then I want an expression that sums up the "Score" (which is a single column). So if ID 80 has a total score of 6 or more then it will be red. Although I'm beginning to think it may not be possible to add a column onto itself. – Joseph Jun 24 '14 at 15:11
  • Ah... this is a totally different question. And I think you can't do it only by using symbology. You will have to work your data to calculate those Scores before you can style it. – Alexandre Neto Jun 24 '14 at 15:25
  • @AlexandreNeto, Yes you may be correct. Thank you anyway as I have learnt much more about the styling properties. – Joseph Jun 24 '14 at 15:28
  • 1
    BTW, "Layer" = 'Site_18' OR "Layer" = 'Site_11' OR "Layer" = 'Site_12' can be substituted by Layer IN ('Site_18','Site_11', 'Site_12') – Alexandre Neto Jun 24 '14 at 15:28
  • @AlexandreNeto, Awesome! Many thanks for that tip! – Joseph Jun 24 '14 at 15:29

You may want to look at doing rule based styling if all you are trying to do is color code features based on attributes in your table. You can access this from the Properties window, then under Style. Within the Style section you can get to the rule based option from the Single Symbol drop down (choose Rule-based). Here you can add rules based on your attributes, which will allow you to define a color schema and the overall look and feel of the layer.

enter image description here

enter image description here

  • Ahhhh apologies Ryan, I was using Rule-based styling (the Expression string builder looks remarkably similar to the Field Calculator!). I have edited my question. – Joseph Jun 24 '14 at 14:08
  • No problem Joseph. What I do in cases like yours is to add a filter for each option (ie: CASE). So if you have 4 options you are looking at, then you would want to add 4 different filters. Each filter could have a multiple attribute query. I have added a graphic showing what I mean. – Ryan Garnett Jun 24 '14 at 14:37
  • Thanks Ryan, I have added a comment to my question which hopefully makes it a bit more clear as to what I am looking for. – Joseph Jun 24 '14 at 15:19

From what I understood from your question, you can do this in too different approaches.

Using Categories

You can use the Expression builder to write conditional expressions that will create all your output different combinations:

enter image description here

You start by pressing the Expression button and write the expression:

CASE WHEN "Score_1"+"Score_2" > 6
THEN 'Bigger than 6'
ELSE 'Lower than 6'

Then you press classify, and all the options (that match your data) appear in the list of symbols.

Notice that you can put a brand new CASE WHEN condition inside the ELSE result to evaluate other conditions and change get other results. Like this:

CASE WHEN "Score_1"+"Score_2" > 6
THEN 'Bigger than 6'
    CASE WHEN "Score_1"+"Score_2" > 2
    THEN 'Bigger than 2'
    ELSE 'Not enough score' END

Using Rule-based Styling

Remember that the CASE WHEN statement returns a result, not a condition evaluation (true or false) so it's of "no use" in Rule-based symbology.

In this case you need to create a rule for each different combination.

enter image description here

Rule 1: "Score_1"+"Score_2" > 6

Rule 2: "Score_1"+"Score_2" > 2 AND "Score_1"+"Score_2" <= 6

Rule 3: ELSE

Notice that you have to create exclusive rules or your data will be symbolized twice (that's why you need the AND "Score_1"+"Score_2" <= 6 in the second rule)

Also notice that you can use the rule ELSE to style everything that is not matched on the other rules.

  • Thanks Alexandre, I have added a comment to my question which hopefully makes it a bit more clear as to what I am looking for. – Joseph Jun 24 '14 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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