[Qgis-developer] Python plugin: how to set a symbol for a layer?

Martin Dobias wonder.sk at gmail.com
Sat Apr 2 16:40:38 EDT 2011


On Fri, Apr 1, 2011 at 9:14 AM,  <luca_manganelli at comune.trento.it> wrote:
>
> Hi,
>
> I've searched across documentation and mailing list, but I didn't found a
> page explaining how to set a symbol.
>
> For example:
>
>  - point layer: I want to use a circle with width 0.5 and gray colour with
> black border;
>  - line layer: width 1 and continous green line
>
> any hint?

Hi Luca

using the new symbology you can do it as follows:

- gray circle with black border:

props = { 'color' : '128,128,128' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleMarker").createSymbolLayer(props)
s = QgsMarkerSymbolV2([sl])

- green line with 1mm width:

props = { 'width' : '1', 'color' : '0,0,255' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
s = QgsLineSymbolV2([sl])

Then you will just use the symbol in a renderer and assign the
renderer to a layer:
layer.setRendererV2( QgsSingleSymbolRendererV2( s ) )

The syntax of properties is dependent on the symbol layer you use, I
guess there is no documentation for the accepted keys and values,
though you can save some symbols into qgis style and then inspect the
~/.qgis/symbology-ng-style.xml.

In order to make creation of simple symbols a bit easier, I have added
just now (r15663) some convenience methods:
s = QgsMarkerSymbolV2.createSimple( { ... } )
s = QgsLineSymbolV2.createSimple( { ... } )
s = QgsFillSymbolV2.createSimple( { ... } )

Regards
Martin


More information about the Qgis-developer mailing list