1

I am trying to make a QGIS plugin to change which band in a multi-band data set (netCDF where the band represents time steps) is used for a gray scale rendering. The file works fine with manual changes, but I cannot find how to change it programatically. I have set up a dialog box where I can select which layer to work on:

for layer in self.iface.legendInterface().layers():
        # iface.messageBar().pushMessage("Type",layer.name(), level=1, duration=10)
        if type(layer) is QgsRasterLayer and layer.bandCount()>1:
            self.layerlist.append(layer)
            self.dlg.comboBoxLayer.addItem(layer.name())

my QGIS related imports are (as far as I can see)

from qgis.core import *
from qgis.utils import *

I have found the attribute setGrayBandName that should select which band to use for presentation, but when I run (knowing 0<=sldValue <=number of bands)

self.dlg.labelStartTime.setText(str(sldValue))
self.activeLayer.setGrayBandName(self.activeLayer.bandName(sldValue))
self.activeLayer.triggerRepaint()

it crashes with "AttributeError: 'QgsRasterLayer' object has no attribute 'setGrayBandName'" - if setGrayBandName does not exist, what should I use then? And why do I find a number of references to it, but none saying that is has been taken out of use? - or have I just misunderstood something? Do I need to import som more modules?

seems like I misunderstood something, I need to set a new gray band on the renderer, i.e.

self.activeLayer.renderer.setGrayBand(sldValue)

But, this only works if I start in the console by selecting the layer and then doing

layer=qgis.utils.iface.activeLayer()
rndr=layer.renderer()
layer.renderer=rndr

Then I can start playing around. If I try to do the rndr=layer.renderer() in the code in the plugin, I am told something like that type of object is not callable...

The present version of the module is available at https://github.com/sickel/TimeStepSelector

  • Morten, Apologies. I understand that you could do this from the console. I managed to do this as well, but have not tried from a script. I cannot even manage to change the band in a multiband raster using the colourmap plot (such as pseudocolor). Even if I try your trick, the console crashes. I have tried to create a new shader and submit it to the raster renderer, but that just crashes as well. What is worse is that there is not even a name for the band layer in the pseudocolourRenderer! It used to be that the band had a GrayBandName method, but even this is gone. Alas, when it comes to doing – Aaron Davis Apr 24 '14 at 13:43
1

I have been having the exact same problem ever since QGis left the 1.8 format. It seems like calls to set the active band from any multiband layer have been dropped. I have been asking about this for the pseudocolour plots for some time now, but no one seems to have an answer! Have you actually been able to set any layer as active and redraw that layer, even if from the console?

  • Aaron - it works in the console - by selecting the layer, getting the renderer from the layer and then setting the renderer again (see the last part of my original post) - but so far not in a script (I have not looked into this for a while now, I have had other things to do, but I would love to manage to do this in a script) – MortenSickel Apr 23 '14 at 10:53

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.