9

I have a set of qml style files saved that I apply to similar layers across multiple projects. An example would be a linetype shapefile layer with about a dozen different styles; currently I have to repeat the process of adding a new style and loading the qml file for each of the different styles. Then I have to do it a few more times in that project and repeat the whole thing again in the next project.

Just trying to see if there's a way to load multiple style files on the same layer at once? Something like instead of browsing to a single qml, I could multi-select several qml files and load them at once?

Example of multiple styles per layer

  • 1
    What do you mean by loading multiple styles at once on the same layer? Wouldn't the last style loaded override all previous loaded styles? Or are you wanting to load many styles to multiple copies of the same layer? – artwork21 Aug 28 '18 at 11:56
  • My current workflow is to open the layer properties; use the style button (bottom left) to add a new style, assign a name, and then load a saved .qml file, then repeat for however many (10-20) styles that layer uses. Currently, I have to do this one-by-one, for about 7-8 layers, in >25 projects. (repetitive data, layers & maps, but project files separated geographically by municipalities - each municipality is a separate client) – CEL-ma Aug 28 '18 at 12:08
  • There is a like question here wanting to use python but no answer yet, gis.stackexchange.com/questions/294016/… – artwork21 Aug 28 '18 at 12:49
2
+50

You can load multiple styles using pyqgis script (explanations in comments):

import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)

You can run it in the QGIS python console or adapt to a processing script.

(Tested on current LTR version QGIS 2.18)

  • 1
    Thank you very much! I was able to successfully load multiple styles - I did get an "[Errno 9] Bad file descriptor" error message while it was running the algorithm, but it did still complete successfully! This will save me A LOT of work! – CEL-ma Sep 4 '18 at 12:52
  • To anyone else reading this post in the future; the above-mentioned "Bad file" error message was due to the qml path having both line and node vector layers. Once separated, no more error. – CEL-ma Sep 7 '18 at 16:36
  • Anyone able to help to convert that code to python 3 so it works in QGIS 3? – CEL-ma Jan 30 at 15:38
0

If I am understaning you correctly you just want to be able to apply geometry styles and not necessarily rules or expressions to layers quickly and easily in any project you have open?

If that is the case you could just save your styles in the symbol library using the style manager? Open up the layer style panel and click the "open library" button.

  • sorry, yes - the styles I would like to batch load are rule-based. – CEL-ma Aug 28 '18 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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