Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow specifying minimum number of colors for coloring map
  • Loading branch information
nyalldawson committed Feb 22, 2017
1 parent 1cf0a20 commit ac5e292
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/help/qgis.yaml 100644 → 100755
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions python/plugins/processing/algs/qgis/TopoColors.py
Expand Up @@ -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

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

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

def defineCharacteristics(self):
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ac5e292

Please sign in to comment.