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 '18 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 '18 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 '18 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 '18 at 4:19

Your Answer

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

Browse other questions tagged or ask your own question.