[GIS] Setting default raster layer style in QGIS

qgisqgis-2.12qgis-2.16rasterstyle

I am trying to set up a QGIS project with a default raster layer style so that I can add additional raster layers (which all have the exact same format but with different names) and they will automatically render with my defined default raster layer style.

I am not sure if this is possible, but I would like to avoid having to manually apply the defined style to each layer individually (I do not want to have all additional layers loaded in to QGIS simultaneously, but be able to add a layer, inspect it, remove it, add another, and so on). It seems it should be possible, otherwise the "Save as default" option would not exist, assuming I am interpreting this correctly. However I am unsure how to get it to work.

I have loaded a raster which includes all possible values and have styled this as I would like. I have saved this style (general file name) and saved the style as default (same file name as layer), I have also tried saving the a layer definition file based on this layer (both with a general name and with the same name as the layer). However, when I load in additional rasters, they are not styled by my defined default style (i.e. they simply appear as black and white).

I am only to get additional rasters styled automatically when I load them into QGIS if I have already copied the default raster style file (.qml) to a new file with the same name as the additional raster to be loaded. This is quite tedious to do, and not straightforward for other team members who also need to use this project, so this is far from an ideal solution.

Should I be able to do what I want?

If yes, how can I get this work? Do I need to be specific about the file names and locations of the default style, the layer this style is based off, and the additional layers to be added? Do they all need to be in the same folder (incredibly messy as I have a LOT of additional raster layers)?

I am using QGIS 2.16.3 Desktop with GRASS 7.0.4 running on Windows 7 64-bit, but have received the same behaviour with QGIS Desktop 2.12.1 with GRASS 6.4.3.

Note the raster style I wish to apply is interpolation style discrete; my default raster has a set of defined values (e.g. 2, 5, 10, 100) and I have assigned a different colour (by setting the RGB) to each value.

Best Answer

I would use a bit of python to create a function whereby an added raster will automatically have a specific style applied. You can use the following code in the Python Console (which you can access from the menubar: Plugins > Python Console). Just change the path of your .qml style:

QGIS 3:

def apply_rasterStyle(layers):
    for layer in layers:
        if layer.type() == 1:
            layer.loadNamedStyle('path/to/qml')
        qgis.utils.iface.layerTreeView().refreshLayerSymbology(layer.id())

QgsProject.instance().layersAdded.connect(apply_rasterStyle)

QGIS 2:

def apply_rasterStyle(layers):
    for layer in layers:
        if layer.type() == 1:
            layer.loadNamedStyle('path/to/qml')
        qgis.utils.iface.legendInterface().refreshLayerSymbology(layer)

QgsMapLayerRegistry.instance().layersAdded.connect(apply_rasterStyle)
Related Question