QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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// clang-format off
34// This is required for the ConvertToSubClassCode to work properly
35// so RTTI for casting is available in the whole module.
36% ModuleCode
43% End
44// clang-format on
45#endif
46
47
53 class CORE_EXPORT QgsClassificationRange
54{
55 public:
57 QgsClassificationRange( const QString &label, double lowerBound, double upperBound )
58 : mLabel( label )
59 , mLowerBound( lowerBound )
60 , mUpperBound( upperBound )
61 {}
62
63 double lowerBound() const { return mLowerBound; }
65 double upperBound() const { return mUpperBound; }
66
68 QString label() const { return mLabel; }
69
70 // clang-format off
71#ifdef SIP_RUN
72 SIP_PYOBJECT __repr__();
73 % MethodCode
74 QString str = u"<QgsClassificationRange: '%1'>"_s.arg( sipCpp->label() );
75 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
76 % End
77#endif
78
79 private:
80 // clang-format on
81
82 QString mLabel;
83 double mLowerBound;
84 double mUpperBound;
85};
86
87
95{
96#ifdef SIP_RUN
98 if ( dynamic_cast<QgsClassificationEqualInterval *>( sipCpp ) )
99 sipType = sipType_QgsClassificationEqualInterval;
100 else if ( dynamic_cast<QgsClassificationJenks *>( sipCpp ) )
101 sipType = sipType_QgsClassificationJenks;
102 else if ( dynamic_cast<QgsClassificationPrettyBreaks *>( sipCpp ) )
103 sipType = sipType_QgsClassificationPrettyBreaks;
104 else if ( dynamic_cast<QgsClassificationQuantile *>( sipCpp ) )
105 sipType = sipType_QgsClassificationQuantile;
106 else if ( dynamic_cast<QgsClassificationStandardDeviation *>( sipCpp ) )
107 sipType = sipType_QgsClassificationStandardDeviation;
108 else if ( dynamic_cast<QgsClassificationFixedInterval *>( sipCpp ) )
109 sipType = sipType_QgsClassificationFixedInterval;
110 else
111 sipType = 0;
112 SIP_END
113#endif
114
115 public:
125
126
127
134
140 explicit QgsClassificationMethod( MethodProperties properties = NoFlag, int codeComplexity = 1 );
141
142 virtual ~QgsClassificationMethod();
143
148 virtual std::unique_ptr< QgsClassificationMethod > clone() const = 0;
149
151 virtual QString name() const = 0;
152
154 virtual QString id() const = 0;
155
157 virtual QIcon icon() const { return QIcon(); }
158
165
169 virtual QString labelForRange( double lowerValue, double upperValue, ClassPosition position = Inner ) const;
170
171
173 virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
174 {
175 Q_UNUSED( element );
176 Q_UNUSED( context )
177 }
178
179 virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context )
180 {
181 Q_UNUSED( element );
182 Q_UNUSED( context )
183 }
184
189 virtual bool valuesRequired() const { return true; }
190
191
192 // *******************
193 // non-virtual methods
194
196 int codeComplexity() const { return mCodeComplexity; }
197
201 bool symmetricModeAvailable() const { return mFlags.testFlag( SymmetricModeAvailable ); }
202
206 bool symmetricModeEnabled() const { return symmetricModeAvailable() && mSymmetricEnabled; }
207
211 double symmetryPoint() const { return mSymmetryPoint; }
212
217 bool symmetryAstride() const { return mSymmetryAstride; }
218
226 void setSymmetricMode( bool enabled, double symmetryPoint = 0, bool symmetryAstride = false );
227
228 // Label properties
230 QString labelFormat() const { return mLabelFormat; }
232 void setLabelFormat( const QString &format ) { mLabelFormat = format; }
234 int labelPrecision() const { return mLabelPrecision; }
236 void setLabelPrecision( int labelPrecision );
238 bool labelTrimTrailingZeroes() const { return mLabelTrimTrailingZeroes; }
240 void setLabelTrimTrailingZeroes( bool trimTrailingZeroes ) { mLabelTrimTrailingZeroes = trimTrailingZeroes; }
241
243 static QList<double> rangesToBreaks( const QList<QgsClassificationRange> &classes );
244
253 Q_DECL_DEPRECATED QList<QgsClassificationRange> classes( const QgsVectorLayer *layer, const QString &expression, int nclasses ) SIP_DEPRECATED;
254
267 QList<QgsClassificationRange> classesV2( const QgsVectorLayer *layer, const QString &expression, int nclasses, QString &error SIP_OUT );
268
274 QList<QgsClassificationRange> classes( const QList<double> &values, int nclasses );
275
283 QList<QgsClassificationRange> classes( double minimum, double maximum, int nclasses );
284
290 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
291
297 static std::unique_ptr< QgsClassificationMethod > create( const QDomElement &element, const QgsReadWriteContext &context );
298
306 static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride );
307
311 QString labelForRange( const QgsRendererRange &range, ClassPosition position = Inner ) const;
312
317 const QgsProcessingParameterDefinition *parameterDefinition( const QString &parameterName ) const;
318
324
329 void setParameterValues( const QVariantMap &values );
330
336 QVariantMap parameterValues() const { return mParameterValues; }
337
338 static const int MAX_PRECISION;
339 static const int MIN_PRECISION;
340
341 protected:
343 void copyBase( QgsClassificationMethod *c ) const;
344
346 QString formatNumber( double value ) const;
347
356
357 private:
363 virtual QList<double> calculateBreaks( double &minimum, double &maximum, const QList<double> &values, int nclasses, QString &error ) = 0;
364
366 virtual QString valueToLabel( double value ) const { return formatNumber( value ); }
367
369 QList<QgsClassificationRange> breaksToClasses( const QList<double> &breaks ) const;
370
371 // implementation properties (set by initialization)
372 MethodProperties mFlags = MethodProperties();
373 int mCodeComplexity = 1;
374
375 // parameters (set by setters)
376 // if some are added here, they should be handled in the clone method
377 bool mSymmetricEnabled = false;
378 double mSymmetryPoint = 0;
379 bool mSymmetryAstride = false;
380 int mLabelPrecision = 4;
381 bool mLabelTrimTrailingZeroes = true;
382 QString mLabelFormat;
383
384 // values used to manage number formatting - precision and trailing zeroes
385 double mLabelNumberScale = 1.0;
386 QString mLabelNumberSuffix;
387
390 QVariantMap mParameterValues;
391};
392
394
395#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:198
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_ABSTRACT
Definition qgis_sip.h:220
#define SIP_INOUT
Definition qgis_sip.h:78
#define SIP_END
Definition qgis_sip.h:215
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)