QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsclassificationequalinterval.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsclassificationequalinterval.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#include <QObject>
17
19#include "qgsapplication.h"
20
21const QString QgsClassificationEqualInterval::METHOD_ID = QStringLiteral( "EqualInterval" );
22
24 : QgsClassificationMethod( SymmetricModeAvailable, 0 /*codeComplexity*/ )
25{
26}
27
29{
30 return QObject::tr( "Equal Interval" );
31}
32
34{
35 return METHOD_ID;
36}
37
38QList<double> QgsClassificationEqualInterval::calculateBreaks( double &minimum, double &maximum,
39 const QList<double> &values, int nclasses, QString &error )
40{
41 Q_UNUSED( values )
42 Q_UNUSED( error )
43
44 // Equal interval algorithm
45 // Returns breaks based on dividing the range ('minimum' to 'maximum') into 'classes' parts.
46 QList<double> breaks;
47 if ( !symmetricModeEnabled() ) // normal mode
48 {
49 const double step = ( maximum - minimum ) / nclasses;
50
51 double value = minimum;
52 breaks.reserve( nclasses );
53 for ( int i = 0; i < nclasses; i++ )
54 {
55 value += step;
56 breaks << value;
57 }
58 // floating point arithmetic is not precise:
59 // set the last break to be exactly maximum so we do not miss it
60 breaks[nclasses - 1] = maximum;
61 }
62 else // symmetric mode
63 {
64 const double distBelowSymmetricValue = std::abs( minimum - symmetryPoint() );
65 const double distAboveSymmetricValue = std::abs( maximum - symmetryPoint() ) ;
66
67 if ( symmetryAstride() )
68 {
69 if ( nclasses % 2 == 0 ) // we want odd number of classes
70 ++nclasses;
71 }
72 else
73 {
74 if ( nclasses % 2 == 1 ) // we want even number of classes
75 ++nclasses;
76 }
77 const double step = 2 * std::min( distBelowSymmetricValue, distAboveSymmetricValue ) / nclasses;
78
79 breaks.reserve( nclasses );
80 double value = ( distBelowSymmetricValue < distAboveSymmetricValue ) ? minimum : maximum - nclasses * step;
81
82 for ( int i = 0; i < nclasses; i++ )
83 {
84 value += step;
85 breaks << value;
86 }
87 breaks[nclasses - 1] = maximum;
88 }
89
90 return breaks;
91}
92
93
100
102{
103 return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualInterval.svg" );
104}
105
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
QgsClassificationMethod * clone() const override
Returns a clone of the method.
QString name() const override
The readable and translate name of the method.
QString id() const override
The id of the method as saved in the project, must be unique in registry.
QIcon icon() const override
The icon of the method.
QgsClassificationMethod is an abstract class for implementations of classification methods.
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
bool symmetryAstride() const
Returns if the symmetric mode is astride if true, it will remove the symmetry point break so that the...
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
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