[Qgis-developer] How to load a colour table on a raster, programmatically (in python)?

Benoit de Cabissole benoit at exigesa.com
Fri Aug 6 08:43:48 EDT 2010


Hi Pete,

Thanks for the clarifications, very helpful. But I still need your expertise
;-)

I've tried many combinations around the following code:

118    # Now we can show the main window: user creates a colour table in it.
119    dialog =
cdlgTBL(self.iface,theLayer,FilePath,FilenameIn,str(lDum),wGRD,hGRD,self.GRD
)
120    dialog.show()
121    dialog.exec_()
122    #
123    # Get back the selected colour table to display
124    theTBL = dialog.theTBL
125    nColo = dialog.nColo     # number of colours used in the colour table
126    if not theTBL:
127        return
128    # Display the selected colour table
129    theLayer.rasterShader().setRasterShaderFunction(
QgsColorRampShader() )
130    theLayer.setRasterShaderFunction( QgsColorRampShader() )
131    myColorRampShader = QgsColorRampShader()
132    myColorRampShader.setColorRampItemList( theTBL )
133    myColorRampShader.setMaximumColorCacheSize( nColo )
134    myColorRampShader.setColorRampType( myColorRampShader.DISCRETE )
135    theLayer.rasterShader().setRasterShaderFunction( myColorRampShader )
136    theLayer.setRasterShaderFunction( myColorRampShader )

It excecutes without any error, but:
- The raster does not change colours.
- If, before launching the plugin, I leave the raster properties to their
default, e.g. greyscale, nothing happens after the plugin closes and work
can carry on with no error in QGIS.
- If, before launching the plugin, I set the 'Color ramp' to 'colormap' in
the raster properties dialog, nothing happens either after closing the
plugin but as soon as I want to open the raster properties dialog QGIS bombs
out!

So, I suppose the code does something but it is ignored or harmful.
Any idea why?

I've also tried to disable lines 129, 130, 133 & 136 in all combinations.
The results are the same as described above.
I've also put lines 129-136 into the dialog class, same results.

As you've alluded in your replies, is it possible that we cannot set the
colormap with Python?
Has someone out there done it successfully?

Cheers,
Benoit



More information about the Qgis-developer mailing list