QGIS API Documentation  3.6.0-Noosa (5873452)
qgsrasterminmaxorigin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterminmaxorigin.h - Origin of min/max values
3  --------------------------------------
4  Date : Dec 2016
5  Copyright : (C) 2016 by Even Rouault
6  email : even.rouault at spatialys.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 "qgsrasterminmaxorigin.h"
19 #include "qgssettings.h"
20 
21 #include <QDomDocument>
22 #include <QDomElement>
23 #include <cmath>
24 
26  : mCumulativeCutLower( CUMULATIVE_CUT_LOWER )
27  , mCumulativeCutUpper( CUMULATIVE_CUT_UPPER )
28  , mStdDevFactor( DEFAULT_STDDEV_FACTOR )
29 {
30  QgsSettings mySettings;
31  mCumulativeCutLower = mySettings.value( QStringLiteral( "Raster/cumulativeCutLower" ), CUMULATIVE_CUT_LOWER ).toDouble();
32  mCumulativeCutUpper = mySettings.value( QStringLiteral( "Raster/cumulativeCutUpper" ), CUMULATIVE_CUT_UPPER ).toDouble();
33  mStdDevFactor = mySettings.value( QStringLiteral( "Raster/defaultStandardDeviation" ), DEFAULT_STDDEV_FACTOR ).toDouble();
34 }
35 
37 {
38  return mLimits == other.mLimits &&
39  mExtent == other.mExtent &&
40  mAccuracy == other.mAccuracy &&
41  std::fabs( mCumulativeCutLower - other.mCumulativeCutLower ) < 1e-5 &&
42  std::fabs( mCumulativeCutUpper - other.mCumulativeCutUpper ) < 1e-5 &&
43  std::fabs( mStdDevFactor - other.mStdDevFactor ) < 1e-5;
44 }
45 
47 {
48  switch ( limits )
49  {
50  case MinMax:
51  return QStringLiteral( "MinMax" );
52  case StdDev:
53  return QStringLiteral( "StdDev" );
54  case CumulativeCut:
55  return QStringLiteral( "CumulativeCut" );
56  default:
57  break;
58  }
59  return QStringLiteral( "None" );
60 }
61 
63 {
64  if ( limits == QLatin1String( "MinMax" ) )
65  {
66  return MinMax;
67  }
68  else if ( limits == QLatin1String( "StdDev" ) )
69  {
70  return StdDev;
71  }
72  else if ( limits == QLatin1String( "CumulativeCut" ) )
73  {
74  return CumulativeCut;
75  }
76  return None;
77 }
78 
80 {
81  switch ( minMaxExtent )
82  {
83  case WholeRaster:
84  return QStringLiteral( "WholeRaster" );
85  case CurrentCanvas:
86  return QStringLiteral( "CurrentCanvas" );
87  case UpdatedCanvas:
88  return QStringLiteral( "UpdatedCanvas" );
89  }
90  return QStringLiteral( "WholeRaster" );
91 }
92 
94 {
95  if ( extent == QLatin1String( "WholeRaster" ) )
96  {
97  return WholeRaster;
98  }
99  else if ( extent == QLatin1String( "CurrentCanvas" ) )
100  {
101  return CurrentCanvas;
102  }
103  else if ( extent == QLatin1String( "UpdatedCanvas" ) )
104  {
105  return UpdatedCanvas;
106  }
107  else
108  {
109  return WholeRaster;
110  }
111 }
112 
114 {
115  if ( accuracy == Exact )
116  return QStringLiteral( "Exact" );
117  return QStringLiteral( "Estimated" );
118 }
119 
121 {
122  if ( accuracy == QLatin1String( "Exact" ) )
123  return Exact;
124  return Estimated;
125 }
126 
127 void QgsRasterMinMaxOrigin::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
128 {
129  // limits
130  QDomElement limitsElem = doc.createElement( QStringLiteral( "limits" ) );
131  QDomText limitsText = doc.createTextNode( limitsString( mLimits ) );
132  limitsElem.appendChild( limitsText );
133  parentElem.appendChild( limitsElem );
134 
135  // extent
136  QDomElement extentElem = doc.createElement( QStringLiteral( "extent" ) );
137  QDomText extentText = doc.createTextNode( extentString( mExtent ) );
138  extentElem.appendChild( extentText );
139  parentElem.appendChild( extentElem );
140 
141  // statAccuracy
142  QDomElement statAccuracyElem = doc.createElement( QStringLiteral( "statAccuracy" ) );
143  QDomText statAccuracyText = doc.createTextNode( statAccuracyString( mAccuracy ) );
144  statAccuracyElem.appendChild( statAccuracyText );
145  parentElem.appendChild( statAccuracyElem );
146 
147  // mCumulativeCutLower
148  QDomElement cumulativeCutLowerElem = doc.createElement( QStringLiteral( "cumulativeCutLower" ) );
149  QDomText cumulativeCutLowerText = doc.createTextNode( QString::number( mCumulativeCutLower ) );
150  cumulativeCutLowerElem.appendChild( cumulativeCutLowerText );
151  parentElem.appendChild( cumulativeCutLowerElem );
152 
153  // mCumulativeCutUpper
154  QDomElement cumulativeCutUpperElem = doc.createElement( QStringLiteral( "cumulativeCutUpper" ) );
155  QDomText cumulativeCutUpperText = doc.createTextNode( QString::number( mCumulativeCutUpper ) );
156  cumulativeCutUpperElem.appendChild( cumulativeCutUpperText );
157  parentElem.appendChild( cumulativeCutUpperElem );
158 
159  // mCumulativeCutUpper
160  QDomElement stdDevFactorElem = doc.createElement( QStringLiteral( "stdDevFactor" ) );
161  QDomText stdDevFactorText = doc.createTextNode( QString::number( mStdDevFactor ) );
162  stdDevFactorElem.appendChild( stdDevFactorText );
163  parentElem.appendChild( stdDevFactorElem );
164 }
165 
166 void QgsRasterMinMaxOrigin::readXml( const QDomElement &elem )
167 {
168  QDomElement limitsElem = elem.firstChildElement( QStringLiteral( "limits" ) );
169  if ( !limitsElem.isNull() )
170  {
171  mLimits = limitsFromString( limitsElem.text() );
172  }
173 
174  QDomElement extentElem = elem.firstChildElement( QStringLiteral( "extent" ) );
175  if ( !extentElem.isNull() )
176  {
177  mExtent = extentFromString( extentElem.text() );
178  }
179 
180  QDomElement statAccuracyElem = elem.firstChildElement( QStringLiteral( "statAccuracy" ) );
181  if ( !statAccuracyElem.isNull() )
182  {
183  mAccuracy = statAccuracyFromString( statAccuracyElem.text() );
184  }
185 
186  QDomElement cumulativeCutLowerElem = elem.firstChildElement( QStringLiteral( "cumulativeCutLower" ) );
187  if ( !cumulativeCutLowerElem.isNull() )
188  {
189  mCumulativeCutLower = cumulativeCutLowerElem.text().toDouble();
190  }
191 
192  QDomElement cumulativeCutUpperElem = elem.firstChildElement( QStringLiteral( "cumulativeCutUpper" ) );
193  if ( !cumulativeCutUpperElem.isNull() )
194  {
195  mCumulativeCutUpper = cumulativeCutUpperElem.text().toDouble();
196  }
197 
198  QDomElement stdDevFactorElem = elem.firstChildElement( QStringLiteral( "stdDevFactor" ) );
199  if ( !stdDevFactorElem.isNull() )
200  {
201  mStdDevFactor = stdDevFactorElem.text().toDouble();
202  }
203 }
QgsRasterMinMaxOrigin::Limits limits() const
Returns the raster limits.
QgsRasterMinMaxOrigin::Extent extent() const
Returns the raster extent.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsRasterMinMaxOrigin()
Default constructor.
Extent
This enumerator describes the extent used to compute min/max values.
static Limits limitsFromString(const QString &limits)
Deserialize Limits.
StatAccuracy
This enumerator describes the accuracy used to compute statistics.
static QgsRasterMinMaxOrigin::Extent extentFromString(const QString &extent)
Deserialize Extent.
Current extent of the canvas (at the time of computation) is used to compute statistics.
This class describes the origin of min/max values.
bool operator==(const QgsRasterMinMaxOrigin &other) const
Equality operator.
void readXml(const QDomElement &elem)
Deserialize object.
Constantly updated extent of the canvas is used to compute statistics.
static constexpr double DEFAULT_STDDEV_FACTOR
Default standard deviation factor.
Range is [ mean - stdDevFactor() * stddev, mean + stdDevFactor() * stddev ].
static constexpr double CUMULATIVE_CUT_UPPER
Default cumulative cut upper limit.
static constexpr double CUMULATIVE_CUT_LOWER
Default cumulative cut lower limit.
Range is [ min + cumulativeCutLower() * (max - min), min + cumulativeCutUpper() * (max - min) ]...
static QString limitsString(Limits limits)
Returns a string to serialize Limits.
static QString extentString(QgsRasterMinMaxOrigin::Extent extent)
Returns a string to serialize Extent.
Limits
This enumerator describes the limits used to compute min/max values.
Whole raster is used to compute statistics.
static QString statAccuracyString(QgsRasterMinMaxOrigin::StatAccuracy accuracy)
Returns a string to serialize StatAccuracy.
static QgsRasterMinMaxOrigin::StatAccuracy statAccuracyFromString(const QString &accuracy)
Deserialize StatAccuracy.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Serialize object.