QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
38 % End
39 #endif
40 
41 
42 
48 class CORE_EXPORT QgsClassificationRange
49 {
50  public:
52  QgsClassificationRange( const QString &label, double lowerBound, double upperBound )
53  : mLabel( label )
54  , mLowerBound( lowerBound )
55  , mUpperBound( upperBound )
56  {}
58  double lowerBound() const {return mLowerBound;}
60  double upperBound() const {return mUpperBound;}
61 
63  QString label() const {return mLabel;}
64 
65 #ifdef SIP_RUN
66  SIP_PYOBJECT __repr__();
67  % MethodCode
68  QString str = QStringLiteral( "<QgsClassificationRange: '%1'>" ).arg( sipCpp->label() );
69  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
70  % End
71 #endif
72 
73  private:
74  QString mLabel;
75  double mLowerBound;
76  double mUpperBound;
77 };
78 
79 
80 
88 {
89 
90 #ifdef SIP_RUN
92  if ( dynamic_cast<QgsClassificationEqualInterval *>( sipCpp ) )
93  sipType = sipType_QgsClassificationEqualInterval;
94  else if ( dynamic_cast<QgsClassificationJenks *>( sipCpp ) )
95  sipType = sipType_QgsClassificationJenks;
96  else if ( dynamic_cast<QgsClassificationPrettyBreaks *>( sipCpp ) )
97  sipType = sipType_QgsClassificationPrettyBreaks;
98  else if ( dynamic_cast<QgsClassificationQuantile *>( sipCpp ) )
99  sipType = sipType_QgsClassificationQuantile;
100  else if ( dynamic_cast<QgsClassificationStandardDeviation *>( sipCpp ) )
101  sipType = sipType_QgsClassificationStandardDeviation;
102  else
103  sipType = 0;
104  SIP_END
105 #endif
106 
107  public:
108 
111  {
112  NoFlag = 0,
113  ValuesNotRequired = 1 << 1,
114  SymmetricModeAvailable = 1 << 2,
115  };
116  Q_DECLARE_FLAGS( MethodProperties, MethodProperty )
117 
118 
119 
121  {
124  UpperBound
125  };
126 
132  explicit QgsClassificationMethod( MethodProperties properties = NoFlag, int codeComplexity = 1 );
133 
134  virtual ~QgsClassificationMethod();
135 
141 
143  virtual QString name() const = 0;
144 
146  virtual QString id() const = 0;
147 
149  virtual QIcon icon() const {return QIcon();}
150 
154  virtual QString labelForRange( double lowerValue, double upperValue, ClassPosition position = Inner ) const;
155 
156 
158  virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const {Q_UNUSED( element ); Q_UNUSED( context )}
160  virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) {Q_UNUSED( element ); Q_UNUSED( context )}
161 
166  virtual bool valuesRequired() const {return true;}
167 
168 
169  // *******************
170  // non-virtual methods
171 
173  int codeComplexity() const {return mCodeComplexity;}
174 
178  bool symmetricModeAvailable() const {return mFlags.testFlag( SymmetricModeAvailable );}
179 
183  bool symmetricModeEnabled() const {return symmetricModeAvailable() && mSymmetricEnabled;}
184 
188  double symmetryPoint() const {return mSymmetryPoint;}
189 
194  bool symmetryAstride() const {return mSymmetryAstride;}
195 
203  void setSymmetricMode( bool enabled, double symmetryPoint = 0, bool symmetryAstride = false );
204 
205  // Label properties
207  QString labelFormat() const { return mLabelFormat; }
209  void setLabelFormat( const QString &format ) { mLabelFormat = format; }
211  int labelPrecision() const { return mLabelPrecision; }
213  void setLabelPrecision( int labelPrecision );
215  bool labelTrimTrailingZeroes() const { return mLabelTrimTrailingZeroes; }
217  void setLabelTrimTrailingZeroes( bool trimTrailingZeroes ) { mLabelTrimTrailingZeroes = trimTrailingZeroes; }
218 
220  static QList<double> rangesToBreaks( const QList<QgsClassificationRange> &classes );
221 
228  QList<QgsClassificationRange> classes( const QgsVectorLayer *layer, const QString &expression, int nclasses );
229 
235  QList<QgsClassificationRange> classes( const QList<double> &values, int nclasses );
236 
244  QList<QgsClassificationRange> classes( double minimum, double maximum, int nclasses );
245 
251  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
252 
258  static QgsClassificationMethod *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
259 
267  static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride );
268 
272  QString labelForRange( const QgsRendererRange &range, ClassPosition position = Inner ) const;
273 
278  const QgsProcessingParameterDefinition *parameterDefinition( const QString &parameterName ) const;
279 
285 
290  void setParameterValues( const QVariantMap &values );
291 
297  QVariantMap parameterValues() const {return mParameterValues;}
298 
299  static const int MAX_PRECISION;
300  static const int MIN_PRECISION;
301 
302  protected:
303 
305  void copyBase( QgsClassificationMethod *c ) const;
306 
308  QString formatNumber( double value ) const;
309 
317  void addParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER );
318 
319  private:
320 
326  virtual QList<double> calculateBreaks( double &minimum, double &maximum,
327  const QList<double> &values, int nclasses ) = 0;
328 
330  virtual QString valueToLabel( double value ) const {return formatNumber( value );}
331 
333  QList<QgsClassificationRange> breaksToClasses( const QList<double> &breaks ) const;
334 
335  // implementation properties (set by initialization)
336  MethodProperties mFlags = MethodProperties();
337  int mCodeComplexity = 1;
338 
339  // parameters (set by setters)
340  // if some are added here, they should be handled in the clone method
341  bool mSymmetricEnabled = false;
342  double mSymmetryPoint = 0;
343  bool mSymmetryAstride = false;
344  int mLabelPrecision = 4;
345  bool mLabelTrimTrailingZeroes = true;
346  QString mLabelFormat;
347 
348  // values used to manage number formatting - precision and trailing zeroes
349  double mLabelNumberScale = 1.0;
350  QString mLabelNumberSuffix;
351 
354  QVariantMap mParameterValues;
355 };
356 
357 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsClassificationMethod::MethodProperties )
358 
359 #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:52
qgsclassificationstandarddeviation.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsClassificationPrettyBreaks
QgsClassificationPrettryBreaks is an implementation of QgsClassificationMethod for pretty breaks.
Definition: qgsclassificationprettybreaks.h:29
QgsClassificationMethod::icon
virtual QIcon icon() const
The icon of the method.
Definition: qgsclassificationmethod.h:149
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:194
QgsClassificationMethod::writeXml
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes extra information about the method.
Definition: qgsclassificationmethod.h:158
QgsRendererRange
Definition: qgsrendererrange.h:37
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:331
qgsclassificationprettybreaks.h
QgsClassificationMethod::codeComplexity
int codeComplexity() const
Code complexity as the exponent in Big O notation.
Definition: qgsclassificationmethod.h:173
QgsClassificationMethod::valuesRequired
virtual bool valuesRequired() const
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
Definition: qgsclassificationmethod.h:166
QgsProcessingParameterDefinitions
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Definition: qgsprocessingparameters.h:761
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsClassificationMethod::parameterDefinitions
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns the list of parameters.
Definition: qgsclassificationmethod.h:284
QgsClassificationEqualInterval
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
Definition: qgsclassificationequalinterval.h:29
QgsClassificationQuantile
QgsClassificationQuantile is an implementation of QgsClassificationMethod based on quantiles.
Definition: qgsclassificationquantile.h:30
QgsClassificationMethod::symmetryPoint
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
Definition: qgsclassificationmethod.h:188
QgsClassificationMethod::clone
virtual QgsClassificationMethod * clone() const =0
Returns a clone of the method.
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
QgsClassificationMethod::LowerBound
@ LowerBound
The class is at the lower bound.
Definition: qgsclassificationmethod.h:122
QgsClassificationMethod::labelPrecision
int labelPrecision() const
Returns the precision for the formatting of the labels.
Definition: qgsclassificationmethod.h:211
QgsClassificationRange::label
QString label() const
Returns the lower bound.
Definition: qgsclassificationmethod.h:63
QgsClassificationMethod::id
virtual QString id() const =0
The id of the method as saved in the project, must be unique in registry.
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:209
QgsClassificationMethod::MIN_PRECISION
static const int MIN_PRECISION
Definition: qgsclassificationmethod.h:300
QgsClassificationMethod::symmetricModeEnabled
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
Definition: qgsclassificationmethod.h:183
QgsClassificationMethod::MAX_PRECISION
static const int MAX_PRECISION
Definition: qgsclassificationmethod.h:299
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:215
QgsClassificationMethod::Inner
@ Inner
The class is not at a bound.
Definition: qgsclassificationmethod.h:123
QgsClassificationRange::upperBound
double upperBound() const
Returns the upper bound.
Definition: qgsclassificationmethod.h:60
qgsclassificationequalinterval.h
QgsClassificationMethod::MethodProperty
MethodProperty
Flags for the classification method.
Definition: qgsclassificationmethod.h:111
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
QgsClassificationRange::lowerBound
double lowerBound() const
Returns the lower bound.
Definition: qgsclassificationmethod.h:58
QgsClassificationMethod::readXml
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads extra information to apply it to the method.
Definition: qgsclassificationmethod.h:160
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:207
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
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsClassificationMethod::ClassPosition
ClassPosition
Defines the class position.
Definition: qgsclassificationmethod.h:121
QgsClassificationMethod::setLabelTrimTrailingZeroes
void setLabelTrimTrailingZeroes(bool trimTrailingZeroes)
Defines if the trailing 0 are trimmed in the label.
Definition: qgsclassificationmethod.h:217
QgsClassificationJenks
QgsClassificationJenks is an implementation of QgsClassificationMethod for natural breaks based on Je...
Definition: qgsclassificationjenks.h:29
QgsClassificationStandardDeviation
QgsClassificationCustom is an implementation of QgsClassificationMethod based on standard deviation.
Definition: qgsclassificationstandarddeviation.h:29
qgsclassificationquantile.h
QgsClassificationMethod::name
virtual QString name() const =0
The readable and translate name of the method.
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
QgsClassificationMethod::parameterValues
QVariantMap parameterValues() const
Returns the values of the processing parameters.
Definition: qgsclassificationmethod.h:297
QgsClassificationMethod::symmetricModeAvailable
bool symmetricModeAvailable() const
Returns if the method supports symmetric calculation.
Definition: qgsclassificationmethod.h:178
SIP_ABSTRACT
#define SIP_ABSTRACT
Definition: qgis_sip.h:199