QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsclassificationquantile.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsclassificationquantile.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 
18 #include "qgsapplication.h"
19 
22 {
23 }
24 
26 {
27  return QObject::tr( "Equal Count (Quantile)" );
28 }
29 
31 {
32  return QStringLiteral( "Quantile" );
33 }
34 
36 {
38  copyBase( c );
39  return c;
40 }
41 
43 {
44  return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualCount.svg" );
45 }
46 
47 
48 QList<double> QgsClassificationQuantile::calculateBreaks( double &minimum, double &maximum,
49  const QList<double> &values, int nclasses )
50 {
51  Q_UNUSED( minimum )
52  Q_UNUSED( maximum )
53 
54  // q-th quantile of a data set:
55  // value where q fraction of data is below and (1-q) fraction is above this value
56  // Xq = (1 - r) * X_NI1 + r * X_NI2
57  // NI1 = (int) (q * (n+1))
58  // NI2 = NI1 + 1
59  // r = q * (n+1) - (int) (q * (n+1))
60  // (indices of X: 1...n)
61 
62  // sort the values first
63  QList<double> _values = values;
64  std::sort( _values.begin(), _values.end() );
65 
66  QList<double> breaks;
67 
68  // If there are no values to process: bail out
69  if ( _values.isEmpty() )
70  return QList<double>();
71 
72  int n = _values.count();
73  double Xq = n > 0 ? _values[0] : 0.0;
74 
75  breaks.reserve( nclasses );
76  for ( int i = 1; i < nclasses; i++ )
77  {
78  if ( n > 1 )
79  {
80  double q = i / static_cast< double >( nclasses );
81  double a = q * ( n - 1 );
82  int aa = static_cast< int >( a );
83 
84  double r = a - aa;
85  Xq = ( 1 - r ) * _values[aa] + r * _values[aa + 1];
86  }
87  breaks.append( Xq );
88  }
89 
90  breaks.append( _values[ n - 1 ] );
91 
92  return breaks;
93 }
94 
QgsClassificationMethod
Definition: qgsclassificationmethod.h:87
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsClassificationQuantile::clone
QgsClassificationMethod * clone() const override
Returns a clone of the method.
Definition: qgsclassificationquantile.cpp:35
QgsClassificationQuantile::icon
QIcon icon() const override
The icon of the method.
Definition: qgsclassificationquantile.cpp:42
QgsClassificationQuantile
Definition: qgsclassificationquantile.h:29
qgsapplication.h
QgsClassificationMethod::copyBase
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
Definition: qgsclassificationmethod.cpp:52
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
QgsClassificationQuantile::QgsClassificationQuantile
QgsClassificationQuantile()
Definition: qgsclassificationquantile.cpp:20
QgsClassificationQuantile::name
QString name() const override
The readable and translate name of the method.
Definition: qgsclassificationquantile.cpp:25
qgsclassificationquantile.h
QgsClassificationQuantile::id
QString id() const override
The id of the method as saved in the project, must be unique in registry.
Definition: qgsclassificationquantile.cpp:30