QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgis_core.h"
20#include "qgis_sip.h"
22
23#include <QIcon>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
28class QgsVectorLayer;
30
31
32#ifdef SIP_RUN
33// This is required for the ConvertToSubClassCode to work properly
34// so RTTI for casting is available in the whole module.
35% ModuleCode
42% End
43#endif
44
45
46
52class CORE_EXPORT QgsClassificationRange
53{
54 public:
56 QgsClassificationRange( const QString &label, double lowerBound, double upperBound )
57 : mLabel( label )
58 , mLowerBound( lowerBound )
59 , mUpperBound( upperBound )
60 {}
61
62 double lowerBound() const {return mLowerBound;}
64 double upperBound() const {return mUpperBound;}
65
67 QString label() const {return mLabel;}
68
69#ifdef SIP_RUN
70 SIP_PYOBJECT __repr__();
71 % MethodCode
72 QString str = u"<QgsClassificationRange: '%1'>"_s.arg( sipCpp->label() );
73 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
74 % End
75#endif
76
77 private:
78 QString mLabel;
79 double mLowerBound;
80 double mUpperBound;
81};
82
83
84
92{
93
94#ifdef SIP_RUN
96 if ( dynamic_cast<QgsClassificationEqualInterval *>( sipCpp ) )
97 sipType = sipType_QgsClassificationEqualInterval;
98 else if ( dynamic_cast<QgsClassificationJenks *>( sipCpp ) )
99 sipType = sipType_QgsClassificationJenks;
100 else if ( dynamic_cast<QgsClassificationPrettyBreaks *>( sipCpp ) )
101 sipType = sipType_QgsClassificationPrettyBreaks;
102 else if ( dynamic_cast<QgsClassificationQuantile *>( sipCpp ) )
103 sipType = sipType_QgsClassificationQuantile;
104 else if ( dynamic_cast<QgsClassificationStandardDeviation *>( sipCpp ) )
105 sipType = sipType_QgsClassificationStandardDeviation;
106 else if ( dynamic_cast<QgsClassificationFixedInterval *>( sipCpp ) )
107 sipType = sipType_QgsClassificationFixedInterval;
108 else
109 sipType = 0;
110 SIP_END
111#endif
112
113 public:
114
124
125
126
133
139 explicit QgsClassificationMethod( MethodProperties properties = NoFlag, int codeComplexity = 1 );
140
141 virtual ~QgsClassificationMethod();
142
147 virtual std::unique_ptr< QgsClassificationMethod > clone() const = 0;
148
150 virtual QString name() const = 0;
151
153 virtual QString id() const = 0;
154
156 virtual QIcon icon() const {return QIcon();}
157
164
168 virtual QString labelForRange( double lowerValue, double upperValue, ClassPosition position = Inner ) const;
169
170
172 virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const {Q_UNUSED( element ); Q_UNUSED( context )}
174 virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) {Q_UNUSED( element ); Q_UNUSED( context )}
175
180 virtual bool valuesRequired() const {return true;}
181
182
183 // *******************
184 // non-virtual methods
185
187 int codeComplexity() const {return mCodeComplexity;}
188
192 bool symmetricModeAvailable() const {return mFlags.testFlag( SymmetricModeAvailable );}
193
197 bool symmetricModeEnabled() const {return symmetricModeAvailable() && mSymmetricEnabled;}
198
202 double symmetryPoint() const {return mSymmetryPoint;}
203
208 bool symmetryAstride() const {return mSymmetryAstride;}
209
217 void setSymmetricMode( bool enabled, double symmetryPoint = 0, bool symmetryAstride = false );
218
219 // Label properties
221 QString labelFormat() const { return mLabelFormat; }
223 void setLabelFormat( const QString &format ) { mLabelFormat = format; }
225 int labelPrecision() const { return mLabelPrecision; }
227 void setLabelPrecision( int labelPrecision );
229 bool labelTrimTrailingZeroes() const { return mLabelTrimTrailingZeroes; }
231 void setLabelTrimTrailingZeroes( bool trimTrailingZeroes ) { mLabelTrimTrailingZeroes = trimTrailingZeroes; }
232
234 static QList<double> rangesToBreaks( const QList<QgsClassificationRange> &classes );
235
244 Q_DECL_DEPRECATED QList<QgsClassificationRange> classes( const QgsVectorLayer *layer, const QString &expression, int nclasses ) SIP_DEPRECATED;
245
258 QList<QgsClassificationRange> classesV2( const QgsVectorLayer *layer, const QString &expression, int nclasses, QString &error SIP_OUT );
259
265 QList<QgsClassificationRange> classes( const QList<double> &values, int nclasses );
266
274 QList<QgsClassificationRange> classes( double minimum, double maximum, int nclasses );
275
281 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
282
288 static std::unique_ptr< QgsClassificationMethod > create( const QDomElement &element, const QgsReadWriteContext &context );
289
297 static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride );
298
302 QString labelForRange( const QgsRendererRange &range, ClassPosition position = Inner ) const;
303
308 const QgsProcessingParameterDefinition *parameterDefinition( const QString &parameterName ) const;
309
315
320 void setParameterValues( const QVariantMap &values );
321
327 QVariantMap parameterValues() const {return mParameterValues;}
328
329 static const int MAX_PRECISION;
330 static const int MIN_PRECISION;
331
332 protected:
333
335 void copyBase( QgsClassificationMethod *c ) const;
336
338 QString formatNumber( double value ) const;
339
348
349 private:
350
356 virtual QList<double> calculateBreaks( double &minimum, double &maximum,
357 const QList<double> &values, int nclasses, QString &error ) = 0;
358
360 virtual QString valueToLabel( double value ) const {return formatNumber( value );}
361
363 QList<QgsClassificationRange> breaksToClasses( const QList<double> &breaks ) const;
364
365 // implementation properties (set by initialization)
366 MethodProperties mFlags = MethodProperties();
367 int mCodeComplexity = 1;
368
369 // parameters (set by setters)
370 // if some are added here, they should be handled in the clone method
371 bool mSymmetricEnabled = false;
372 double mSymmetryPoint = 0;
373 bool mSymmetryAstride = false;
374 int mLabelPrecision = 4;
375 bool mLabelTrimTrailingZeroes = true;
376 QString mLabelFormat;
377
378 // values used to manage number formatting - precision and trailing zeroes
379 double mLabelNumberScale = 1.0;
380 QString mLabelNumberSuffix;
381
384 QVariantMap mParameterValues;
385};
386
388
389#endif // QGSCLASSIFICATIONMETHOD_H
A classification method which uses equal width intervals.
Implementation of a fixed interval classification.
A classification method for natural breaks, based on Jenks method.
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.
QFlags< MethodProperty > MethodProperties
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.
virtual std::unique_ptr< QgsClassificationMethod > clone() const =0
Returns a clone of the method.
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.
@ UpperBound
The class is at the upper 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...
void addParameter(QgsProcessingParameterDefinition *definition)
Add a parameter to the method.
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.
QgsClassificationMethod(MethodProperties properties=NoFlag, int codeComplexity=1)
Creates a classification 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.
QString formatNumber(double value) const
Format the number according to label properties.
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.
@ ValuesNotRequired
Deprecated since QGIS 3.12.
@ SymmetricModeAvailable
This allows using symmetric classification.
@ IgnoresClassCount
The classification method does not compute classes based on a class count.
bool symmetricModeAvailable() const
Returns if the method supports symmetric calculation.
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation).
QgsClassificationMethod::MethodProperties flags() const
Returns the classification flags.
A classification method which applies pretty breaks to data.
A classification method which creates classes based on quantiles.
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.
A classification method which classifies based on standard deviation of values.
Base class for the definition of processing parameters.
A container for the context for various read/write operations on objects.
Represents a value range for a QgsGraduatedSymbolRenderer.
Represents a vector layer which manages a vector based dataset.
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 SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_ABSTRACT
Definition qgis_sip.h:221
#define SIP_INOUT
Definition qgis_sip.h:79
#define SIP_END
Definition qgis_sip.h:216
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)