QGIS API Documentation  2.99.0-Master (8ec3eaf)
qgsrasterminmaxwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterminmaxwidget.h
3  ---------------------------------
4  begin : July 2012
5  copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include <QSettings>
19 #include <QMessageBox>
20 
21 #include "qgsrasterlayer.h"
22 #include "qgsrasterminmaxwidget.h"
23 #include "qgsmapcanvas.h"
24 #include "qgsrasterrenderer.h"
25 #include "qgsrasterdataprovider.h"
26 
28  : QWidget( parent )
29  , mLayer( theLayer )
30  , mCanvas( nullptr )
31 {
32  QgsDebugMsg( "Entered." );
33  setupUi( this );
34 
35  QSettings mySettings;
36 
37  // set contrast enhancement setting to default
38  // ideally we should set it actual method last used to get min/max, but there is no way to know currently
39  QString contrastEnchacementLimits = mySettings.value( QStringLiteral( "/Raster/defaultContrastEnhancementLimits" ), "CumulativeCut" ).toString();
40  if ( contrastEnchacementLimits == QLatin1String( "MinMax" ) )
41  mMinMaxRadioButton->setChecked( true );
42  else if ( contrastEnchacementLimits == QLatin1String( "StdDev" ) )
43  mStdDevRadioButton->setChecked( true );
44 
45  double myLower = 100.0 * mySettings.value( QStringLiteral( "/Raster/cumulativeCutLower" ), QString::number( QgsRasterLayer::CUMULATIVE_CUT_LOWER ) ).toDouble();
46  double myUpper = 100.0 * mySettings.value( QStringLiteral( "/Raster/cumulativeCutUpper" ), QString::number( QgsRasterLayer::CUMULATIVE_CUT_UPPER ) ).toDouble();
47  mCumulativeCutLowerDoubleSpinBox->setValue( myLower );
48  mCumulativeCutUpperDoubleSpinBox->setValue( myUpper );
49 
50  mStdDevSpinBox->setValue( mySettings.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble() );
51 }
52 
54 {
55 }
56 
58 {
59  mCanvas = canvas;
60 }
61 
63 {
64  return mCanvas;
65 }
66 
68 {
69  if ( !cbxClipExtent->isChecked() )
70  return QgsRectangle();
71 
72  if ( mLayer && mCanvas )
73  return mCanvas->mapSettings().outputExtentToLayerExtent( mLayer, mCanvas->extent() );
74  else if ( mCanvas )
75  return mCanvas->extent();
76  else
77  return QgsRectangle();
78 }
79 
80 void QgsRasterMinMaxWidget::on_mLoadPushButton_clicked()
81 {
82  QgsDebugMsg( "Entered." );
83 
84  Q_FOREACH ( int myBand, mBands )
85  {
87  QgsDebugMsg( QString( "myBand = %1" ).arg( myBand ) );
88  if ( myBand < 1 || myBand > mLayer->dataProvider()->bandCount() )
89  {
90  continue;
91  }
92  double myMin = std::numeric_limits<double>::quiet_NaN();
93  double myMax = std::numeric_limits<double>::quiet_NaN();
94 
95  QgsRectangle myExtent = extent(); // empty == full
96  if ( cbxClipExtent->isChecked() )
97  {
99  }
100  else
101  {
103  }
104  QgsDebugMsg( QString( "myExtent.isEmpty() = %1" ).arg( myExtent.isEmpty() ) );
105 
106  int mySampleSize = sampleSize(); // 0 == exact
107  if ( cboAccuracy->currentIndex() == 0 )
108  {
110  }
111  else
112  {
114  }
115 
116  if ( mCumulativeCutRadioButton->isChecked() )
117  {
118  double myLower = mCumulativeCutLowerDoubleSpinBox->value() / 100.0;
119  double myUpper = mCumulativeCutUpperDoubleSpinBox->value() / 100.0;
120  mLayer->dataProvider()->cumulativeCut( myBand, myLower, myUpper, myMin, myMax, myExtent, mySampleSize );
122  }
123  else if ( mMinMaxRadioButton->isChecked() )
124  {
125  // TODO: consider provider minimum/maximumValue() (has to be defined well in povider)
126  QgsRasterBandStats myRasterBandStats = mLayer->dataProvider()->bandStatistics( myBand, QgsRasterBandStats::Min | QgsRasterBandStats::Max, myExtent, mySampleSize );
127  myMin = myRasterBandStats.minimumValue;
128  myMax = myRasterBandStats.maximumValue;
130  }
131  else if ( mStdDevRadioButton->isChecked() )
132  {
133  QgsRasterBandStats myRasterBandStats = mLayer->dataProvider()->bandStatistics( myBand, QgsRasterBandStats::Mean | QgsRasterBandStats::StdDev, myExtent, mySampleSize );
134  double myStdDev = mStdDevSpinBox->value();
135  myMin = myRasterBandStats.mean - ( myStdDev * myRasterBandStats.stdDev );
136  myMax = myRasterBandStats.mean + ( myStdDev * myRasterBandStats.stdDev );
138  }
139  else
140  {
141  QMessageBox::warning( this, tr( "No option selected" ), tr( "Please select an option to load min/max values." ) );
142  return;
143  }
144 
145  emit load( myBand, myMin, myMax, origin );
146  }
147 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
double maximumValue
The maximum cell value in the raster band.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:106
virtual QgsRasterBandStats bandStatistics(int theBandNo, int theStats=QgsRasterBandStats::All, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Get band statistics.
double stdDev
The standard deviation of the cell values.
The RasterBandStats struct is a container for statistics about a single raster band.
double mean
The mean cell value for the band. NO_DATA values are excluded.
bool isEmpty() const
test if rectangle is empty.
static const double CUMULATIVE_CUT_UPPER
Default cumulative cut upper limit.
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
QgsRasterMinMaxWidget(QgsRasterLayer *theLayer, QWidget *parent=nullptr)
int sampleSize()
Return the selected sample size.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
QgsRectangle extent()
Return the extent selected by the user.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
static const double CUMULATIVE_CUT_LOWER
Default cumulative cut lower limit.
double minimumValue
The minimum cell value in the raster band.
QgsRasterDataProvider * dataProvider()
Returns the data provider.
virtual void cumulativeCut(int theBandNo, double theLowerCount, double theUpperCount, double &theLowerValue, double &theUpperValue, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Find values for cumulative pixel count cut.
QgsMapCanvas * mapCanvas()
Returns the map canvas associated with the widget.
QgsRectangle outputExtentToLayerExtent(QgsMapLayer *theLayer, QgsRectangle extent) const
transform bounding box from output CRS to layer&#39;s CRS