QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
6  email : [email protected]
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 
21 const 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 
38 QList<double> QgsClassificationEqualInterval::calculateBreaks( double &minimum, double &maximum,
39  const QList<double> &values, int nclasses )
40 {
41  Q_UNUSED( values )
42 
43  // Equal interval algorithm
44  // Returns breaks based on dividing the range ('minimum' to 'maximum') into 'classes' parts.
45  QList<double> breaks;
46  if ( !symmetricModeEnabled() ) // normal mode
47  {
48  double step = ( maximum - minimum ) / nclasses;
49 
50  double value = minimum;
51  breaks.reserve( nclasses );
52  for ( int i = 0; i < nclasses; i++ )
53  {
54  value += step;
55  breaks << value;
56  }
57  // floating point arithmetic is not precise:
58  // set the last break to be exactly maximum so we do not miss it
59  breaks[nclasses - 1] = maximum;
60  }
61  else // symmetric mode
62  {
63  double distBelowSymmetricValue = std::abs( minimum - symmetryPoint() );
64  double distAboveSymmetricValue = std::abs( maximum - symmetryPoint() ) ;
65 
66  if ( symmetryAstride() )
67  {
68  if ( nclasses % 2 == 0 ) // we want odd number of classes
69  ++nclasses;
70  }
71  else
72  {
73  if ( nclasses % 2 == 1 ) // we want even number of classes
74  ++nclasses;
75  }
76  double step = 2 * std::min( distBelowSymmetricValue, distAboveSymmetricValue ) / nclasses;
77 
78  breaks.reserve( nclasses );
79  double value = ( distBelowSymmetricValue < distAboveSymmetricValue ) ? minimum : maximum - nclasses * step;
80 
81  for ( int i = 0; i < nclasses; i++ )
82  {
83  value += step;
84  breaks << value;
85  }
86  breaks[nclasses - 1] = maximum;
87  }
88 
89  return breaks;
90 }
91 
92 
94 {
96  copyBase( c );
97  return c;
98 }
99 
101 {
102  return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualInterval.svg" );
103 }
104 
QgsClassificationMethod
QgsClassificationMethod is an abstract class for implementations of classification methods.
Definition: qgsclassificationmethod.h:88
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsClassificationEqualInterval::QgsClassificationEqualInterval
QgsClassificationEqualInterval()
Definition: qgsclassificationequalinterval.cpp:23
QgsClassificationMethod::symmetryAstride
bool symmetryAstride() const
Returns if the symmetric mode is astride if true, it will remove the symmetry point break so that the...
Definition: qgsclassificationmethod.h:194
QgsClassificationEqualInterval::icon
QIcon icon() const override
The icon of the method.
Definition: qgsclassificationequalinterval.cpp:100
QgsClassificationEqualInterval
QgsClassificationEqualInterval is an implementation of QgsClassificationMethod for equal intervals.
Definition: qgsclassificationequalinterval.h:29
qgsapplication.h
QgsClassificationMethod::symmetryPoint
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
Definition: qgsclassificationmethod.h:188
QgsClassificationMethod::symmetricModeEnabled
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
Definition: qgsclassificationmethod.h:183
QgsClassificationMethod::copyBase
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
Definition: qgsclassificationmethod.cpp:52
qgsclassificationequalinterval.h
QgsClassificationEqualInterval::name
QString name() const override
The readable and translate name of the method.
Definition: qgsclassificationequalinterval.cpp:28
QgsClassificationEqualInterval::METHOD_ID
static const QString METHOD_ID
Definition: qgsclassificationequalinterval.h:40
QgsClassificationEqualInterval::id
QString id() const override
The id of the method as saved in the project, must be unique in registry.
Definition: qgsclassificationequalinterval.cpp:33
c
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
Definition: porting_processing.dox:1
QgsClassificationEqualInterval::clone
QgsClassificationMethod * clone() const override
Returns a clone of the method.
Definition: qgsclassificationequalinterval.cpp:93