13

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

3
  • 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
    Commented Aug 28, 2018 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
    Commented Aug 28, 2018 at 12:08
  • There is a like question here wanting to use python but no answer yet, gis.stackexchange.com/questions/294016/…
    – artwork21
    Commented Aug 28, 2018 at 12:49

3 Answers 3

6
+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)

2
  • 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
    Commented Sep 4, 2018 at 12:52
  • 1
    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
    Commented Sep 7, 2018 at 16:36
1

In case someone comes across this from post - 2023 and using 3.28.2 Firenze and above, the QGIS Dev guru's have implemented a Save Mulitple Stlyes to QML. Feature Request description on Github

Save Multiple Styles

An Example process:

  1. Right Click> Add Style: rename to something sensible
  2. Modify your symbology (this auto-saves)
  3. Repeat for all styles
  4. Once happy with all your stlyes. Open Symbology from your layer. Click 'Style'>Save Multiple Styles
  5. In your next project, Load Styles> Navigate to your .qml file.
  6. Open and Enjoy.
2
  • When exporting, this functionality creates different qml files for each style present in the layer. However, to import, I didn't find a way to import all the styles at once. Commented Mar 24 at 18:49
  • I've had a look and can't find how to load mulitples. I wonder if you save it to a database you can load mulitples in one go.
    – Nick_Jo
    Commented Apr 17 at 5:33
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.

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

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.