Skip to content

Commit ac5e292

Browse files
committed
Allow specifying minimum number of colors for coloring map
1 parent 1cf0a20 commit ac5e292

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

python/plugins/processing/algs/help/qgis.yaml

100644100755
+2-2
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,9 @@ qgis:texttofloat: >
547547
qgis:topologicalcoloring: >
548548
This algorithm assigns a color index to polygon features in such a way that no adjacent polygons share the same color index.
549549

550-
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.
550+
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.
551551

552-
The color index is saved to a new attribute named color_idx.
552+
The color index is saved to a new attribute named color_id.
553553

554554
qgis:translate: >
555555
This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement.

python/plugins/processing/algs/qgis/TopoColors.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
from processing.core.GeoAlgorithm import GeoAlgorithm
4040
from processing.core.parameters import ParameterVector
41+
from processing.core.parameters import ParameterNumber
4142
from processing.core.outputs import OutputVector
4243
from processing.tools import dataobjects, vector
4344

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

4748
class TopoColor(GeoAlgorithm):
4849
INPUT_LAYER = 'INPUT_LAYER'
50+
MIN_COLORS='MIN_COLORS'
4951
OUTPUT_LAYER = 'OUTPUT_LAYER'
5052

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

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

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

6165
def processAlgorithm(self, feedback):
6266
layer = dataobjects.getObjectFromUri(
6367
self.getParameterValue(self.INPUT_LAYER))
68+
min_colors = self.getParameterValue(self.MIN_COLORS)
6469

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

7883
topology, id_graph = self.compute_graph(features, feedback)
79-
feature_colors = ColoringAlgorithm.balanced(topology, feedback)
84+
feature_colors = ColoringAlgorithm.balanced(topology,
85+
feedback,
86+
min_colors=min_colors)
8087

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

146153
@staticmethod
147-
def balanced(graph, feedback):
154+
def balanced(graph, feedback, min_colors = 4):
148155
feature_colors = {}
149-
# start with 4 colors in pool
150-
color_pool = set(range(1, 5))
156+
# start with minimum number of colors in pool
157+
color_pool = set(range(1, min_colors+1))
151158

152159
# calculate count of neighbours
153160
neighbour_count = defaultdict(int)

0 commit comments

Comments
 (0)