8

Im loading a shapefile using the method QgsVectorLayer() and adding a style to it using the method loadSldStyle() and the path to the sld file. So far so good.

What I want now is to export that style as .qml file. I suppose that maybe I could use the method exportNamedStyle() but this method use an argument of type QDomDocument. I have no idea how to create this this object.

Can anyone tell me how to export the .qml file of this loaded layer using python?

Using the method saveNamedStyle() it only works if I use the python console inside qgis. In the python console in the qgis desktop I did this:

vlayer = iface.addVectorLayer("/home/inesf/sag/prodgen/SAGautodataset/Cystoseira_compressa.shp", 'mylayer', 'ogr')
vlayer.loadSldStyle("/home/inesf/sag/prodgen/SAGautodataset/Cystoseira_compressa.sld")
vlayer.saveNamedStyle("/home/inesf/sag/prodgen/SAGautodataset/Cystoseira_compressa.qml")

And it worked fine, it exported a .qml file with the same style defined in the SLD file. Perfect.

When I do this in the python console outside the QGIS desktop, it exports a .qml file but empty.

When I run a python program with this code with the same code, does not give errors, but nothing happens.

Alexandre Neto
  • 14,212
  • 2
  • 58
  • 85
Falcoa
  • 335
  • 2
  • 9

1 Answers1

5

You can use QgsMapLayer::saveNamedStyle to export .qml files:

layer.saveNamedStyle('/path/to/style.qml')

EDIT:

Yes, you can create a .qml file outside QGIS. I tested this in a standalone script (which was mentioned in the comment by @AlexandreNeto).

This is the code I used (note that I use Windows so you will need to change your paths accordingly):

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *

from os.path import expanduser
home = expanduser("~")

QgsApplication( [], False, home + "/AppData/Local/Temp" )
QgsApplication.setPrefixPath("C://OSGeo4W64//apps//qgis", True)
app = QApplication([], True)
QgsApplication.initQgis()

layer = QgsVectorLayer(home + "/Desktop/New folder//polygon example.shp", "EligibleAreas_polygons", "ogr")
layer.loadSldStyle(home + "/Desktop/sld_style.sld")
layer.saveNamedStyle(home + "/Desktop/qml_style.qml")

QgsApplication.exitQgis()
app.exit()

  1. First I saved the .sld file inside QGIS:

    Saving sld file

  2. Then I ran the script which loads the layer, then the .sld style and lastly saves this as a .qml file:

    Running script

  3. Finally, I loaded the .qml file into QGIS and the style is exactly the same as the one in the .sld file:

    Result

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • do you know if it is possible to create a qml file without installing qgis? – ePascoal Apr 05 '16 at 11:37
  • 1
    @MrMartin - qml files are basically xml files so my guess is yes, you can create them without QGIS =) – Joseph Apr 05 '16 at 11:48
  • In fact, the method saveNamedStyle works, but only if I'm using the python console inside QGIS desktop. I will edit my question to give the 2 examples (using the python console inside QGIS and running a python program) – Falcoa Apr 05 '16 at 14:08
  • The problem is that when I associate the SLD style outside the qgis desktop, my QgsVectorLayer object does not assume the style, so when I save the qml file, the file is empty because the layer has no style at all.... how can I make the layer object assume the sld style, running the python program outside the qgis desktop? – Falcoa Apr 05 '16 at 14:57
  • 2
    @Falcoa, have you seen this http://gis.stackexchange.com/a/130304/6191? – Alexandre Neto Apr 05 '16 at 20:11
  • @Falcoa - Edited post ;) – Joseph Apr 06 '16 at 09:25
  • 1
    @AlexandreNeto - Thank you for mentioning that post which is what I used to edit my answer ;) – Joseph Apr 06 '16 at 09:25
  • @Joseph - Thank you, I will try this. Pity is that I have to install qgis to use such a simple functionality... is there any way to avoid the installation of all qgis and install just the necessary modules? – Falcoa Apr 06 '16 at 09:37
  • @Falcoa - You may need to ask this as a new question as I am not sure. You can download the QGIS libraries only by using the OSGeo4W Network Installer > Advanced Install then when you get to Select Packages, type "QGIS" and Skip all the Desktop packages while installing the Libs. But this is just a guess =) – Joseph Apr 06 '16 at 09:49