Background : I create a lot of symbology using 'saturation' ramps - for want of a better term, e.g. where the hue and value of each symbol stays the same, but the saturation changes. I couldn't figure out how to do this easily in qgis (EDIT: see comment - its easy when you know how), and each time I classify items, the colours reset to a colour ramp rather than a saturation ramp.
I figured this should be relatively easy to do using pyqgis, which it turns out has been, using this code (built for 5 symbols):
from PyQt4.QtGui import QColor # import this namespace as I want to refer to QColor later layer = qgis.utils.iface.activeLayer() i = 0 # used to increment the saturation value for sym in layer.rendererV2.symbols(): # loop through each symbol in the layer using 'new symbology' i += 50 sym.setColor(QColor.fromHsv(40,i,255))
Problem: . The above code works fine, but is not dynamic, in that I've set it up to increase the saturation by 50 each time (giving 5 graduations between 0 and 255). I want to set the saturation to graduate dynamically based on the total number of symbols.
The mathematics of this are pretty straight-forward, but I'm not able to get the count from the symbols() method. I've tried:
>>> rendererV2.symbols().count() Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: count() takes exactly one argument (0 given)
... which as you can see returns an error (although the QT documentation seems to list count() as a method of a QList<> object).
>>> rendererV2.symbols().count(QgsSymbol) 0
... which as you can see returns 0, which isn't true. There are 5 items in the list.
I'm sure I must be doing something daft, but being new to python, pyqgis and qt, it is beyond my ability to see what at present.