20#include <QRandomGenerator>
30 return QObject::tr(
"Natural Breaks (Jenks)" );
35 return QStringLiteral(
"Jenks" );
51QList<double> QgsClassificationJenks::calculateBreaks(
double &minimum,
double &maximum,
52 const QList<double> &values,
int nclasses, QString &error )
65 if ( values.isEmpty() )
66 return QList<double>();
70 return QList<double>() << maximum;
73 if ( nclasses >= values.size() )
78 QVector<double> sample;
79 QVector<double> sorted;
82 if ( values.size() > mMaximumSize )
88 sample.resize( std::max( mMaximumSize,
static_cast<int>( values.size() ) / 10 ) );
90 QgsDebugMsgLevel( QStringLiteral(
"natural breaks (jenks) sample size: %1" ).arg( sample.size() ), 2 );
93 sample[ 0 ] = minimum;
94 sample[ 1 ] = maximum;
96 sorted = values.toVector();
97 std::sort( sorted.begin(), sorted.end() );
105 for (
int i = 1; i < sorted.size() - 2; i++ )
107 if ( ( i * ( mMaximumSize - 2 ) / ( sorted.size() - 2 ) ) > j )
110 sample[ j + 2 ] = sorted[ i ];
116 sample = values.toVector();
119 const int n = sample.size();
122 std::sort( sample.begin(), sample.end() );
124 QVector< QVector<int> > matrixOne( n + 1 );
125 QVector< QVector<double> > matrixTwo( n + 1 );
127 for (
int i = 0; i <= n; i++ )
129 matrixOne[i].resize( nclasses + 1 );
130 matrixTwo[i].resize( nclasses + 1 );
133 for (
int i = 1; i <= nclasses; i++ )
137 matrixTwo[0][i] = 0.0;
138 for (
int j = 2; j <= n; j++ )
140 matrixTwo[j][i] = std::numeric_limits<double>::max();
144 for (
int l = 2; l <= n; l++ )
152 for (
int m = 1; m <= l; m++ )
154 const int i3 = l - m + 1;
156 const double val = sample[ i3 - 1 ];
162 v = s2 - ( s1 * s1 ) /
static_cast< double >( w );
163 const int i4 = i3 - 1;
166 for (
int j = 2; j <= nclasses; j++ )
168 if ( matrixTwo[l][j] >= v + matrixTwo[i4][j - 1] )
170 matrixOne[l][j] = i4;
171 matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
180 QVector<double> breaks( nclasses );
181 breaks[nclasses - 1] = sample[n - 1];
183 for (
int j = nclasses, k = n; j >= 2; j-- )
185 const int id = matrixOne[k][j] - 1;
186 breaks[j - 2] = sample[
id];
187 k = matrixOne[k][j] - 1;
190 return breaks.toList();
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsClassificationJenks is an implementation of QgsClassificationMethod for natural breaks based on Je...
QgsClassificationMethod * clone() const override
Returns a clone of the method.
QIcon icon() const override
The icon 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.
QgsClassificationMethod is an abstract class for implementations of classification methods.
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
#define QgsDebugMsgLevel(str, level)