Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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).

I've tried:

>>> 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.

Help?

share|improve this question
    
This would also make a good feature request. –  Nathan W Oct 16 '12 at 1:00
    
I've just realised that I can actually achieve a 'saturation ramp' using QGIS color ramp, and setting the second colour to white! Exactly the same effect. –  Jayden Nov 5 '12 at 23:21
add comment

1 Answer

up vote 2 down vote accepted

You can use len()

 len(layer.rendererV2().symbols())

Like so:

 >>> len(layer.rendererV2().symbols())
 1

EDIT: Count doesn't work because you are getting back a native Python list not a QList and the Python count method is different. There are no QList binding in PyQt because they are auto converted to list.

>>> type(layer.rendererV2().symbols()) is list
True

Count in Python:

>>> mylist = [1,4,5,5,6,7,7,7,7,7,7,7]
>>> mylist.count(5)
2

Counts the number of 5s in the list.

share|improve this answer
    
Great, thanks. Obvious when you know how. I'm interested as to why the count() on the QList doesn't work still however? Any insight? –  Jayden Oct 16 '12 at 1:23
    
See my edit to the answer. –  Nathan W Oct 16 '12 at 1:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.