<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 class="im"><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'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("PseudoColorShader")</b></div><div><br></div><div>Doesn't seem to do anything at all. Strange.</div>
<div>
<br></div><div>Cheers,</div><div>JP</div><br><div class="gmail_quote"><div class="im">On Sat, Sep 4, 2010 at 6:35 PM, Benoit de Cabissole <span dir="ltr"><<a href="mailto:benoit@exigesa.com" target="_blank">benoit@exigesa.com</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">
<div>
<div><font face="Arial" color="#0000ff"><span>Hi
JP,</span></font></div>
<div><font face="Arial" color="#0000ff"><span></span></font> </div>
<div><font face="Arial" color="#0000ff"><span>I would
suggest asking your question on the Qgis-developer list instead of this
one.</span></font></div>
<div><font face="Arial" color="#0000ff"><span></span></font> </div>
<div><font face="Arial" color="#0000ff"><span>Below is
what I've done to display a custom colormap on a raster. Could it be adapted to
your problem?</span></font></div>
<div><font face="Arial" color="#0000ff"><span></span></font> </div>
<div><font face="Arial" color="#0000ff"><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 &
legend<br></font> if hasattr(theLayer, "setCacheImage"):
</span></font></div>
<div><font face="Arial" color="#0000ff"><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 face="Arial" color="#0000ff"><span>Cheers,</span></font></div>
<div><font face="Arial" color="#0000ff"><span>Benoit</span></font></div><div><div></div><div>
<div><font face="Arial" color="#0000ff"><span> </span></font></div>
<blockquote dir="ltr" style="padding-left:5px;margin-left:5px;border-left:#0000ff 2px solid;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"><<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:#ccc 1px solid">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-right:rgb(221,221,221) 1px solid;padding-right:1.5em;border-top:rgb(221,221,221) 1px solid;padding-left:1.5em;font-size:12px;padding-bottom:0.75em;border-left:rgb(221,221,221) 1px solid;padding-top:0.75em;border-bottom:rgb(221,221,221) 1px solid;background-color:rgb(238,238,238)">
mypLayer->setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED);
mypLayer->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></div>
<div><br>
<div class="gmail_quote">On Fri, Sep 3, 2010 at 7:43 AM, JP Glutting <span dir="ltr"><<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:#ccc 1px solid">
<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('SingleBandPseudoColor'))</div>
<div>
<div>
resultsLayer.setCacheImage(None)</div>
<div>
resultsLayer.triggerRepaint()</div></div></div>
<div><br></div>
<div>(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:</div>
<div><br></div>
<div><span style="font-size:13px;font-family:monospace, fixed">myRasterLayer-><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-right:rgb(204,204,204) 1px solid;padding-right:6px;border-top:rgb(204,204,204) 1px solid;padding-left:6px;font-size:13px;padding-bottom:4px;margin:4px 8px 4px 2px;border-left:rgb(204,204,204) 1px solid;padding-top:4px;border-bottom:rgb(204,204,204) 1px solid;font-family:monospace, fixed;background-color:rgb(245,245,245)">
<br>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="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00205"></a>00205 <span style="color:rgb(0,128,0)">const</span> QString & baseName = QString(),
<a style="color:rgb(26,65,168)" name="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00206"></a>00206 <span style="color:rgb(0,128,0)">const</span> QString & path = QString(),
<a style="color:rgb(26,65,168)" name="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00207"></a>00207 <span style="color:rgb(0,128,0)">const</span> QString & providerLib = QString(),
<a style="color:rgb(26,65,168)" name="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00208"></a>00208 <span style="color:rgb(0,128,0)">const</span> QStringList & layers = QStringList(),
<a style="color:rgb(26,65,168)" name="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00209"></a>00209 <span style="color:rgb(0,128,0)">const</span> QStringList & styles = QStringList(),
<a style="color:rgb(26,65,168)" name="12ae2611cd218532_12add9b15e2d46b2_12ad6257a37c3c34_12ad61ec7902b935_l00210"></a>00210 <span style="color:rgb(0,128,0)">const</span> QString & 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 & 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></div>
<div><br>
<div class="gmail_quote">On Fri, Sep 3, 2010 at 3:04 AM, JP Glutting <span dir="ltr"><<a href="mailto:jpglutting@gmail.com" target="_blank">jpglutting@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:#ccc 1px solid">Hello,
<div><br></div>
<div>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:</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'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'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
"/Users//.qgis/python/plugins/mcelite/MCELiteDialog.py", line 361, in
accept<br> extentMin, extentMax =
resultsLayer.computeMinimumMaximumFromLastExtent(band)<br>TypeError:
'float' 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'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 class="im"><br>
<a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
</div><div class="im"><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>