QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsclassificationmethod.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsclassificationmethod.h
3  ---------------------
4  begin : September 2019
5  copyright : (C) 2019 by Denis Rouzaud
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSCLASSIFICATIONMETHOD_H
17 #define QGSCLASSIFICATIONMETHOD_H
18 
19 #include <QIcon>
20 
21 #include "qgis_sip.h"
22 #include "qgis_core.h"
24 
25 class QgsVectorLayer;
26 class QgsRendererRange;
27 
28 
29 #ifdef SIP_RUN
30 // This is required for the ConvertToSubClassCode to work properly
31 // so RTTI for casting is available in the whole module.
32 % ModuleCode
34 #include "qgsclassificationjenks.h"
39 % End
40 #endif
41 
42 
43 
49 class CORE_EXPORT QgsClassificationRange
50 {
51  public:
53  QgsClassificationRange( const QString &label, double lowerBound, double upperBound )
54  : mLabel( label )
55  , mLowerBound( lowerBound )
56  , mUpperBound( upperBound )
57  {}
59  double lowerBound() const {return mLowerBound;}
61  double upperBound() const {return mUpperBound;}
62 
64  QString label() const {return mLabel;}
65 
66 #ifdef SIP_RUN
67  SIP_PYOBJECT __repr__();
68  % MethodCode
69  QString str = QStringLiteral( "<QgsClassificationRange: '%1'>" ).arg( sipCpp->label() );
70  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
71  % End
72 #endif
73 
74  private:
75  QString mLabel;
76  double mLowerBound;
77  double mUpperBound;
78 };
79 
80 
81 
89 {
90 
91 #ifdef SIP_RUN
93  if ( dynamic_cast<QgsClassificationEqualInterval *>( sipCpp ) )
94  sipType = sipType_QgsClassificationEqualInterval;
95  else if ( dynamic_cast<QgsClassificationJenks *>( sipCpp ) )
96  sipType = sipType_QgsClassificationJenks;
97  else if ( dynamic_cast<QgsClassificationPrettyBreaks *>( sipCpp ) )
98  sipType = sipType_QgsClassificationPrettyBreaks;
99  else if ( dynamic_cast<QgsClassificationQuantile *>( sipCpp ) )
100  sipType = sipType_QgsClassificationQuantile;
101  else if ( dynamic_cast<QgsClassificationStandardDeviation *>( sipCpp ) )
102  sipType = sipType_QgsClassificationStandardDeviation;
103  else if ( dynamic_cast<QgsClassificationFixedInterval *>( sipCpp ) )
104  sipType = sipType_QgsClassificationFixedInterval;
105  else
106  sipType = 0;
107  SIP_END
108 #endif
109 
110  public:
111 
114  {
115  NoFlag = 0,
116  ValuesNotRequired = 1 << 1,
117  SymmetricModeAvailable = 1 << 2,
118  IgnoresClassCount = 1 << 3,
119  };
120  Q_DECLARE_FLAGS( MethodProperties, MethodProperty )
121 
122 
123 
125  {
128  UpperBound
129  };
130 
136  explicit QgsClassificationMethod( MethodProperties properties = NoFlag, int codeComplexity = 1 );
137 
138  virtual ~QgsClassificationMethod();
139 
144  virtual QgsClassificationMethod *clone() const = 0 SIP_FACTORY;
145 
147  virtual QString name() const = 0;
148 
150  virtual QString id() const = 0;
151 
153  virtual QIcon icon() const {return QIcon();}
154 
160  QgsClassificationMethod::MethodProperties flags() const { return mFlags; }
161 
165  virtual QString labelForRange( double lowerValue, double upperValue, ClassPosition position = Inner ) const;
166 
167 
169  virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const {Q_UNUSED( element ); Q_UNUSED( context )}
171  virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) {Q_UNUSED( element ); Q_UNUSED( context )}
172 
177  virtual bool valuesRequired() const {return true;}
178 
179 
180  // *******************
181  // non-virtual methods
182 
184  int codeComplexity() const {return mCodeComplexity;}
185 
189  bool symmetricModeAvailable() const {return mFlags.testFlag( SymmetricModeAvailable );}
190 
194  bool symmetricModeEnabled() const {return symmetricModeAvailable() && mSymmetricEnabled;}
195 
199  double symmetryPoint() const {return mSymmetryPoint;}
200 
205  bool symmetryAstride() const {return mSymmetryAstride;}
206 
214  void setSymmetricMode( bool enabled, double symmetryPoint = 0, bool symmetryAstride = false );
215 
216  // Label properties
218  QString labelFormat() const { return mLabelFormat; }
220  void setLabelFormat( const QString &format ) { mLabelFormat = format; }
222  int labelPrecision() const { return mLabelPrecision; }
224  void setLabelPrecision( int labelPrecision );
226  bool labelTrimTrailingZeroes() const { return mLabelTrimTrailingZeroes; }
228  void setLabelTrimTrailingZeroes( bool trimTrailingZeroes ) { mLabelTrimTrailingZeroes = trimTrailingZeroes; }
229 
231  static QList<double> rangesToBreaks( const QList<QgsClassificationRange> &classes );
232 
239  QList<QgsClassificationRange> classes( const QgsVectorLayer *layer, const QString &expression, int nclasses );
240 
246  QList<QgsClassificationRange> classes( const QList<double> &values, int nclasses );
247 
255  QList<QgsClassificationRange> classes( double minimum, double maximum, int nclasses );
256 
262  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
263 
269  static QgsClassificationMethod *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
270 
278  static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride );
279 
283  QString labelForRange( const QgsRendererRange &range, ClassPosition position = Inner ) const;
284 
289  const QgsProcessingParameterDefinition *parameterDefinition( const QString &parameterName ) const;
290 
296 
301  void setParameterValues( const QVariantMap &values );
302 
308  QVariantMap parameterValues() const {return mParameterValues;}
309 
310  static const int MAX_PRECISION;
311  static const int MIN_PRECISION;
312 
313  protected:
314 
316  void copyBase( QgsClassificationMethod *c ) const;
317 
319  QString formatNumber( double value ) const;
320 
328  void addParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER );
329 
330  private:
331 
337  virtual QList<double> calculateBreaks( double &minimum, double &maximum,
338  const QList<double> &values, int nclasses ) = 0;
339 
341  virtual QString valueToLabel( double value ) const {return formatNumber( value );}
342 
344  QList<QgsClassificationRange> breaksToClasses( const QList<double> &breaks ) const;
345 
346  // implementation properties (set by initialization)
347  MethodProperties mFlags = MethodProperties();
348  int mCodeComplexity = 1;
349 
350  // parameters (set by setters)
351  // if some are added here, they should be handled in the clone method
352  bool mSymmetricEnabled = false;
353  double mSymmetryPoint = 0;
354  bool mSymmetryAstride = false;
355  int mLabelPrecision = 4;
356  bool mLabelTrimTrailingZeroes = true;
357  QString mLabelFormat;
358 
359  // values used to manage number formatting - precision and trailing zeroes
360  double mLabelNumberScale = 1.0;
361  QString mLabelNumberSuffix;
362 
365  QVariantMap mParameterValues;
366 };
367 
368 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsClassificationMethod::MethodProperties )
369 
370 #endif // QGSCLASSIFICATIONMETHOD_H
QgsClassificationMethod
QgsClassificationMethod is an abstract class for implementations of classification methods.
Definition: qgsclassificationmethod.h:88
QgsClassificationRange::QgsClassificationRange
QgsClassificationRange(const QString &label, double lowerBound, double upperBound)
Constructor.
Definition: qgsclassificationmethod.h:53
qgsclassificationstandarddeviation.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsClassificationPrettyBreaks
QgsClassificationPrettyBreaks is an implementation of QgsClassificationMethod for pretty breaks.
Definition: qgsclassificationprettybreaks.h:28
QgsClassificationMethod::icon
virtual QIcon icon() const
The icon of the method.
Definition: qgsclassificationmethod.h:153
QgsClassificationMethod::symmetryAstride
bool symmetryAstride() const
Returns if the symmetric mode is astride if true, it will remove the symmetry point break so that the...
Definition: qgsclassificationmethod.h:205
QgsClassificationMethod::writeXml
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes extra information about the method.
Definition: qgsclassificationmethod.h:169
QgsRendererRange
Definition: qgsrendererrange.h:36
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:334
qgsclassificationprettybreaks.h
QgsClassificationMethod::codeComplexity
int codeComplexity() const
Code complexity as the exponent in Big O notation.
Definition: qgsclassificationmethod.h:184
QgsClassificationMethod::valuesRequired
virtual bool valuesRequired() const
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
Definition: qgsclassificationmethod.h:177
QgsProcessingParameterDefinitions
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Definition: qgsprocessingparameters.h:889
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsClassificationMethod::parameterDefinitions
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns the list of parameters.
Definition: qgsclassificationmethod.h:295
QgsClassificationEqualInterval
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
Definition: qgsclassificationequalinterval.h:28
QgsClassificationQuantile
QgsClassificationQuantile is an implementation of QgsClassificationMethod based on quantiles.
Definition: qgsclassificationquantile.h:29
QgsClassificationMethod::symmetryPoint
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
Definition: qgsclassificationmethod.h:199
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
QgsClassificationMethod::LowerBound
@ LowerBound
The class is at the lower bound.
Definition: qgsclassificationmethod.h:126
QgsClassificationMethod::labelPrecision
int labelPrecision() const
Returns the precision for the formatting of the labels.
Definition: qgsclassificationmethod.h:222
QgsClassificationRange::label
QString label() const
Returns the lower bound.
Definition: qgsclassificationmethod.h:64
qgsclassificationjenks.h
QgsClassificationMethod::setLabelFormat
void setLabelFormat(const QString &format)
Defines the format of the labels for the classes, using %1 and %2 for the bounds.
Definition: qgsclassificationmethod.h:220
QgsClassificationMethod::MIN_PRECISION
static const int MIN_PRECISION
Definition: qgsclassificationmethod.h:311
QgsClassificationMethod::symmetricModeEnabled
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
Definition: qgsclassificationmethod.h:194
QgsClassificationMethod::flags
QgsClassificationMethod::MethodProperties flags() const
Returns the classification flags.
Definition: qgsclassificationmethod.h:160
QgsClassificationMethod::MAX_PRECISION
static const int MAX_PRECISION
Definition: qgsclassificationmethod.h:310
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsClassificationMethod::labelTrimTrailingZeroes
bool labelTrimTrailingZeroes() const
Returns if the trailing 0 are trimmed in the label.
Definition: qgsclassificationmethod.h:226
QgsClassificationFixedInterval
Implementation of a fixed interval classification.
Definition: qgsclassificationfixedinterval.h:28
QgsClassificationMethod::Inner
@ Inner
The class is not at a bound.
Definition: qgsclassificationmethod.h:127
QgsClassificationRange::upperBound
double upperBound() const
Returns the upper bound.
Definition: qgsclassificationmethod.h:61
qgsclassificationequalinterval.h
QgsClassificationMethod::MethodProperty
MethodProperty
Flags for the classification method.
Definition: qgsclassificationmethod.h:113
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
qgsclassificationfixedinterval.h
QgsClassificationRange::lowerBound
double lowerBound() const
Returns the lower bound.
Definition: qgsclassificationmethod.h:59
QgsClassificationMethod::readXml
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads extra information to apply it to the method.
Definition: qgsclassificationmethod.h:171
qgsprocessingparameters.h
QgsClassificationRange
QgsClassificationRange contains the information about a classification range.
Definition: qgsclassificationmethod.h:49
QgsClassificationMethod::labelFormat
QString labelFormat() const
Returns the format of the label for the classes.
Definition: qgsclassificationmethod.h:218
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
str
#define str(x)
Definition: qgis.cpp:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsClassificationMethod::ClassPosition
ClassPosition
Defines the class position.
Definition: qgsclassificationmethod.h:124
QgsClassificationMethod::setLabelTrimTrailingZeroes
void setLabelTrimTrailingZeroes(bool trimTrailingZeroes)
Defines if the trailing 0 are trimmed in the label.
Definition: qgsclassificationmethod.h:228
QgsClassificationJenks
QgsClassificationJenks is an implementation of QgsClassificationMethod for natural breaks based on Je...
Definition: qgsclassificationjenks.h:28
QgsClassificationStandardDeviation
QgsClassificationCustom is an implementation of QgsClassificationMethod based on standard deviation.
Definition: qgsclassificationstandarddeviation.h:28
qgsclassificationquantile.h
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
QgsClassificationMethod::parameterValues
QVariantMap parameterValues() const
Returns the values of the processing parameters.
Definition: qgsclassificationmethod.h:308
QgsClassificationMethod::symmetricModeAvailable
bool symmetricModeAvailable() const
Returns if the method supports symmetric calculation.
Definition: qgsclassificationmethod.h:189
SIP_ABSTRACT
#define SIP_ABSTRACT
Definition: qgis_sip.h:208