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

2021-06-11 Thread Jorge Gustavo Rocha
Olá Manuel,

First save the style (using the GUI or using PyQGIS) as a qml file.

To apply the style to another layer (use the Python console to test the
code):

1) Select the other layer you want to apply the style

2) Run the PyQGIS code:

l = iface.activeLayer()
l.loadNamedStyle('/home/manuel/awesome.qml')
l.triggerRepaint()

Good luck!

Gustavo

On 11/06/21 14:58, Manuel Campagnolo wrote:
> Olá Gustavo
>
> How do I apply the named style afterwards in PyQGIS from the style qml
> file as the example below?
>
> Thanks again
>
> Manuel
>
> http://mrcc.com/qgis.dtd' 'SYSTEM'>
>  simplifyDrawingTol="1" simplifyDrawingHints="1" simplifyAlgorithm="0"
> version="3.18.1-Zürich" readOnly="0" hasScaleBasedVisibilityFlag="0"
> minScale="1" maxScale="0" styleCategories="AllStyleCategories">
>   
>     1
>     1
>     1
>     0
>   
>    fixedDuration="0" startField="" mode="0" durationField=""
> accumulate="0" endField="" endExpression="">
>     
>   
>   
>     
>   
>    symbollevels="0" type="categorizedSymbol">
>     
>   
>   
>     
>     
>    type="fill">
>     
>   
>     
>     
>     
>   
>     
>     
>   
>      name="border_width_map_unit_scale" type="QString"/>
>     
>     
>     
>      name="offset_map_unit_scale" type="QString"/>
>
>
> [...]
>
>
> On 11/06/2021 14:41, Jorge Gustavo Rocha wrote:
>> Olá Manuel,
>>
>> There is no way to get the PyQGIS code from the style, as far as I know.
>>
>> But you can save the style (using the native QGIS format) and apply the
>> named style afterwards in PyQGIS easily. Is this alternative workflow
>> suitable for you?
>>
>> Regards,
>>
>> Gustavo
>>
>> On 11/06/21 12:56, Manuel Campagnolo wrote:
>>> 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@lists.osgeo.org
>>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> ___
>> Qgis-user mailing list
>> Qgis-user@lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


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

2021-06-11 Thread Tudorache, Marian
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  On Behalf Of Manuel 
Campagnolo
Sent: June 11, 2021 7:56 AM
To: qgis-user@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@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 

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

2021-06-11 Thread Manuel Campagnolo

Olá Gustavo

How do I apply the named style afterwards in PyQGIS from the style qml 
file as the example below?


Thanks again

Manuel

http://mrcc.com/qgis.dtd' 'SYSTEM'>
simplifyDrawingTol="1" simplifyDrawingHints="1" simplifyAlgorithm="0" 
version="3.18.1-Zürich" readOnly="0" hasScaleBasedVisibilityFlag="0" 
minScale="1" maxScale="0" styleCategories="AllStyleCategories">

  
    1
    1
    1
    0
  
  fixedDuration="0" startField="" mode="0" durationField="" accumulate="0" 
endField="" endExpression="">

    
  
  
    
  
  symbollevels="0" type="categorizedSymbol">

    
  
  

    
    
  type="fill">

    
  
    
    
    
  
    
    
  
    name="border_width_map_unit_scale" type="QString"/>

    
    
    
    type="QString"/>



[...]


On 11/06/2021 14:41, Jorge Gustavo Rocha wrote:

Olá Manuel,

There is no way to get the PyQGIS code from the style, as far as I know.

But you can save the style (using the native QGIS format) and apply the
named style afterwards in PyQGIS easily. Is this alternative workflow
suitable for you?

Regards,

Gustavo

On 11/06/21 12:56, Manuel Campagnolo wrote:

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@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


--
-
Manuel Campagnolo
https://www.isa.ulisboa.pt/id/mlc

___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


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

2021-06-11 Thread Jorge Gustavo Rocha
Olá Manuel,

There is no way to get the PyQGIS code from the style, as far as I know.

But you can save the style (using the native QGIS format) and apply the
named style afterwards in PyQGIS easily. Is this alternative workflow
suitable for you?

Regards,

Gustavo

On 11/06/21 12:56, Manuel Campagnolo wrote:
> 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@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user