Hi Peter,<div><br></div><div>Thanks for the response. Sorry if I wasn&#39;t clear - there was no real question at this point, I was just following up on my previous question with the resolution of the problem, mostly for the users list, and I copied the message to the developers list as well, just for completeness. </div>

<div><br></div><div>However, the information about when and how Mins and Maxes can be calculated is interesting. I am not sure that computeMinimumMaximumFromLastExtent is what I want - I was using code form a plugin that was adjusting the color scheme for the visible extent, not the whole layer (which is what I want to do). Where does it store the result? This code:</div>
<div><br></div><div>        QgsMapLayerRegistry.instance().addMapLayer(resultsLayer)</div><div>        resultsLayer.triggerRepaint()</div><div>        band = resultsLayer.bandNumber(resultsLayer.grayBandName()) #Should be 1</div>
<div>        minVal = resultsLayer.minimumValue(band)</div><div>        maxVal = resultsLayer.maximumValue(band)</div><div>        QtGui.QMessageBox.information(self, <a href="http://self.tr">self.tr</a>(&quot;MinMax&quot;), <a href="http://self.tr">self.tr</a>(&quot;Min: %s Max %s:&quot; %(minVal, maxVal)))</div>
<div>        resultsLayer.computeMinimumMaximumFromLastExtent(band)</div><div>        minVal = resultsLayer.minimumValue(band)</div><div>        maxVal = resultsLayer.maximumValue(band)</div><div>        QtGui.QMessageBox.information(self, <a href="http://self.tr">self.tr</a>(&quot;MinMax&quot;), <a href="http://self.tr">self.tr</a>(&quot;Min: %s Max %s:&quot; %(minVal, maxVal)))</div>
<div><br></div><div>Produces the same values (0.0 and 255.0) for a raster with values of 0 and 1 before and after calling computeMinimumMaximumFromLastExtent.</div><meta charset="utf-8"><div><br></div><div>Calling this next:</div>
<div>        resultsLayer.setContrastEnhancementAlgorithm(QgsContrastEnhancement.StretchToMinimumMaximum)</div><div><div>        minVal = resultsLayer.minimumValue(band)</div><div>        maxVal = resultsLayer.maximumValue(band)</div>
<div>        QtGui.QMessageBox.information(self, <a href="http://self.tr">self.tr</a>(&quot;MinMax&quot;), <a href="http://self.tr">self.tr</a>(&quot;Min: %s Max %s:&quot; %(minVal, maxVal)))</div></div><div><br></div><div>
stretches the grayscale colormap to 0 and 1, with the layer properties (from the GUI) Min and Max set to 0 and 1, so I assume that it is computing the real values itself, I can&#39;t quite follow the internals. Without this call, the properties window in the GUI shows 0 and 255 for min and max, even after calling computeMinimumMaximumFromLastExtent (I can&#39;t seem to find a way to figure out if this call does anything at all - I can&#39;t find any values or results that indicate any effect, although I am probably looking in the wrong place).</div>
<meta charset="utf-8"><div><br></div><div>Of course, the MessageBox continues to report 0 and 255, so I not sure where the correct information is stored.</div><div><br></div><meta charset="utf-8"><div>The tip on using the PseudoColorShader was exactly what I needed, thank you. I knew it was just something simple I was going at wrong.</div>
<div><br></div><div>Anyway, my plug-in works very nicely now, and I will add it to the QGIS repository here in a few weeks when I hand in my assignment. </div><div><br></div><div>Cheers,</div><div>JP</div><div><br><div class="gmail_quote">
On Tue, Sep 7, 2010 at 3:12 AM, Peter Ersts <span dir="ltr">&lt;<a href="mailto:ersts@amnh.org" target="_blank">ersts@amnh.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


  

