QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 : denis@opengis.ch
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
24
26{
27 return QObject::tr( "Equal Count (Quantile)" );
28}
29
31{
32 return QStringLiteral( "Quantile" );
33}
34
41
43{
44 return QgsApplication::getThemeIcon( "classification_methods/mClassificationEqualCount.svg" );
45}
46
47
48QList<double> QgsClassificationQuantile::calculateBreaks( double &minimum, double &maximum,
49 const QList<double> &values, int nclasses, QString &error )
50{
51 Q_UNUSED( minimum )
52 Q_UNUSED( maximum )
53 Q_UNUSED( error )
54
55 // q-th quantile of a data set:
56 // value where q fraction of data is below and (1-q) fraction is above this value
57 // Xq = (1 - r) * X_NI1 + r * X_NI2
58 // NI1 = (int) (q * (n+1))
59 // NI2 = NI1 + 1
60 // r = q * (n+1) - (int) (q * (n+1))
61 // (indices of X: 1...n)
62
63 // sort the values first
64 QList<double> _values = values;
65 std::sort( _values.begin(), _values.end() );
66
67 QList<double> breaks;
68
69 // If there are no values to process: bail out
70 if ( _values.isEmpty() )
71 return QList<double>();
72
73 const int n = _values.count();
74 double Xq = n > 0 ? _values[0] : 0.0;
75
76 breaks.reserve( nclasses );
77 for ( int i = 1; i < nclasses; i++ )
78 {
79 if ( n > 1 )
80 {
81 const double q = i / static_cast< double >( nclasses );
82 const double a = q * ( n - 1 );
83 const int aa = static_cast< int >( a );
84
85 const double r = a - aa;
86 Xq = ( 1 - r ) * _values[aa] + r * _values[aa + 1];
87 }
88 breaks.append( Xq );
89 }
90
91 breaks.append( _values[ n - 1 ] );
92
93 return breaks;
94}
95
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsClassificationMethod is an abstract class for implementations of classification methods.
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
QgsClassificationQuantile is an implementation of QgsClassificationMethod based on quantiles.
QgsClassificationMethod * clone() const override
Returns a clone 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.
QString name() const override
The readable and translate name of the method.
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