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

Peter Ersts ersts at amnh.org
Thu Aug 5 09:32:59 EDT 2010


On 08/05/2010 05:25 AM, Benoit de Cabissole wrote:
> Could you please point me to the function(s) that are used to match a custom
> colour table (QgsColorRampShader?) to a raster?
>
>    

Howdy Benoit,

First you will need to set the color shading algorithm for the raster,
 >> YourRasterLayer.setColorShadingAlgorithm( "ColorRampShader" )
This creates a new shader for the raster.

Next you will need to build your color ramp (palette). This is just a 
list of items of type QgsColorRampShader.ColorRampItem, Each item has 
three variables, lable (QString), color (QColor) , and value (double)

Once you have your list you will need to give that list to the shader.
 >> YourRasterLayer.rasterShader().setColorRampItemList( YourColorRamp )

Then you need to set the color ramp type:
 >> YourRasterLayer.rasterShader().setColorRampType( TheType )

Where TheType is, INTERPOLATED, DISCRETE or EXACT. If your color ramp 
only had three entries:
Label1, Red, 0
Label2, Yellow, 127
Label3, Green, 255

exact - looks for an exact match at the time of shading, a value of 90 
would not be shaded.

discrete - is a bin type approach, images pixels with values of 50, 70, 
or 90 would all be assigned the same color

interpolated - is as the name says, 90 will be an interpolated color 
between Red and Yellow. In this case, more yellow than red as 90 is 
closer to 127 than 0.


Hope that helps.
-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