QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
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
25class QgsVectorLayer;
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
39% End
40#endif
41
42
43
49class 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
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
368Q_DECLARE_OPERATORS_FOR_FLAGS( QgsClassificationMethod::MethodProperties )
369
370#endif // QGSCLASSIFICATIONMETHOD_H
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
Implementation of a fixed interval classification.
QgsClassificationJenks is an implementation of QgsClassificationMethod for natural breaks based on Je...
QgsClassificationMethod is an abstract class for implementations of classification methods.
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
int codeComplexity() const
Code complexity as the exponent in Big O notation.
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
virtual QgsClassificationMethod * clone() const =0
Returns a clone of the method.
int labelPrecision() const
Returns the precision for the formatting of the labels.
virtual QString id() const =0
The id of the method as saved in the project, must be unique in registry.
void setLabelTrimTrailingZeroes(bool trimTrailingZeroes)
Defines if the trailing 0 are trimmed in the label.
QVariantMap parameterValues() const
Returns the values of the processing parameters.
ClassPosition
Defines the class position.
@ LowerBound
The class is at the lower bound.
@ Inner
The class is not at a bound.
bool symmetryAstride() const
Returns if the symmetric mode is astride if true, it will remove the symmetry point break so that the...
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads extra information to apply it to the method.
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes extra information about the method.
QString labelFormat() const
Returns the format of the label for the classes.
virtual bool valuesRequired() const
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
virtual QString name() const =0
The readable and translate name of the method.
void setLabelFormat(const QString &format)
Defines the format of the labels for the classes, using %1 and %2 for the bounds.
virtual QIcon icon() const
The icon of the method.
bool labelTrimTrailingZeroes() const
Returns if the trailing 0 are trimmed in the label.
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns the list of parameters.
MethodProperty
Flags for the classification method.
bool symmetricModeAvailable() const
Returns if the method supports symmetric calculation.
QgsClassificationMethod::MethodProperties flags() const
Returns the classification flags.
QgsClassificationPrettyBreaks is an implementation of QgsClassificationMethod for pretty breaks.
QgsClassificationQuantile is an implementation of QgsClassificationMethod based on quantiles.
QgsClassificationRange contains the information about a classification range.
QgsClassificationRange(const QString &label, double lowerBound, double upperBound)
Constructor.
QString label() const
Returns the lower bound.
double upperBound() const
Returns the upper bound.
double lowerBound() const
Returns the lower bound.
QgsClassificationCustom is an implementation of QgsClassificationMethod based on standard deviation.
Base class for the definition of processing parameters.
The class is used as a container of context for various read/write operations on other objects.
Represents a vector layer which manages a vector based data sets.
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
#define str(x)
Definition: qgis.cpp:37
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_ABSTRACT
Definition: qgis_sip.h:208
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
#define SIP_END
Definition: qgis_sip.h:203
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.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.