2

I am trying to set a categorised renderer for a point layer, according to its categories. I used this code:

vector = QgsVectorLayer("route to shape", "title", "ogr")      
mi_rend = QgsCategorizedSymbolRendererV2() 
mi_rend.setClassAttribute('TheAttribute') #Do I need one more attribute?
vector.setRendererV2(mi_rend)
QgsMapLayerRegistry.instance().addMapLayer(vector)

The problem is that the layer is loaded in the table of contents, nevertheless, it doesn't appear. I have been looking for solutions in this forum but I haven't found any solution at the moment.

1 Answer 1

2

I don't see you setting any categories. Use addCategory() to add categories.

Or even much more intuitive (modified from the example in Lawhead QGIS Python programming cookbook, pp 142):

# next line is pseudo code !! Replace and extent with your values !!
# my_classes = {class_value: (symbol_property, label_text), ...}
# for example:
my_classes = {1: ('yellow', 'First'),
              2: ('red', 'Second'),
              3: ('green', 'Third'),
              4: ('blue', 'Fourth')}
categories = []

for class_value, (symbol_property, label_text) in my_classes.items():

    # get default symbol for this geometry type
    symbol = QgsSymbolV2.defaultSymbol(vector.geometryType())

    # set symbol properties as you like
    # symbol.set*(symbol_property)
    symbol.setColor(QColor(symbol_property))

    # create a category with these properties
    category = QgsRendererCategoryV2(class_value, symbol, label_text)
    categories.append(category)

mi_rend = QgsCategorizedSymbolRendererV2('ESTRELLA', categories) 
vector.setRendererV2(mi_rend)

In this example category values, symbol properties (such as color, size, svg filename), and label strings are put into a dict. Then a loop picks each dict element one by one, creates a renderer category, and saves it in a list. Finally the renderer is initialized with this list of categories and assigned to the layer.

3
  • Thank you very much, but I don't know what "class_value" could be. Is this correct? my_classes = {'class_value' = '1,2,3,4'} I ' m looking for rendering 4 categories of points, which correspond to the attribute (which is called 'ESTRELLA') and has four values (1,2,3,4) Thank you again.
    – user64823
    Mar 30, 2016 at 11:39
  • Edited answer. I have declared the dict with 4 entries. You have to specify at least the label text part, and the colors, if you wish. You can also vary the symbol size, in this case give sizes (e.g. 8, 10,12,14) instead of color names, and later call setSize().
    – Detlev
    Mar 30, 2016 at 12:29
  • Thank you, I was checking this code with mine, absolutely works very good. I didn't find any example with QgsCategorizedSymbolRendererV2() class, so I didn't understand the parameters very good.
    – user64823
    Mar 30, 2016 at 15:04

Your Answer

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

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