<div bgcolor="#ffffff" text="#000000">
Howdy JP,<br>
<br>
Not quite sure what the question is at this stage. However, here are
some general comments. <br>
<br>
When the raster is first loaded, the min max values for each band are
provided by GDAL but are estimates. You will not get the exact min max
values until the band statistics are generated, which depending on the
size of the image can take be slow. The raster has to be rendered at
least once for computeMinimumMaximumFromLastExtent() to provide
meaningful values.<br>
<br>
Re:<div><br>
&quot;On the other hand, <br>
resultsLayer.setColorShadingAlgorithm(&quot;PseudoColorShader&quot;)<br>
Doesn&#39;t seem to do anything at all. Strange.&quot;<br>
</div><div><br>
Pseudo color shading does not just use the minimum and maximum values,
it requires you to define the number of standard deviations to apply.
Also the pseudo color shader has to be used with the correct drawing
style, i.e., QgsRasterLayer.SingleBandPseudoColor<br>
</div>
<br>
<br>
-pete<div><div></div><div><br>
<br>
On 09/05/2010 10:52 AM, JP Glutting wrote:
</div></div><blockquote type="cite"><div><div></div><div>
  <div class="gmail_quote">Ok, I figured this out. It was as simple as:
  <div><br>
  </div>
  <div><b>resultsLayer.setContrastEnhancementAlgorithm(QgsContrastEnhancement.StretchToMinimumMaximum)</b></div>
  <div><br>
  </div>
  <div>That computes the min and max, and stretches the grayscale (to
black and white for boolean rasters).</div>
  <div><br>
  </div>
  <div>In the mean time I was futzing around with:</div>
  <div>
  <div><br>
  </div>
  <div>
  <div>       <b> band =
resultsLayer.bandNumber(resultsLayer.grayBandName()) </b></div>
  </div>
  </div>
  <div>
  <div><b>        minVal = resultsLayer.minimumValue(band)</b></div>
  <div><b>        maxVal = resultsLayer.maximumValue(band)</b></div>
  <div><br>
  </div>
  <div>and</div>
  <div><br>
  </div>
  <div>
  <div>      <b>  resultsLayer.setMinimumValue(band, minVal,
generateLookupTableFlag)</b></div>
  <div><b>        resultsLayer.setMaximumValue(band, maxVal,
generateLookupTableFlag)</b></div>
  </div>
  <div><br>
  </div>
  <div>but it turns out that  <b>resultsLayer.</b><b>setMaximumValue(band)
  </b>returns the theoretical maximum (255) rather than the real
maximum (1), which is not what I wanted. You can get the correct value
with</div>
  <div><br>
  </div>
  <div><b>        maxVal =
resultsLayer.bandStatistics(band).maximumValue</b></div>
  <div><br>
  </div>
  <div>However, it says that calling bandStatistics is very CPU
intensive, since it also calculates a lot of other stuff. As it turns
out, the StretchToMinimumMaximum algorithm seems to calculate the
minmax anyway, so none of that is needed.</div>
  <div><br>
  </div>
  <div>My big mistake, as I was rushing through this, was to confuse
extent and values, as I was trying to copy from a tutorial and wasn&#39;t
paying close attention. Once I figured that out, it was easy.</div>
  <div><br>
  </div>
  <div>On the other hand, </div>
  <div><br>
  </div>
  <div><b>resultsLayer.setColorShadingAlgorithm(&quot;PseudoColorShader&quot;)</b></div>
  <div><br>
  </div>
  <div>Doesn&#39;t seem to do anything at all. Strange.</div>
  <div>
  <br>
  </div>
  <div>Cheers,</div>
  <div>JP</div>
  <br>
  <div class="gmail_quote">
  <div>On Sat, Sep 4, 2010 at 6:35 PM, Benoit de Cabissole <span dir="ltr">&lt;<a href="mailto:benoit@exigesa.com" target="_blank">benoit@exigesa.com</a>&gt;</span>
wrote:<br>
  </div>
  <blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
    <div>
    <div>
    <div>
    <div><font color="#0000ff" face="Arial"><span>Hi JP,</span></font></div>
    <div><font color="#0000ff" face="Arial"><span></span></font> </div>
    <div><font color="#0000ff" face="Arial"><span>I would suggest
asking your question on the Qgis-developer list instead of this one.</span></font></div>
    <div><font color="#0000ff" face="Arial"><span></span></font> </div>
    <div><font color="#0000ff" face="Arial"><span>Below is what I&#39;ve
done to display a custom colormap on a raster. Could it be adapted to
your problem?</span></font></div>
    <div><font color="#0000ff" face="Arial"><span></span></font> </div>
    <div><font color="#0000ff" face="Arial"><span>     <font color="#008000"># Display the raster with the selected colour table:<br>
     #<br>
     # - tell the layer to use a QgsColorRampShader function<br>
    </font>     theLayer.setColorShadingAlgorithm(
QgsRasterLayer.ColorRampShader )<br>
     <font color="#008000"># - get a pointer to the raster shader
