Is there a way in QGIS to use an expression to define a polygon feature's color based on whether it's adjacent to other features? I would like to achieve something like the following:

-If a feature has no adjacent features, it gets the default color.
-If two features are adjacent, one gets the default color and the other gets a secondary color.
-If another feature is adjacent to the both of the first two, it gets a tertiary color, and so on.

This is similar to the four-color theorem, except that I want to define the order in which colors are applied, so that the default color is always the first choice and the other colors are given second, third, fourth priority, etc.

The easiest way I can think of is to add a "Color ID" field and manually assign each feature a value of 0, 1, 2 or 3. But can I do this without adding an attribute, just using the existing geometry and searching for shared boundaries?

  • Did u solve that question ? U seem to want to define a sort order for the color of adjacent features but ... is there any logic to expres in which order adjacent features are found ?
    – Snaileater
    Feb 8, 2018 at 10:06
  • I have not solved it, other than the data-defined workaround. I don't know how you'd express the order in which features are found, but perhaps just the number of adjacent features is sufficient. For example, if there are no adjacent features, then color 0 must be used. If there is one adjacent feature, then QGIS randomly can choose either 0 or 1 for each (but not the same for both). If there are two adjacent features, choose randomly from 0, 1 or 2 (without duplication), and so on. I know I've seen this approach used in 4-color maps, where the fourth color is almost never used.
    – Nathan
    Feb 9, 2018 at 14:24
  • Did u check the plug-ins solving these 4-color maps ? https://gis.stackexchange.com/questions/89015/four-color-theorem-for-qgis-4-colour
    – Snaileater
    Feb 9, 2018 at 15:09
  • No, I haven't. This was a rather old question, so I'm no longer working on whatever project I needed this for. I think QGIS 3.0 will have some new tools for this; meanwhile, I probably just used the data-defined workaround.
    – Nathan
    Feb 11, 2018 at 4:19

1 Answer 1


Since QGIS 3.16, you have the new overlay functions you can use to get all kind of information about spatial relationships between different layers or between features on the same layer. You can use this expression for a data driven override of the color:

ramp_color (
    array_length (

Screenshot: Data driven override of the color; the label shows the no of overlapping neighbors, using this expression as label text: array_length (overlay_intersects( @layer, $id)) enter image description here

Explanation: use overlay_intersects( layer, expression) for the current layer (@layer) to get an array of the id's ($id) of intersecting features. With array_length, get the no. of intersecting features. Divide this by the number of max. overlapping polygons (in my case: 5) to get values from 0 to 1. Than use ramp_color(ramp_name,value) to select a color ramp and set the position inside of this ramp (0 to 1).

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.