QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
22const QString QgsClassificationEqualInterval::METHOD_ID = QStringLiteral( "EqualInterval" );
23
28
30{
31 return QObject::tr( "Equal Interval" );
32}
33
35{
36 return METHOD_ID;
37}
38
39QList<double> QgsClassificationEqualInterval::calculateBreaks( double &minimum, double &maximum,
40 const QList<double> &values, int nclasses, QString &error )
41{
42 Q_UNUSED( values )
43 Q_UNUSED( error )
44
45 // Equal interval algorithm
46 // Returns breaks based on dividing the range ('minimum' to 'maximum') into 'classes' parts.
47 QList<double> breaks;
48 if ( !symmetricModeEnabled() ) // normal mode
49 {
50 const double step = ( maximum - minimum ) / nclasses;
51
52 double value = minimum;
53 breaks.reserve( nclasses );
54 for ( int i = 0; i < nclasses; i++ )
55 {
56 value += step;
57 breaks << value;
58 }
59 // floating point arithmetic is not precise:
60 // set the last break to be exactly maximum so we do not miss it
61 breaks[nclasses - 1] = maximum;
62 }
63 else // symmetric mode
64 {
65 const double distBelowSymmetricValue = std::abs( minimum - symmetryPoint() );
66 const double distAboveSymmetricValue = std::abs( maximum - symmetryPoint() ) ;
67
68 if ( symmetryAstride() )
69 {
70 if ( nclasses % 2 == 0 ) // we want odd number of classes
71 ++nclasses;
72 }
73 else
74 {
75 if ( nclasses % 2 == 1 ) // we want even number of classes
76 ++nclasses;
77 }
78 const double step = 2 * std::min( distBelowSymmetricValue, distAboveSymmetricValue ) / nclasses;
79
80 breaks.reserve( nclasses );
81 double value = ( distBelowSymmetricValue < distAboveSymmetricValue ) ? minimum : maximum - nclasses * step;
82
83 for ( int i = 0; i < nclasses; i++ )
84 {
85 value += step;
86 breaks << value;
87 }
88 breaks[nclasses - 1] = maximum;
89 }
90
91 return breaks;
92}
93
94
95std::unique_ptr< QgsClassificationMethod > QgsClassificationEqualInterval::clone() const
96{
97 auto c = std::make_unique< QgsClassificationEqualInterval >();
98 copyBase( c.get() );
99 return c;
100}
101
103{
104 return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualInterval.svg" );
105}
106
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