function (QgsColorRampShader)<br>
    </font>     myColorRampShader =
theLayer.rasterShader().rasterShaderFunction()<br>
     <font color="#008000"># - set parameters for the
QgsColorRampShader function</font><br>
     myColorRampShader.setColorRampType( QgsColorRampShader.DISCRETE )<br>
     myColorRampShader.setColorRampItemList( theTBL )<br>
     theLayer.setDrawingStyle( QgsRasterLayer.SingleBandPseudoColor )<br>
    <font color="#008000"> # - refresh map &amp; legend<br>
    </font>     if hasattr(theLayer, &quot;setCacheImage&quot;): </span></font></div>
    <div><font color="#0000ff" face="Arial"><span>        
theLayer.setCacheImage( None )<br>
     theLayer.triggerRepaint()<br>
     self.iface.legendInterface().refreshLayerSymbology( theLayer )<br>
    <font color="#008000"> # - tell QGIS that it needs to ask user to
save changes<br>
    </font>     self.iface.mapCanvas().setDirty( True )<br>
    </span></font></div>
    <div><font color="#0000ff" face="Arial"><span>Cheers,</span></font></div>
    <div><font color="#0000ff" face="Arial"><span>Benoit</span></font></div>
    <div>
    <div>
    <div><font color="#0000ff" face="Arial"><span> </span></font></div>
    <blockquote dir="ltr" style="border-left:2px solid rgb(0, 0, 255);padding-left:5px;margin-left:5px;margin-right:0px">
      <div dir="ltr" align="left"><font face="Tahoma" size="2">-----Original
Message-----<br>
      <b>From:</b> <a href="mailto:qgis-user-bounces@lists.osgeo.org" target="_blank">qgis-user-bounces@lists.osgeo.org</a>
[mailto:<a href="mailto:qgis-user-bounces@lists.osgeo.org" target="_blank">qgis-user-bounces@lists.osgeo.org</a>]<b>On
Behalf Of </b>JP Glutting<br>
      <b>Sent:</b> Saturday, 04 September 2010 17:47<br>
      <b>To:</b> <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
      <b>Subject:</b> [Qgis-user] Re: Raster layer display control from
Plugin<br>
      <br>
      </font></div>
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.
      <div><br>
      </div>
      <div>Any ideas?</div>
      <div><br>
      </div>
      <div>Thanks,</div>
      <div>JP<br>
      <br>
      <div class="gmail_quote">On Fri, Sep 3, 2010 at 7:50 AM, JP
Glutting <span dir="ltr">&lt;<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>&gt;</span>
wrote:<br>
      <blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0px 0px 0px 0.8ex;padding-left:1ex">Although
here (<a href="http://blog.qgis.org/node/94" target="_blank">http://blog.qgis.org/node/94</a>) it seems to indicate
that SingleBandPseudoColor is a constant:
        <div><br>
        </div>
        <div><span style="font-size:12px;line-height:20px;font-family:Verdana,Arial,Helvetica,sans-serif;border-collapse:collapse">
        <pre style="border:1px solid rgb(221, 221, 221);padding:0.75em 1.5em;font-size:12px;background-color:rgb(238, 238, 238)">mypLayer-&gt;setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED);
  mypLayer-&gt;setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR);
  std::deque myLayerSet;</pre>
        </span>
        <div>which is what I was thinking in the first place, and here
