[Qgis-user] Convert symbology of qgis layer into PyQGIS code to define renderer

Tudorache, Marian Marian.Tudorache at navcanada.ca
Fri Jun 11 06:31:46 PDT 2021


Hi,

As far as I know symbology is tight to the topology.
Here is a simple example for lines.

mlayer = qgis.utils.iface.activeLayer()
styles = {'width': '0.5', 'color': 'yellow', 'penstyle': 'solid'}
registry = qgis.core.QgsSymbolLayerV2Registry.instance()
symbol = qgis.core.QgsSymbolV2.defaultSymbol(mlayer.geometryType())
lineLayer = lineMeta.createSymbolLayer(styles)
symbol.deleteSymbolLayer(0)
symbol.appendSymbolLayer(lineLayer)
renderer = qgis.core.QgsSingleSymbolRendererV2(symbol)
mlayer.setRendererV2(renderer)
mlayer.triggerRepaint()
qgis.utils.iface.mapCanvas().refresh()

For points
styles = {'name': 'diamond', 'color': 'yellow', 'size': '2'}
pointSymbol = qgis.core.QgsMarkerSymbolV2.createSimple(styles)
mlayer.rendererV2().setSymbol(pointSymbol)
qgis.utils.iface.mapCanvas().refresh()
mlayer.triggerRepaint()

for polygons
                    symbol.setColor(QtGui.QColor("blue"))
                    registry = QgsSymbolLayerV2Registry.instance()
                    lineMeta = registry.symbolLayerMetadata("SimpleLine")
                    lineLayer = lineMeta.createSymbolLayer({'width': '0.8', 'color': 'blue', 'offset': '0', 'penstyle': 'solid'})
                    symbol.appendSymbolLayer(lineLayer)
                    symbol.setAlpha(0.2)

                    renderer = QgsRuleBasedRendererV2(symbol)
                    vLayer.setRendererV2(renderer)
                    vLayer.setLayerTransparency(0)
                    vLayer.triggerRepaint()

This when you want to have static symbols.
QGIS also gives the capability of creating dynamic symbols
-----Original Message-----
From: Qgis-user <qgis-user-bounces at lists.osgeo.org> On Behalf Of Manuel Campagnolo
Sent: June 11, 2021 7:56 AM
To: qgis-user at lists.osgeo.org
Subject: [EXT] [Qgis-user] Convert symbology of qgis layer into PyQGIS code to define renderer

CAUTION: This email originated from outside of the organization. If you do not recognize the sender or you are not certain the content is safe, then do not click on any links or open any attachments.
AVERTISSEMENT : Ce courriel provient d’une source externe à l’organisation. Si vous ne reconnaissez pas l’expéditeur ou si vous doutez de la sécurité du contenu, ne cliquez sur aucun lien et n’ouvrez aucune pièce jointe.


Hi

Do you know of a way of using the style of a ,say, vector layer in QGIS3 to create automatically the python code that would generate it, namely the QgsSymbol part in the code below.

Thanks


symbol1=QgsSymbol.defaultSymbol(ifn_ppsm.geometryType())
symbol2=QgsSymbol.defaultSymbol(ifn_ppsm.geometryType())
symbol3=QgsSymbol.defaultSymbol(ifn_ppsm.geometryType())
symbol4=QgsSymbol.defaultSymbol(ifn_ppsm.geometryType())
symbol5=QgsSymbol.defaultSymbol(ifn_ppsm.geometryType())

symbol1.setColor(QColor('black'))
symbol2.setColor(QColor('gray'))
symbol3.setColor(QColor('dark green'))
symbol4.setColor(QColor('light green'))
symbol5.setColor(QColor('orange'))

cat1=QgsRendererCategory('Improdutivos', symbol1, 'Improdutivos') # category, symbol, label cat2=QgsRendererCategory('Urbano', symbol2, 'Urbano') cat3=QgsRendererCategory('Matos e Pastagens', symbol3, 'Matos e Pastagens') cat4=QgsRendererCategory('Floresta', symbol4, 'Floresta') cat5=QgsRendererCategory('Agricultura', symbol5, 'Agricultura') categories=[cat1,cat2,cat3,cat4,cat5] # list

renderer = QgsCategorizedSymbolRenderer('usosolo', categories)
ifn_ppsm.setRenderer(renderer)

_______________________________________________
Qgis-user mailing list
Qgis-user at lists.osgeo.org
List info: https://urldefense.com/v3/__https://lists.osgeo.org/mailman/listinfo/qgis-user__;!!P0kg3YazkQ!Wh7k2FBqY62GCB78RuenegW_YsQXCfSx7yC8s8SDK5rZJL96TYagaJQFJKb14VXtcg$
Unsubscribe: https://urldefense.com/v3/__https://lists.osgeo.org/mailman/listinfo/qgis-user__;!!P0kg3YazkQ!Wh7k2FBqY62GCB78RuenegW_YsQXCfSx7yC8s8SDK5rZJL96TYagaJQFJKb14VXtcg$

________________________________

This electronic message, as well as any transmitted files included in the electronic message, may contain privileged or confidential information and is intended solely for the use of the individual(s) or entity to which it is addressed. If you have received this electronic message in error please notify the sender immediately and delete the electronic message. Any unauthorized copying, disclosure or distribution of the electronic message is strictly forbidden. NAV CANADA accepts no liability for any damage caused by any virus and/or other malicious code transmitted by this electronic communication.

Le présent message électronique et tout fichier qui peut y être joint peuvent contenir des renseignements privilégiés ou confidentiels destinés à l’usage exclusif des personnes ou des organismes à qui ils s’adressent. Si vous avez reçu ce message électronique par erreur, veuillez en informer l’expéditeur immédiatement et supprimez le. Toute reproduction, divulgation ou distribution du présent message électronique est strictement interdite. NAV CANADA n’assume aucune responsabilité en cas de dommage causé par tout virus ou autre programme malveillant transmis par ce message électronique.


More information about the Qgis-user mailing list