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

Peter Ersts ersts at amnh.org
Mon Aug 9 15:57:02 EDT 2010


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.



More information about the Qgis-developer mailing list