Skip to content
Permalink
Browse files

Allow specifying minimum number of colors for coloring map

  • Loading branch information
nyalldawson committed Feb 22, 2017
1 parent 1cf0a20 commit ac5e29224385f3add8e199c865e3b60093d1742f
Showing with 13 additions and 6 deletions.
  1. +2 −2 python/plugins/processing/algs/help/qgis.yaml
  2. +11 −4 python/plugins/processing/algs/qgis/TopoColors.py
@@ -547,9 +547,9 @@ qgis:texttofloat: >
qgis:topologicalcoloring: >
This algorithm assigns a color index to polygon features in such a way that no adjacent polygons share the same color index.

The algorithm attempts to assign colors so that the total number of colors required is minimized, whilst keeping the count of features assigned to each individual color index balanced.
The algorithm attempts to assign colors so that the total number of colors required is minimized, whilst keeping the count of features assigned to each individual color index balanced. A minimum number of colors can be specified if desired.

The color index is saved to a new attribute named color_idx.
The color index is saved to a new attribute named color_id.

qgis:translate: >
This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement.
@@ -38,6 +38,7 @@

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector

@@ -46,6 +47,7 @@

class TopoColor(GeoAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
MIN_COLORS='MIN_COLORS'
OUTPUT_LAYER = 'OUTPUT_LAYER'

def defineCharacteristics(self):
@@ -55,12 +57,15 @@ def defineCharacteristics(self):

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterNumber(self.MIN_COLORS,
self.tr('Minimum number of colors'), 1, 1000, 4))

self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Colored'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))

def processAlgorithm(self, feedback):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
min_colors = self.getParameterValue(self.MIN_COLORS)

fields = layer.fields()
fields.append(QgsField('color_id', QVariant.Int))
@@ -76,7 +81,9 @@ def processAlgorithm(self, feedback):
features = deque(f for f in vector.features(layer))

topology, id_graph = self.compute_graph(features, feedback)
feature_colors = ColoringAlgorithm.balanced(topology, feedback)
feature_colors = ColoringAlgorithm.balanced(topology,
feedback,
min_colors=min_colors)

max_colors = max(feature_colors.values())
feedback.pushInfo(self.tr('{} colors required').format(max_colors))
@@ -144,10 +151,10 @@ def compute_graph(features, feedback, create_id_graph=False):
class ColoringAlgorithm:

@staticmethod
def balanced(graph, feedback):
def balanced(graph, feedback, min_colors = 4):
feature_colors = {}
# start with 4 colors in pool
color_pool = set(range(1, 5))
# start with minimum number of colors in pool
color_pool = set(range(1, min_colors+1))

# calculate count of neighbours
neighbour_count = defaultdict(int)

0 comments on commit ac5e292

Please sign in to comment.
You can’t perform that action at this time.