QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
26  : QWidget( parent )
27  , mLayer( theLayer )
28  , mCanvas( nullptr )
29 {
30  QgsDebugMsg( "Entered." );
31  setupUi( this );
32 
33  QSettings mySettings;
34 
35  // set contrast enhancement setting to default
36  // ideally we should set it actual method last used to get min/max, but there is no way to know currently
37  QString contrastEnchacementLimits = mySettings.value( "/Raster/defaultContrastEnhancementLimits", "CumulativeCut" ).toString();
38  if ( contrastEnchacementLimits == "MinMax" )
39  mMinMaxRadioButton->setChecked( true );
40  else if ( contrastEnchacementLimits == "StdDev" )
41  mStdDevRadioButton->setChecked( true );
42 
43  double myLower = 100.0 * mySettings.value( "/Raster/cumulativeCutLower", QString::number( QgsRasterLayer::CUMULATIVE_CUT_LOWER ) ).toDouble();
44  double myUpper = 100.0 * mySettings.value( "/Raster/cumulativeCutUpper", QString::number( QgsRasterLayer::CUMULATIVE_CUT_UPPER ) ).toDouble();
45  mCumulativeCutLowerDoubleSpinBox->setValue( myLower );
46  mCumulativeCutUpperDoubleSpinBox->setValue( myUpper );
47 
48  mStdDevSpinBox->setValue( mySettings.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble() );
49 }
50 
52 {
53 }
54 
56 {
57  mCanvas = canvas;
58 }
59 
61 {
62  return mCanvas;
63 }
64 
66 {
67  if ( !cbxClipExtent->isChecked() )
68  return QgsRectangle();
69 
70  if ( mLayer && mCanvas )
71  return mCanvas->mapSettings().outputExtentToLayerExtent( mLayer, mCanvas->extent() );
72  else if ( mCanvas )
73  return mCanvas->extent();
74  else
75  return QgsRectangle();
76 }
77 
78 void QgsRasterMinMaxWidget::on_mLoadPushButton_clicked()
79 {
80  QgsDebugMsg( "Entered." );
81 
82  Q_FOREACH ( int myBand, mBands )
83  {
85  QgsDebugMsg( QString( "myBand = %1" ).arg( myBand ) );
86  if ( myBand < 1 || myBand > mLayer->dataProvider()->bandCount() )
87  {
88  continue;
89  }
90  double myMin = std::numeric_limits<double>::quiet_NaN();
91  double myMax = std::numeric_limits<double>::quiet_NaN();
92 
93  QgsRectangle myExtent = extent(); // empty == full
94  if ( cbxClipExtent->isChecked() )
95  {
97  }
98  else
99  {
101  }
102  QgsDebugMsg( QString( "myExtent.isEmpty() = %1" ).arg( myExtent.isEmpty() ) );
103 
104  int mySampleSize = sampleSize(); // 0 == exact
105  if ( cboAccuracy->currentIndex() == 0 )
106  {
108  }
109  else
110  {
112  }
113 
114  if ( mCumulativeCutRadioButton->isChecked() )
115  {
116  double myLower = mCumulativeCutLowerDoubleSpinBox->value() / 100.0;
117  double myUpper = mCumulativeCutUpperDoubleSpinBox->value() / 100.0;
118  mLayer->dataProvider()->cumulativeCut( myBand, myLower, myUpper, myMin, myMax, myExtent, mySampleSize );
120  }
121  else if ( mMinMaxRadioButton->isChecked() )
122  {
123  // TODO: consider provider minimum/maximumValue() (has to be defined well in povider)
124  QgsRasterBandStats myRasterBandStats = mLayer->dataProvider()->bandStatistics( myBand, QgsRasterBandStats::Min | QgsRasterBandStats::Max, myExtent, mySampleSize );
125  myMin = myRasterBandStats.minimumValue;
126  myMax = myRasterBandStats.maximumValue;
128  }
129  else if ( mStdDevRadioButton->isChecked() )
130  {
131  QgsRasterBandStats myRasterBandStats = mLayer->dataProvider()->bandStatistics( myBand, QgsRasterBandStats::Mean | QgsRasterBandStats::StdDev, myExtent, mySampleSize );
132  double myStdDev = mStdDevSpinBox->value();
133  myMin = myRasterBandStats.mean - ( myStdDev * myRasterBandStats.stdDev );
134  myMax = myRasterBandStats.mean + ( myStdDev * myRasterBandStats.stdDev );
136  }
137  else
138  {
139  QMessageBox::warning( this, tr( "No option selected" ), tr( "Please select an option to load min/max values." ) );
140  return;
141  }
142 
143  emit load( myBand, myMin, myMax, origin );
144  }
145 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setupUi(QWidget *widget)
#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.
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
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.
QString number(int n, int base)
The RasterBandStats struct is a container for statistics about a single raster band.
double mean
The mean cell value for the band.
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.
QVariant value(const QString &key, const QVariant &defaultValue) const
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.
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
double toDouble(bool *ok) const
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.
QString toString() const
QgsRectangle outputExtentToLayerExtent(QgsMapLayer *theLayer, QgsRectangle extent) const
transform bounding box from output CRS to layer&#39;s CRS