[Qgis-developer] how to set square symbol in a point layer with python

Salvatore Larosa lrssvtml at gmail.com
Mon Oct 13 03:17:37 PDT 2014


Hi,

On Mon, Oct 13, 2014 at 11:51 AM, Stefano Masera
<stefano.masera at arpa.piemonte.it> wrote:
> Thanks for the answer!
> Sorry but I didn't understand how to use QgsSimpleMarkerSymbolLayerV2.
> I did some attempt but I didn't manage..
> Below there's my code (with only two symbol item as example)
>
> myTargetField = field
> myRangeList = []
> myOpacity = 1
>
>
> # symbol 1
> mySymbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
> mySymbol.setColor(QColor('#FFFFFF'))
> mySymbol.setAlpha(myOpacity)
> myRange = QgsRendererRangeV2(0.1,34.4,mySymbol,"< 35")
> myRangeList.append(myRange)
> # symbol 2
> mySymbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
> mySymbol.setColor(QColor('#238443'))
> mySymbol.setAlpha(myOpacity)
> myRange = QgsRendererRangeV2(34.5,39.4,mySymbol,"35 - 39 dB(A)")
> myRangeList.append(myRange)
>
> # other symbol items
>
>
> myRenderer = QgsGraduatedSymbolRendererV2('', myRangeList)
> myRenderer.setMode(QgsGraduatedSymbolRendererV2.EqualInterval)
> myRenderer.setClassAttribute(myTargetField)
>
> layer.setRendererV2(myRenderer)
> iface.legendInterface().refreshLayerSymbology(layer)
> layer.triggerRepaint()
>
>
>
> And I want that symbol 1 became a square.
> I can't understand how to use QgsSimpleMarkerSymbolLayerV2.

you can get the symbol layer from QgsSymbolV2 instance:

>>> mySymbol.symbolLayer(0)

and then change the name for the markersymbol:

>>> mySymbol.setName("square")

a working example which you can test from python console is:

>>> layer = iface.activeLayer()
>>> mySymbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
>>> mySymbolLayer = mySymbol.symbolLayer(0)
>>> mySymbolLayer.setName("square")
>>> layer.rendererV2().symbols()[0].changeSymbolLayer(0, mySymbolLayer)
>>> iface.legendInterface().refreshLayerSymbology(layer)

Best Regatds,
-SL

-- 
Salvatore Larosa
linkedIn: http://linkedin.com/in/larosasalvatore
twitter: @lrssvt
skype: s.larosa
IRC: lrssvt on freenode


More information about the Qgis-developer mailing list