3

I am trying to use Conditional Statements to assign distinguished colors to 10 different polygons within my shapefile. I want to have control over selection of the color for each polygon. Therefore I :

  1. Opened Attribute Table
  2. Select features using an expression
  3. Selected Fields and Values ( I chose the ID attribute)

did something like this in expression window :

CASE
    WHEN "ID"=36 THEN color_rgb(0,102,204)
    WHEN "ID"=50 THEN color_rgb(255,128,0)
    WHEN "ID"=63 THEN color_rgb(0,204,0)
    WHEN "ID"=77 THEN color_rgb(204,0,0)
    WHEN "ID"=104 THEN color_rgb(255,0,255)
    WHEN "ID"=123 THEN color_rgb(102,51,0)
    WHEN "ID"=128 THEN color_rgb(255,153,255)
    WHEN "ID"=130 THEN color_rgb(128,128,128)
    WHEN "ID"=143 THEN color_rgb(204,204,0)
    WHEN "ID"=163 THEN color_rgb(0,204,204)
    ELSE color_rgb(0,0,0)
END

Now when I press Select Features I expect to have 10 colorful polygons as the result, however by doing this I only get a new color for all the polygons.

enter image description here

1
  • 2
    Selection will always cause the selected polygons to be yellow (or whatever other colour is set in the project settings)
    – Ian Turton
    Commented Jul 1, 2021 at 8:21

1 Answer 1

9

If you use the "select features using an expression" you are only selecting feature so the selected feature appear in the selection color (you can change this selected features color but all polygon will have the same color, not what you want)

To give each polygon it's own color by using an expression you need to use "data defined override" for fill color and paste your expression there.

To do that :

  1. Set your symbology to "Single symbol"
  2. Select the "simple fill" symbol layer
  3. Click on the data defined override just on the right of the fill color selector and choose edit in the menu
  4. Paste your expression in the expression builder

enter image description here

Another way of doing it would be to use rule based symbology where you set a rule for each polygon color (dont put the color in the rule, it should just "select" the polygon so a rule would look like "ID"=50) then you style each rule as you like. This second solution is better if you need a legend as you won't get one with data defined override solution

1
  • Thanks a lot for the precise answer and also the suggestion of the rule-based symbology. It looks like a powerful tool to be true.
    – Sinooshka
    Commented Jul 2, 2021 at 11:40

Your Answer

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

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