2

I am trying to colorize all features in all layers in my QGIS 2.8.2-Document based on a color-value, that is being determined for every Feature individually. I do have a QtGui.QColor instance for every feature, but I still need to apply it to the Feature via a Renderer.

from PyQt4 import QtGui, QtCore
canvas = qgis.utils.iface.mapCanvas()
layers = canvas.layers()

for layer in layers:
    iter = layer.getFeatures()
    for feature in iter:
        if  feature['ELEM_NR'] != '' and feature['ELEM_NR'] != NULL:
            # 
            color = QtGui.QColor()

            # ...
            # hexValue is being determined based on feature['ELEM_NR']
            # ...

            color = QtGui.QColor(hexValue )

            # how can I apply color to feature`?    

Background (why I do not simply use graduated symbols):

Every Layer has an attribute that is kind of an id. I want that every Feature (no matter which layer it is in), that has the same attribute-value, is colored the same way.

Because the the number of features differs between layers and the value range of the attribute is quit big, neither the catgorized nor the graduatedSymbol-Renderer descibed here would bring the desired result (at least in my understanding).

Using a custom renderer as described in the documentation looks promising, but I can't figure how to include this.

Any Idea?

  • You have the color value on the feature as a field? – Nathan W Oct 12 '15 at 10:27
  • Not yet, but I could easily generate such a field. How would that help? – geoM Oct 12 '15 at 10:28
  • I'm really confused on what you are trying to do. It seems you have a hex value that you are generating based on a field? Is that correct? – Nathan W Oct 12 '15 at 10:29
  • Yes, that's right. – geoM Oct 12 '15 at 10:37
  • Background is this: I have a road-network with complex IDs (something like 21548266_21548267_01). The features (street objects) in several thematic layers have the same attribute and are matched to single roads by sharing the same ID. My task is to prove that allocation done by others, which is not possible with a script, only by visual inspection. Because labels are not quite handy, I would like to use colorization. – geoM Oct 12 '15 at 10:44
1

If you have a field (or you can use an expression for more complex things) you can bind the color to the field and use that value

enter image description here

Color field in this case just has a hex value #FF33FF

| improve this answer | |
  • Okay, that worked for me so far, thanks. But it still would be handy set the attribute field as color-string programmatically. I would like to avoid doing this for every layer eacht time. Any Ideas anyone? – geoM Oct 23 '15 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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