Discussion:
how to set square symbol in a point layer with python
Stefano Masera
2014-10-13 08:11:34 UTC
Permalink
Hi list,
I wan't to set a square symbol in a point layer with python.

Currently, I use a graduated symbol legend, and I have a circle (point) symbol with this line for each item:

symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())

But I want to set a square for one item.

How can I do?

Thanks,
Stefano













Stefano Masera
Arpa Piemonte - SC06 Dipartimento Provinciale di Torino
Via Pio VII, 9 - 10135 Torino
Tel.: 011 19680349; Fax: 011 19681441
www.arpa.piemonte.it

************************************************************************
Riservatezza/Confidentiality

In ottemperanza al D.Lgs. n. 196 del 30/06/03 in materia di protezione
dei dati personali, le informazioni contenute in questo messaggio sono
strettamente riservate ed esclusivamente indirizzate al destinatario
indicato (oppure alla persona responsabile di rimetterlo al
destinatario). Vogliate tener presente che qualsiasi uso, riproduzione o
divulgazione di questo messaggio sono vietati. Nel caso in aveste
ricevuto questo messaggio per errore, vogliate cortesemente avvertire il
mittente e distruggere il presente messaggio.

According to italian law D.lgs. 196/2003 concerning privacy, if you are
not the addressee (or responsible for delivery of the message to such
person) you are hereby notified that any disclosure, reproduction,
distribution or other dissemination or use of this communication is
strictly prohibited. If you have received this message in error, please
destroy it and notify us by email.
Salvatore Larosa
2014-10-13 09:21:37 UTC
Permalink
Hi,

On Mon, Oct 13, 2014 at 10:11 AM, Stefano Masera
Post by Stefano Masera
Hi list,
I wan't to set a square symbol in a point layer with python.
Currently, I use a graduated symbol legend, and I have a circle (point)
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
But I want to set a square for one item.
How can I do?
after having got the symbol layer (QgsSimpleMarkerSymbolLayerV2) you
can use the setName() method to change from circle (default) to any
other symbol. In your case setName("square").

Regards,
-SL
--
Salvatore Larosa
linkedIn: http://linkedin.com/in/larosasalvatore
twitter: @lrssvt
skype: s.larosa
IRC: lrssvt on freenode
Stefano Masera
2014-10-13 09:51:17 UTC
Permalink
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.

Sorry and thank you!

C
S











----- Messaggio originale -----

Da: "Salvatore Larosa" <lrssvtml-***@public.gmane.org>
A: "Stefano Masera" <stefano.masera-byImbSz69/JkpYFk/***@public.gmane.org>
Cc: "qgis-developer" <qgis-developer-***@public.gmane.org>
Inviato: Lunedì, 13 ottobre 2014 11:21:37
Oggetto: Re: [Qgis-developer] how to set square symbol in a point layer with python

Hi,

On Mon, Oct 13, 2014 at 10:11 AM, Stefano Masera
Post by Stefano Masera
Hi list,
I wan't to set a square symbol in a point layer with python.
Currently, I use a graduated symbol legend, and I have a circle (point)
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
But I want to set a square for one item.
How can I do?
after having got the symbol layer (QgsSimpleMarkerSymbolLayerV2) you
can use the setName() method to change from circle (default) to any
other symbol. In your case setName("square").

Regards,
-SL
--
Salvatore Larosa
linkedIn: http://linkedin.com/in/larosasalvatore
twitter: @lrssvt
skype: s.larosa
IRC: lrssvt on freenode
Salvatore Larosa
2014-10-13 10:17:37 UTC
Permalink
Hi,

On Mon, Oct 13, 2014 at 11:51 AM, Stefano Masera
Post by Stefano Masera
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.
mySymbol.symbolLayer(0)
mySymbol.setName("square")
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
Stefano Masera
2014-10-13 11:49:20 UTC
Permalink
Perfect!

Thank you very mach!

Loading...