[Qgis-developer] Re: scaling points size according to attribute values

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Wed Dec 30 21:07:06 EST 2009


Hello list
Having fixed my problem, I'm going to reply to myself, leaving this as
a reference for someone who may stumble upon this in the future.

In order to get the size of the points of my vector layer scaled
according to some attribute's values I had to:
- create a new symbol object
- set its graphical properties, including the scale field
- create a new renderer of type QgsSingleSymbolRenderer
- add the symbol to the renderer
- add the renderer to the vector layer

so, the working python code is something like:
...
symbol = QgsSymbol(layer.geometryType())
symbol.setBrush(oldSymbol.brush())
...
(set the rest of the symbol's properties)
...
symbol.setScaleClassificationField(provider.fieldNameIndex("gid"))
newRend = QgsSingleSymbolRenderer(layer.geometryType())
newRend.addSymbol(symbol)
layer.setRenderer(newRend)
layer.triggerRepaint()


To be honest I was amazed that this worked. The reason for that is
that the class QgsSingleSymbolRenderer is supposed (as shown by the
online API docs) to have an updateSymbolAttributes() method. This
method is in fact mysteriously missing from the python bindings... I
don't know how but it seems there is some other method that updates
the symbols' properties and my code is somehow calling it. I guess its
the triggerRepaint() slot of QgsVectorLayer that originates this
updating of the symbols' properties...

I also didn't understand why it was needed to create a new
QgsSingleSymbolRenderer instead of just changing the old one, which
didn't work...

Can someone clarify this in a kind of a newbie-friendly explanation?
Thanks

On Sun, Dec 27, 2009 at 8:32 PM, Ricardo Filipe Soares Garcia da
<ricardo.garcia.silva at gmail.com> wrote:
> Hello list
> How can I change a points' size according to the values of one of its
> attributes programatically? What I mean is achieving the same effect
> that the GUI allows in layer properties -> symbology -> area scale
> field ?
>
> I have been trying to use the setScaleclassificationField method of
> the QgsSymbol class, but it doesn't seem to work...
> Here is a piece of my python code, where I try to scale the symbol
> according to the gid attribute of a postgis layer:
>
> ...
> renderer = layer.renderer()
> symbols = renderer.symbols()
> symbols[0].setScaleClassificationField(provider.fieldNameIndex("gid"))
> layer.triggerRepaint()
>
> Instead of what I wanted, this piece of code will reset the symbols'
> size to 1 and it doesn't scale them according to the "gid" attribute
> at all.
> thanks for your help
>
>
> --
> ___________________________ ___ __
> Ricardo Garcia Silva
>



-- 
___________________________ ___ __
Ricardo Garcia Silva


More information about the Qgis-developer mailing list