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.

1 Answer 1

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

9
  • do you know if it is possible to create a qml file without installing qgis?
    – ePascoal
    Commented Apr 5, 2016 at 11:37
  • 1
    @MrMartin - qml files are basically xml files so my guess is yes, you can create them without QGIS =)
    – Joseph
    Commented Apr 5, 2016 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
    Commented Apr 5, 2016 at 14:08
  • 2
    @Falcoa, have you seen this gis.stackexchange.com/a/130304/6191? Commented Apr 5, 2016 at 20:11
  • 1
    @AlexandreNeto - Thank you for mentioning that post which is what I used to edit my answer ;)
    – Joseph
    Commented Apr 6, 2016 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.