Tuesday, 24 March 2015

QGIS: How to load feature (.qml) styles from python?




Possible Duplicate:
How to style a layer from console?



I'm trying to set the styles for a vector layer by loading the .qml file in the python console. The qml-style file contains all categories and symbols needed, but I can not find the right function to load the qml file. The QgsCategorizedSymbolRendererV2::create(domElement) seems to be not available in the python console (QGIS 1.8.0). Is myRenderer.load(doc.documentElement()) the right function to use?


Thank you!



Stefan


#load the xml
xmlfile = QtCore.QFile("qgis-layer-style.qml")
doc = QtXml.QDomDocument( "Categories" )
if xmlfile.open(QtCore.QIODevice.ReadWrite | QtCore.QIODevice.Text) == 0:
print "cannot open file"
if doc.setContent( xmlfile ) == 0:
print "no xml content"

# creating a new renderer

myRenderer = QgsCategorizedSymbolRendererV2('',[])
myRenderer.load(doc.documentElement())
# here myRenderer has still no categories!

# assign myRenderer to the active layer
layer = qgis.utils.iface.activeLayer()
layer.setRendererV2(myRenderer)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...