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

Benoit de Cabissole benoit at exigesa.com
Wed Aug 11 03:26:12 EDT 2010


Martin, Peter, Duarte, Borys,

Success!

Below the working code:

=======================================================================
  #
  # Display the selected colour table:
  #
  # - tell the layer to use a QgsColorRampShader function
  theLayer.setColorShadingAlgorithm( QgsRasterLayer.ColorRampShader )
  #
  # - get a pointer to the raster shader function (QgsColorRampShader)
  myColorRampShader = theLayer.rasterShader().rasterShaderFunction()
  #
  # - set parameters for the QgsColorRampShader function
  myColorRampShader.setColorRampType( QgsColorRampShader.DISCRETE )
  myColorRampShader.setColorRampItemList( theTBL )
  theLayer.setDrawingStyle( QgsRasterLayer.SingleBandPseudoColor )
  #
  # - refresh map & legend
  theLayer.setCacheImage( None )
  theLayer.triggerRepaint()
  self.iface.legendInterface().refreshLayerSymbology( theLayer )
  #
  # - tell QGIS that it needs to ask user to save changes
  self.iface.mapCanvas().setDirty( True )
=======================================================================

Something is still bothering me: what does happen if the raster is cached
before I set the cache to None?
I've tried the following code:

  # - backup current cache state
  bQI = theLayer.cacheImage()
  if bQI != None: theLayer.setCacheImage( None )

  ... refresh raster ...

  # - restore the state of image cache
  theLayer.setCacheImage( bQI )

It runs with no error, but on exit of QGIS it bombs out!

Any idea? It is correct to try to backup/restore the cache state?

Thanks a lot for helping me on this matter for the last few weeks...
Benoit


> -----Original Message-----
> From: qgis-developer-bounces at lists.osgeo.org
> [mailto:qgis-developer-bounces at lists.osgeo.org]On Behalf Of Benoit EXIGE
> Sent: Tuesday, 10 August 2010 22:03
> To: Martin Dobias
> Cc: qgis-developer at lists.osgeo.org
> Subject: Re: [Qgis-developer] How to load a colour table on a
> raster,programmatically (in python)?
>
>
> Martin,
>
> Thanks for the answers. I will test them tomorrow.
>
> As for QgsLegendInterface the problem was that I did not know how to
> use it correctly!
>
> Cheers,
> Benoit
>
>
> On 10 Aug 2010, at 21:52, Martin Dobias <wonder.sk at gmail.com> wrote:
>
> > On Mon, Aug 9, 2010 at 11:01 PM, Benoit de Cabissole
> <benoit at exigesa.com
> > > wrote:
> >> 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.
> >
> > Use QgsLegendInterface class to refresh the symbology:
> >
> > iface.legendInterface().refreshLayerSymbology(layer)
> >
> >
> >> 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.
> >
> > Possibly you're using caching. Make sure to clear the cached image
> > before calling triggerRepaint():
> >
> > layer.setCacheImage(None)
> >
> >> 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!)
> >
> > What probles with QgsLegendInterface did you have?
> >
> >
> > Regards
> > Martin
> _______________________________________________
> 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