I have two lists of raster (digital elevation model - DEM) layers by name. The first list of layers should be symbolized as follows: symbology should be set to hillshade layer rendering, blend mode should be set to soft light

enter image description here

and transparency, global opacity should be set to 30%

enter image description here

The second list of layers should be symbolized as follows: symbology should be set to singleband pseudocolor mode should be set to equal interval and classes should be set to 5

enter image description here

Is there a way to apply all of these settings with PyQGIS code, since I need to set them manually every single time? I tried to do it myself but this task far surpasses my level of knowledge in PyQGIS.

  • 1
    Is this a one time job, or a recurring task? If this is a recurring task, does it concern new layers, or the same layers and new projects?
    – Erik
    Jan 4 at 11:00
  • The workflow is set up that for every single project these steps are the same. There is always at least one layer per list. The layers differ project by project, but the settings stay the same. After applying the aforementioned settings, the tweaks need to be applied manually. I hope I am explaining my situation so it can be understood. Jan 4 at 11:12
  • 1
    You could create .qml files containing the correct style before loading the raster data. You may even set a .qml as default style for your raster layers in your options, though this will only fix half of the problem.
    – Erik
    Jan 4 at 11:22
  • I considered that but was wondering if there is a simpler solution with PyQGIS, to skip all the clicks if possible. Jan 4 at 11:25
  • If there's a .qml with the same name as your raster saved to the same location as your raster, the raster will be loaded with the style saved to the .qml. Or you could load/set the style for one raster, then right click it, choose copy style, then mark all other rasters which should look the same and paste the style.
    – Erik
    Jan 4 at 11:34

I have managed to partially solve the aforementioned set of problems. The code for the first list of rasters (sets all the parameters properly):

`for i in list_1 :
    layer_1 = QgsProject.instance().mapLayersByName(i)[0]
    r = QgsHillshadeRenderer(layer_1.dataProvider(), 1, 315, 45)

The code for the second list of rasters (only part of the solution):

`for i in list_2 :
    layer_2 = QgsProject.instance().mapLayersByName(i)[0]
    sbps = QgsSingleBandPseudoColorRenderer(layer_2.dataProvider(),1,)

I just don't know how to set Mode to Equal Interval and Classes to 5. I would be extremely grateful if someone would offer a solution to this part of my problem.

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.