( <a href="http://doc.qgis.org/stable/classQgsRasterLayer.html#36796f1a303dac9848ba3dce3e5527dc7b7c9814c053986846b579119d2e5be9" target="_blank">http://doc.qgis.org/stable/classQgsRasterLayer.html#36796f1a303dac9848ba3dce3e5527dc7b7c9814c053986846b579119d2e5be9</a> )
DrawingStyle is described as an enumerator, which seems coherent. I am
not sure how to do this from Python.</div>
        <div><br>
        </div>
        <div>Cheers,</div>
        <div>JP</div>
        <div>
        <div><br>
        <div class="gmail_quote">On Fri, Sep 3, 2010 at 7:43 AM, JP
Glutting <span dir="ltr">&lt;<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>&gt;</span>
wrote:<br>
        <blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0px 0px 0px 0.8ex;padding-left:1ex">
          <div>Actually, I am not even sure that first part is the way
to do it. I tried this:</div>
          <div><br>
          </div>
          <div>
          <div>      
 resultsLayer.setDrawingStyle(QtCore.QString(&#39;SingleBandPseudoColor&#39;))</div>
          <div>
          <div>        resultsLayer.setCacheImage(None)</div>
          <div>        resultsLayer.triggerRepaint()</div>
          </div>
          </div>
          <div><br>
          </div>
          <div>(passing the &#39;SingleBandPseudoColor&#39; style as a string)
and it makes the raster invisible. It still shows up black in the
Layers Panel, but it doesn&#39;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:</div>
          <div><br>
          </div>
          <div><span style="font-size:13px;font-family:monospace,fixed">myRasterLayer-&gt;<a style="font-weight:normal;color:rgb(0, 0, 255);text-decoration:none" href="http://classQgsRasterLayer.html#3a923f732bedd87d0b920c5552215434" target="_blank">setDrawingStyle</a>(<a style="font-weight:normal;color:rgb(0, 0, 255);text-decoration:none" href="http://classQgsRasterLayer.html#36796f1a303dac9848ba3dce3e5527dc7b7c9814c053986846b579119d2e5be9" target="_blank">QgsRasterLayer::SingleBandPseudoColor</a>);</span></div>


          <div><br>
          </div>
          <div><span style="font-size:13px;font-family:monospace,fixed"></span>(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):</div>
          <div><br>
          </div>
          <span style="font-size:11px;font-family:monospace,fixed">
          <pre style="border:1px solid rgb(204, 204, 204);margin:4px 8px 4px 2px;padding:4px 6px;font-size:13px;font-family:monospace,fixed;background-color:rgb(245, 245, 245)">00204     <a style="font-weight:normal;color:rgb(0, 0, 255);text-decoration:none" href="http://classQgsRasterLayer.html" target="_blank">QgsRasterLayer</a>( <span style="color:rgb(96, 64, 32)">int</span> dummy,
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00205"></a>00205                     <span style="color:rgb(0, 128, 0)">const</span> QString &amp; baseName = QString(),
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00206"></a>00206                     <span style="color:rgb(0, 128, 0)">const</span> QString &amp; path = QString(),
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00207"></a>00207                     <span style="color:rgb(0, 128, 0)">const</span> QString &amp; providerLib = QString(),
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00208"></a>00208                     <span style="color:rgb(0, 128, 0)">const</span> QStringList &amp; layers = QStringList(),
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00209"></a>00209                     <span style="color:rgb(0, 128, 0)">const</span> QStringList &amp; styles = QStringList(),
<a style="color:rgb(26, 65, 168)" name="12aeb6f4c6ecc467_12ae9c0e4dc49526_12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00210"></a>00210                     <span style="color:rgb(0, 128, 0)">const</span> QString &amp; format = QString(), </pre>


          </span>
          <div><span style="font-size:13px;font-family:monospace,fixed">00211 <span style="color:rgb(0, 128, 0)">const</span> QString &amp; crs =
QString() );</span></div>
          <div><br>
          </div>
          <div><span style="font-size:13px;font-family:monospace,fixed"></span>Thanks,</div>
          <div>JP </div>
          <div>
          <div><br>
          <div class="gmail_quote">On Fri, Sep 3, 2010 at 3:04 AM, JP
Glutting <span dir="ltr">&lt;<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>&gt;</span>
wrote:<br>
          <blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0px 0px 0px 0.8ex;padding-left:1ex">Hello,
            <div><br>
            </div>
            <div>I am working on a plugin (I mentioned it on the list
earlier, but it isn&#39;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:</div>
            <div><br>
            </div>
            <div>
            <div>        resultsLayer =
qgis.core.QgsRasterLayer(self.query.results_file,
QtCore.QFileInfo(self.query.results_file).baseName())</div>
            <div>      
 qgis.core.QgsMapLayerRegistry.instance().addMapLayer(resultsLayer)</div>
            </div>
            <div><br>
            </div>
            <div><br>
            </div>
            <div>which works fine for opening the file, but I would
like to fine-tune the display so the user doesn&#39;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.</div>
            <div><br>
            </div>
            <div>I tried the psuedocolor with this code:</div>
            <div><br>
            </div>
            <div>
            <div>      
 resultsLayer.setDrawingStyle(qgis.core.QgsRasterLayer.SingleBandPseudoColor)</div>
            <div>        resultsLayer.setCacheImage(None)</div>
            <div>        resultsLayer.triggerRepaint()</div>
            </div>
            <div><br>
            </div>
            <div>which doesn&#39;t seem to do anything at all, and I am
just guessing, really.</div>
            <div><br>
            </div>
            <div>I found a nice tutorial about how to calculate the min
and max extent of a raster and adjust the display here:</div>
            <div><br>
            </div>
            <div><a href="http://linfiniti.com/2010/08/a-simple-qgis-python-tutorial/" target="_blank">http://linfiniti.com/2010/08/a-simple-qgis-python-tutorial/</a></div>
            <div><br>
            </div>
            <div>and I tried the following code:</div>
            <div><br>
            </div>
            <div>
            <div>        band =
resultsLayer.bandNumber(resultsLayer.grayBandName())</div>
            <div>        extentMin = 0.0</div>
            <div>        extentMax = 0.0</div>
            <div>        generateLookupTableFlag = False</div>
            <div>        extentMin, extentMax =
resultsLayer.computeMinimumMaximumFromLastExtent(band)</div>
            <div>        resultsLayer.setMinimumValue(band, extentMin,
generateLookupTableFlag)</div>
            <div>        resultsLayer.setMaximumValue(band, extentMax,
generateLookupTableFlag)</div>
            <div>        resultsLayer.setStandardDeviations(0.0)</div>
            <div>        resultsLayer.setUserDefinedGrayMinimumMaximum(
True )</div>
            <div>        resultsLayer.setCacheImage(None)</div>
            <div>        resultsLayer.triggerRepaint()</div>
            </div>
            <div><br>
            </div>
            <div>but that fails with the following error:</div>
            <div><br>
            </div>
            <div>
            <p style="margin:0px;text-indent:0px">Traceback (most
recent call last):<br>
  File &quot;/Users//.qgis/python/plugins/mcelite/MCELiteDialog.py&quot;, line
361, in accept<br>
    extentMin, extentMax =
resultsLayer.computeMinimumMaximumFromLastExtent(band)<br>
TypeError: &#39;float&#39; object is not iterable<br>
            </p>
            <p style="margin:0px;text-indent:0px"><br>
            </p>
            <p style="margin:0px;text-indent:0px">and I don&#39;t
understand what the float object is, exactly.</p>
            <p style="margin:0px;text-indent:0px"><br>
            </p>
            <p style="margin:0px;text-indent:0px">Any help or
suggestions much appreciated.</p>
            <p style="margin:0px;text-indent:0px"><br>
            </p>
            <p style="margin:0px;text-indent:0px">Cheers,</p>
            <p style="margin:0px;text-indent:0px">JP</p>
            </div>
          </blockquote>
          </div>
          <br>
          </div>
          </div>
        </blockquote>
        </div>
        <br>
        </div>
        </div>
        </div>
      </blockquote>
      </div>
      <br>
      </div>
    </blockquote>
    </div>
    </div>
    </div>
    <br>
    </div>
    </div>
_______________________________________________<br>
Qgis-user mailing list
    <div><br>
    <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
    </div>
    <div><a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
    <br>
    </div>
  </blockquote>
  </div>
  <br>
  </div>
  </div>
  <br>
  </div></div><pre><fieldset></fieldset>
_______________________________________________
Qgis-developer mailing list
<a href="mailto:Qgis-developer@lists.osgeo.org" target="_blank">Qgis-developer@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
  </pre>
</blockquote>
<br>
<br>
<pre cols="72">-- 
====================================
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: <a href="http://biodiversityinformatics.amnh.org" target="_blank">http://biodiversityinformatics.amnh.org</a>
Web: <a href="http://cbc.amnh.org" target="_blank">http://cbc.amnh.org</a>

Open Source,
...evolving through community cooperation to change the world bit by bit

Quantum GIS Raster Development Team. 
</pre>
</div>

<br>_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org" target="_blank">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br></blockquote></div><br></div>