[Qgis-user] Re: Raster layer display control from Plugin

Benoit de Cabissole benoit at exigesa.com
Sat Sep 4 09:35:53 PDT 2010


Hi JP,

I would suggest asking your question on the Qgis-developer list instead of
this one.

Below is what I've done to display a custom colormap on a raster. Could it
be adapted to your problem?

     # Display the raster with 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
     if hasattr(theLayer, "setCacheImage"):
         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 )

Cheers,
Benoit

  -----Original Message-----
  From: qgis-user-bounces at lists.osgeo.org
[mailto:qgis-user-bounces at lists.osgeo.org]On Behalf Of JP Glutting
  Sent: Saturday, 04 September 2010 17:47
  To: Qgis-user at lists.osgeo.org
  Subject: [Qgis-user] Re: Raster layer display control from Plugin


  No takers? No hints? I have been looking all over the place, and I am
stuck. If it is something absurdly simple, just point me in the right
direction.


  Any ideas?


  Thanks,
  JP


  On Fri, Sep 3, 2010 at 7:50 AM, JP Glutting <jpglutting at gmail.com> wrote:

    Although here (http://blog.qgis.org/node/94) it seems to indicate that
SingleBandPseudoColor is a constant:


mypLayer->setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED);
  mypLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR);
  std::deque myLayerSet;which is what I was thinking in the first place, and
here (
http://doc.qgis.org/stable/classQgsRasterLayer.html#36796f1a303dac9848ba3dce
3e5527dc7b7c9814c053986846b579119d2e5be9 ) DrawingStyle is described as an
enumerator, which seems coherent. I am not sure how to do this from Python.


    Cheers,
    JP


    On Fri, Sep 3, 2010 at 7:43 AM, JP Glutting <jpglutting at gmail.com>
wrote:

      Actually, I am not even sure that first part is the way to do it. I
tried this:


              resultsLayer.setDrawingStyle(QtCore.QString('SingleBandPseudoC
olor'))
              resultsLayer.setCacheImage(None)
              resultsLayer.triggerRepaint()


      (passing the 'SingleBandPseudoColor' style as a string) and it makes
the raster invisible. It still shows up black in the Layers Panel, but it
doesn't show in the main window until you change the properties manually
(and it is Grayscale when you do). It feels like I am pretty close, but I am
not sure how to interpret this code from the QGIS documentation:


      myRasterLayer->setDrawingStyle(QgsRasterLayer::SingleBandPseudoColor);


      (I never learned more than the basics of C++, and that was a long time
ago). The source code seems to indicate that the format needs to be passed
as a string (of course, when the layer is generated):


00204     QgsRasterLayer( int dummy,
00205                     const QString & baseName = QString(),
00206                     const QString & path = QString(),
00207                     const QString & providerLib = QString(),
00208                     const QStringList & layers = QStringList(),
00209                     const QStringList & styles = QStringList(),
00210                     const QString & format = QString(), 00211 const
QString & crs = QString() );


      Thanks,
      JP


      On Fri, Sep 3, 2010 at 3:04 AM, JP Glutting <jpglutting at gmail.com>
wrote:

        Hello,


        I am working on a plugin (I mentioned it on the list earlier, but it
isn't relevant to the question I have now). I have the results written to a
raster file, and I need to display it. I am using this code:


                resultsLayer =
qgis.core.QgsRasterLayer(self.query.results_file,
QtCore.QFileInfo(self.query.results_file).baseName())
                qgis.core.QgsMapLayerRegistry.instance().addMapLayer(results
Layer)




        which works fine for opening the file, but I would like to fine-tune
the display so the user doesn't have to reset the properties (in my test
exaple the values are 0 and 1 and the display is essentially all black). I
would like to either display the results in pseudocolor directly, or in
grayscale with the scale stretched to the min and max extent of the raster.


        I tried the psuedocolor with this code:


                resultsLayer.setDrawingStyle(qgis.core.QgsRasterLayer.Single
BandPseudoColor)
                resultsLayer.setCacheImage(None)
                resultsLayer.triggerRepaint()


        which doesn't seem to do anything at all, and I am just guessing,
really.


        I found a nice tutorial about how to calculate the min and max
extent of a raster and adjust the display here:


        http://linfiniti.com/2010/08/a-simple-qgis-python-tutorial/


        and I tried the following code:


                band = resultsLayer.bandNumber(resultsLayer.grayBandName())
                extentMin = 0.0
                extentMax = 0.0
                generateLookupTableFlag = False
                extentMin, extentMax =
resultsLayer.computeMinimumMaximumFromLastExtent(band)
                resultsLayer.setMinimumValue(band, extentMin,
generateLookupTableFlag)
                resultsLayer.setMaximumValue(band, extentMax,
generateLookupTableFlag)
                resultsLayer.setStandardDeviations(0.0)
                resultsLayer.setUserDefinedGrayMinimumMaximum( True )
                resultsLayer.setCacheImage(None)
                resultsLayer.triggerRepaint()


        but that fails with the following error:


        Traceback (most recent call last):
          File "/Users//.qgis/python/plugins/mcelite/MCELiteDialog.py", line
361, in accept
            extentMin, extentMax =
resultsLayer.computeMinimumMaximumFromLastExtent(band)
        TypeError: 'float' object is not iterable





        and I don't understand what the float object is, exactly.




        Any help or suggestions much appreciated.




        Cheers,

        JP






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20100904/afe75283/attachment.html>


More information about the Qgis-user mailing list