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

Benoit de Cabissole benoit at exigesa.com
Mon Aug 9 17:01:27 EDT 2010


Hi Pete,

You're a star!!

Here is the code I'm now using:

>> theLayer.setColorShadingAlgorithm( QgsRasterLayer.ColorRampShader )
>> myColorRampShader = theLayer.rasterShader().rasterShaderFunction()
>> myColorRampShader.setColorRampType( QgsColorRampShader.DISCRETE )
>> myColorRampShader.setColorRampItemList( theTBL )
>> theLayer.setDrawingStyle( QgsRasterLayer.SingleBandPseudoColor )
>> theLayer.triggerRepaint()

It works (well, sort of)...

Colormap is given to the raster (and into the coloramp tab in the raster
properties). The colour ramp attribute is also reflected in the symbology
tab: color map combobox set to colormap. So the raster is given the correct
info. So far so good. But, the legend does not refresh to show the change to
colormap: the raster icon and raster colour bar stay grey.
The main raster (the only layer in my test) does not refresh either in the
QGIS map canvas window. Only if I move it will it refresh and display the
colour map.
The only way to refresh the legend to the correct info seems to open the
raster properties box and click ok. (I've tried the QgsLegendInterface, but
I have problems to make it work!)

>From my test it thus seems the "theLayer.triggerRepaint()" does not do the
expected job. Can you confirm? Or is there another function to refresh the
legend and main window?
Do I have to implement a signal/slot relation to refresh the map canvas, or
will triggerRepaint() do the job alone?

BTW, I took the four first lines of the code above from the new cookbook.

Many thanks again for your help, I appreciate it very much.
Benoit

> -----Original Message-----
> From: qgis-developer-bounces at lists.osgeo.org
> [mailto:qgis-developer-bounces at lists.osgeo.org]On Behalf Of Peter Ersts
> Sent: Monday, 09 August 2010 21:57
> To: qgis-developer at lists.osgeo.org
> Subject: Re: [Qgis-developer] How to load a colour table on a
> raster,programmatically (in python)?
>
>
> On 08/09/2010 12:22 PM, Benoit de Cabissole wrote:
> > Here is the code I'm using:
> >
> >     # tell the layer to use a QgsColorRampShader function
> >     theLayer.setColorShadingAlgorithm( QgsRasterLayer.ColorRampShader )
> >     theLayer.rasterShader().setRasterShaderFunction(
> QgsColorRampShader() )
> >
> >     # get the raster shader function: should be a QgsColorRampShader
> >     myColorRampShader = theLayer.rasterShader().rasterShaderFunction()
> >
> >     # set parameter for the QgsColorRampShader function
> >     myColorRampShader.setColorRampItemList( theTBL )
> >     myColorRampShader.setMaximumColorCacheSize( nColo )
> >     myColorRampShader.setColorRampType( myColorRampShader.DISCRETE )
> >
> >     # feed my custom QgsColorRampShader back to the raster
> >     theLayer.setRasterShaderFunction( myColorRampShader )
> >
> > It does not produce an error but raster does not change to the given
> > colormap.
>
> Benoit,
>
> With Martin's recent updates you can remove a couple of lines,
>
> ---theLayer.rasterShader().setRasterShaderFunction( QgsColorRampShader()
> ) can be removed, setColorShadingAlgorithm() already creates a new
> shader instance and theLayer.rasterShader().rasterShaderFunction() new
> returns the actual class not the base class, thanks Martin!
>
>
> ---theLayer.setRasterShaderFunction( myColorRampShader ) is also not
> necessary, because you are already working with the actual shader (i.e.,
> myColorRampShader ). This will also cause a crash/segfault because the
> setRasterShaderFunction() now is deleting the existing function and
> replacing it with the new, however, in this case it is deleting itself.
> MARTIN: This was why QgsRasterLayer managed deleting the functions and
> checked to see if the QgsRasterShaderFunction was user defined. I saw
> too many possibilities for crashes if the shader itself managed the
> deleting of functions. This case illustrates on instance of that problem.
>
>
> I suspect you are not seeing your changes because the draying style is
> not set correctly. For example, if you load a gray scale image and then
> apply a color map using the above code, you will still have to set the
> drawing style to a type that will use the shader as apposed to a
> contrast enhancement.
>  >>theLayer.setDrawingStyle( QgsRasterLayer.SingleBandPseudoColor )
>  >>theLayer.triggerRepaint()
>
> The same would apply for multi band images:
>  >>theLayer.setDrawingStyle(
> QgsRasterLayer.MultiBandSingleBandPseudoColor )
>
>
> If it would help I could give you a complete example with sample data,
> but it would have to wait until the end of the week.
>
> -pete
>
> --
> ====================================
> Peter J. Ersts, Software Developer
> American Museum of Natural History
> Center for Biodiversity and Conservation
> Central Park West at 79th Street
> New York, New York 10024
> Web: http://biodiversityinformatics.amnh.org
> Web: http://cbc.amnh.org
>
> Open Source,
> ...evolving through community cooperation to change the world bit by bit
>
> Quantum GIS Raster Development Team.
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the Qgis-developer mailing list