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 )
64 if ( values.isEmpty() )
65 return QList<double>();
69 return QList<double>() << maximum;
72 if ( nclasses >= values.size() )
77 QVector<double> sample;
78 QVector<double> sorted;
81 if ( values.size() > mMaximumSize )
87 sample.resize( std::max( mMaximumSize,
static_cast<int>( values.size() ) / 10 ) );
89 QgsDebugMsgLevel( QStringLiteral(
"natural breaks (jenks) sample size: %1" ).arg( sample.size() ), 2 );
92 sample[ 0 ] = minimum;
93 sample[ 1 ] = maximum;
95 sorted = values.toVector();
96 std::sort( sorted.begin(), sorted.end() );
104 for (
int i = 1; i < sorted.size() - 2; i++ )
106 if ( ( i * ( mMaximumSize - 2 ) / ( sorted.size() - 2 ) ) > j )
109 sample[ j + 2 ] = sorted[ i ];
115 sample = values.toVector();
118 const int n = sample.size();
121 std::sort( sample.begin(), sample.end() );
123 QVector< QVector<int> > matrixOne( n + 1 );
124 QVector< QVector<double> > matrixTwo( n + 1 );
126 for (
int i = 0; i <= n; i++ )
128 matrixOne[i].resize( nclasses + 1 );
129 matrixTwo[i].resize( nclasses + 1 );
132 for (
int i = 1; i <= nclasses; i++ )
136 matrixTwo[0][i] = 0.0;
137 for (
int j = 2; j <= n; j++ )
139 matrixTwo[j][i] = std::numeric_limits<double>::max();
143 for (
int l = 2; l <= n; l++ )
151 for (
int m = 1; m <= l; m++ )
153 const int i3 = l - m + 1;
155 const double val = sample[ i3 - 1 ];
161 v = s2 - ( s1 * s1 ) /
static_cast< double >( w );
162 const int i4 = i3 - 1;
165 for (
int j = 2; j <= nclasses; j++ )
167 if ( matrixTwo[l][j] >= v + matrixTwo[i4][j - 1] )
169 matrixOne[l][j] = i4;
170 matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
179 QVector<double> breaks( nclasses );
180 breaks[nclasses - 1] = sample[n - 1];
182 for (
int j = nclasses, k = n; j >= 2; j-- )
184 const int id = matrixOne[k][j] - 1;
185 breaks[j - 2] = sample[
id];
186 k = matrixOne[k][j] - 1;
189 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)