[Qgis-user] Convert symbology of qgis layer into PyQGIS code to define renderer
Jorge Gustavo Rocha
jgr at geomaster.pt
Fri Jun 11 07:12:21 PDT 2021
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
>
> <!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
> <qgis simplifyLocal="1" labelsEnabled="0" simplifyMaxScale="1"
> simplifyDrawingTol="1" simplifyDrawingHints="1" simplifyAlgorithm="0"
> version="3.18.1-Zürich" readOnly="0" hasScaleBasedVisibilityFlag="0"
> minScale="100000000" maxScale="0" styleCategories="AllStyleCategories">
> <flags>
> <Identifiable>1</Identifiable>
> <Removable>1</Removable>
> <Searchable>1</Searchable>
> <Private>0</Private>
> </flags>
> <temporal durationUnit="min" enabled="0" startExpression=""
> fixedDuration="0" startField="" mode="0" durationField=""
> accumulate="0" endField="" endExpression="">
> <fixedRange>
> <start></start>
> <end></end>
> </fixedRange>
> </temporal>
> <renderer-v2 forceraster="0" attr="layer" enableorderby="0"
> symbollevels="0" type="categorizedSymbol">
> <categories>
> <category value="interior" render="true" symbol="0" label="Faixa
> interior"/>
> <category value="exterior" render="true" symbol="1" label="Faixa
> exterior"/>
> </categories>
> <symbols>
> <symbol name="0" clip_to_extent="1" force_rhr="0" alpha="0.7"
> type="fill">
> <data_defined_properties>
> <Option type="Map">
> <Option value="" name="name" type="QString"/>
> <Option name="properties"/>
> <Option value="collection" name="type" type="QString"/>
> </Option>
> </data_defined_properties>
> <layer enabled="1" locked="0" pass="0" class="SimpleFill">
> <Option type="Map">
> <Option value="3x:0,0,0,0,0,0"
> name="border_width_map_unit_scale" type="QString"/>
> <Option value="128,128,128,255" name="color" type="QString"/>
> <Option value="bevel" name="joinstyle" type="QString"/>
> <Option value="0,0" name="offset" type="QString"/>
> <Option value="3x:0,0,0,0,0,0"
> 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 at 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 at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
More information about the Qgis-user
mailing list