2
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def Symbology():
    canvas = qgis.utils.iface.mapCanvas()
    layer = qgis.utils.iface.activeLayer()
    for feat in layer.getFeatures():
        Value = feat['field']
        print Value 
        if Value != '':
            featColor = {
                  Value :  ('red',Value ),
                    }
        elif Value == '':
             featColor = {
                  '' :  ('green','')
                    }
        ranges = []
        for rType, (color, label) in featColor.items():
            print rType,(color, label)
            symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
            symbol.setColor(QColor(color))
            category = QgsRendererCategoryV2(rType, symbol, label)
            ranges.append(category)

    # create the renderer and assign it to a layer
    expression = 'field' # field name
    renderer = QgsCategorizedSymbolRendererV2(expression, categories)
    layer.setRendererV2(renderer)
    canvas.zoomToSelected()
    layer.removeSelection()

Here i have to set based on the feature value but when i am executing this script it is setting for only one value. I f i want to set colors based on the values how can i do that?

The value contains for example Value = ['x','y',NULL]

1 Answer 1

2

Did you consider rule based rendering instead of categorized rendering?

https://qgis.org/api/2.18/classQgsRuleBasedRendererV2.html

rsp.

https://qgis.org/api/classQgsRuleBasedRenderer.html for QGIS 3

2
  • Could you please update the link?
    – Aaron
    Commented Dec 6, 2017 at 13:38
  • @Aaron Links updated for the current 2.18 LTR and the master version Commented Dec 6, 2017 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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