QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
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