5

I am setting a query on the symbology in QGIS.

I have a layer of point locations (586 points which show the location that data has been collected) and I have a layer of polygon areas (576 polygons the area of study). These two layers both have a common attribute which is a location code. I want the points and the polygons to display the same colour based on the location code.

Eg. Point 36 location code is abc123. Polygon 75 location code is also abc123. Both are coloured in green.

Does anyone know a way to query this in the symbology?

2 Answers 2

6

For the color, set a data-driven override > Assistant (see left screenshot). In the opening dialog panel (right screenshot), for the source select the code attribute, load Values from/to with the double arrow symbol and select a color ramp.

If the field is not a numerical one but of string type (as seems to be the case), in the source field introduce an expression that converts these values to numbers:

case 
    when code = 'abc123' then 1
    when code = 'def243' then 2
    when code = 'ghi653' then 3
end

Repeat this with the second layer and make the same settings to get the same color based on the same attribute value. If both layers have the same geometry type, you can copy/paste the style from the layer's context menu (right-click the layer in the layer panel).

enter image description here

4

A more sophisticated way is to create a project variable containing the expression used to assign colors to the different values of the code attribute. This is especially handy if you have a lot of layers to which you want to apply the same color scheme as you can make changes (e.g. defining another color ramp) once in the variable and all layers at once will automatically change.

First proceed as explained in my other solution, using assistant. When done, go to data driven override and select Edit.... Copy the expression you find there. It should look something like:

    coalesce(
        ramp_color(
            'Reds',  -- change the name or definition of the color ramp here
            scale_linear(
                case 
                    when code = 'abc123' then 1
                    when code = 'def243' then 2
                    when code = 'ghi653' then 3
                end, 
                1, 
                3, 
                0, 
                1
            )
        ), 
        '#000000'
    )

Copy this expression, then go to Menu Project > Properties > Variables Tab and add a new variable, here named color. For the value of this variable, paste the expression from above.

Now you can set the same color scheme for every layer you want. Click data driven override for the color > Edit and paste the expression eval(@color), where color is the name of the variable defined before:

enter image description here

enter image description here

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.