I'm trying to customize the rendering of a point layer in QGIS using python. Thus I have written a renderer class as explained in the documentation (http://www.qgis.org/pyqgis-cookbook/vector.html#creating-custom-renderers).
From what I have understood, I should define which symbol to render for each feature using the method 'symbolForFeature'. My problem is that inside this function when I try to get the attribute map using 'attributeMap' I always get an empty dictionary.
class MyRenderer(QgsFeatureRendererV2):
def __init__(self, syms=None):
QgsFeatureRendererV2.__init__(self, "MyRenderer")
self.syms = syms if syms else [ QgsSymbolV2.defaultSymbol(QGis.Point), QgsSymbolV2.defaultSymbol(QGis.Point) ]
def symbolForFeature(self, feature):
attrs = feature.attributeMap()
# attrs[0] should be the id
if attrs[0] > 10 :
return self.syms[0]
else :
return self.syms[1]
def startRender(self, context, vlayer):
for s in self.syms:
s.startRender(context)
def stopRender(self, context):
for s in self.syms:
s.stopRender(context)
def usedAttributes(self):
return []
def clone(self):
return MyRenderer(self.syms)
When I use this class, I get the error: 'KeyError: 0', simply because attrs is empty.
However, the other methods of the feature object seem to provide the expected results, for instance when I replace 'attrs[0]' by 'feature.id()' everything works fine.
What am I doing wrong?