5

I have a polygon layer and want to change the colour of single polygons.

Usually I use a rule based symbolization and filter the polygons be their "ID". But by that I have 30 or more rules.

Is there any other way to change the colour of single polygons in the same layer? For example with select features by area or single click ?

4
  • do you have a specific scheme or would just a random color per polygon work?
    – Ian Turton
    May 27, 2020 at 12:33
  • I have specfic colours for every polygon
    – joe_gerner
    Jun 2, 2020 at 7:08
  • 4
    You can store the color in a field and use that field for generate the color of a simple fill (small dropdown at the end-right of the color button). Select polygons, update the color field of the selected, invert the selection, update to other color. It's possible to link a small PyQGIS code for manage that at each selection. Jun 3, 2020 at 12:17
  • You could use categories to assign colors randomly or by a color ramp, and then looking at the result change individual colors. Once you have something you like you could save it as style or layer file
    – John
    Jun 3, 2020 at 14:15

1 Answer 1

12
+50

Here is a simple workaround with no Rule-based Symbolization.

Let's assume there is a polygon layer "Layer_A" (purple) with its corresponding attribute table accordingly, see image below.

input

Proceed with Properties... > Symbology > Single symbol > Fill colour > Edit...

single_symbol

Search for colours you want to use and connect them with polygons by "id" or any other attribute.

condition

Schema 1. When each feature is unique

CASE
WHEN "id" = 1 THEN '#colour1'
WHEN "id" = 2 THEN '#colour2'
...
ELSE '#colourN'
END 

Schema 2. When the features can be filled with the same colour

CASE
WHEN "id" IN (0,1,2,3,4) THEN '#colour1'
WHEN "id" IN (5,6,7,8,9) THEN '#colour2'
...
ELSE '#colourN'
END 

The output will look as following

result1

The same approach can be applied to Fill style, Stroke width, Stroke style etc.


Moreover, it is possible to get colours randomly using the following expression:

color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))

Additionally, you may try using a Python function.

In 'Function Editor' create a new function with such code:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
import random

@qgsfunction(args='auto', group='Custom')
def colfunc(feature, parent):

    layer = iface.activeLayer()

    for i in range(layer.featureCount()):
        c = lambda: random.randint(0,255)
        return '#{:02x}{:02x}{:02x}'.format(c(),c(),c())

Function Editor

Apply 'Save and Load functions' and call this function in 'Expression window' with colfunc() and click 'Ok'.

Function

So, the expected result might be looking as

result2


References:

2
  • Is it possible to add alpha to the result?
    – RaRdEvA
    Dec 14, 2021 at 22:44
  • For sure, to which approach would you like to add the alpha?
    – Taras
    Dec 15, 2021 at 6:00

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.