QGIS API Documentation 3.99.0-Master (d270888f95f)
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
17
18#include "qgsapplication.h"
19
20#include <QObject>
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
25const QString QgsClassificationEqualInterval::METHOD_ID = u"EqualInterval"_s;
26
31
33{
34 return QObject::tr( "Equal Interval" );
35}
36
38{
39 return METHOD_ID;
40}
41
42QList<double> QgsClassificationEqualInterval::calculateBreaks( double &minimum, double &maximum,
43 const QList<double> &values, int nclasses, QString &error )
44{
45 Q_UNUSED( values )
46 Q_UNUSED( error )
47
48 // Equal interval algorithm
49 // Returns breaks based on dividing the range ('minimum' to 'maximum') into 'classes' parts.
50 QList<double> breaks;
51 if ( !symmetricModeEnabled() ) // normal mode
52 {
53 const double step = ( maximum - minimum ) / nclasses;
54
55 double value = minimum;
56 breaks.reserve( nclasses );
57 for ( int i = 0; i < nclasses; i++ )
58 {
59 value += step;
60 breaks << value;
61 }
62 // floating point arithmetic is not precise:
63 // set the last break to be exactly maximum so we do not miss it
64 breaks[nclasses - 1] = maximum;
65 }
66 else // symmetric mode
67 {
68 const double distBelowSymmetricValue = std::abs( minimum - symmetryPoint() );
69 const double distAboveSymmetricValue = std::abs( maximum - symmetryPoint() ) ;
70
71 if ( symmetryAstride() )
72 {
73 if ( nclasses % 2 == 0 ) // we want odd number of classes
74 ++nclasses;
75 }
76 else
77 {
78 if ( nclasses % 2 == 1 ) // we want even number of classes
79 ++nclasses;
80 }
81 const double step = 2 * std::min( distBelowSymmetricValue, distAboveSymmetricValue ) / nclasses;
82
83 breaks.reserve( nclasses );
84 double value = ( distBelowSymmetricValue < distAboveSymmetricValue ) ? minimum : maximum - nclasses * step;
85
86 for ( int i = 0; i < nclasses; i++ )
87 {
88 value += step;
89 breaks << value;
90 }
91 breaks[nclasses - 1] = maximum;
92 }
93
94 return breaks;
95}
96
97
98std::unique_ptr< QgsClassificationMethod > QgsClassificationEqualInterval::clone() const
99{
100 auto c = std::make_unique< QgsClassificationEqualInterval >();
101 copyBase( c.get() );
102 return c;
103}
104
106{
107 return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualInterval.svg" );
108}
109
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
std::unique_ptr< 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.
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...
QgsClassificationMethod(MethodProperties properties=NoFlag, int codeComplexity=1)
Creates a classification method.
@ SymmetricModeAvailable
This allows using symmetric classification.
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