22#include <QRandomGenerator>
32 return QObject::tr(
"Natural Breaks (Jenks)" );
37 return QStringLiteral(
"Jenks" );
42 auto c = std::make_unique< QgsClassificationJenks >();
53QList<double> QgsClassificationJenks::calculateBreaks(
double &minimum,
double &maximum,
54 const QList<double> &values,
int nclasses, QString &error )
67 if ( values.isEmpty() )
68 return QList<double>();
72 return QList<double>() << maximum;
75 if ( nclasses >= values.size() )
80 QVector<double> sample;
81 QVector<double> sorted;
84 if ( values.size() > mMaximumSize )
90 sample.resize( std::max( mMaximumSize,
static_cast<int>( values.size() ) / 10 ) );
92 QgsDebugMsgLevel( QStringLiteral(
"natural breaks (jenks) sample size: %1" ).arg( sample.size() ), 2 );
95 sample[ 0 ] = minimum;
96 sample[ 1 ] = maximum;
98 sorted = values.toVector();
99 std::sort( sorted.begin(), sorted.end() );
107 for (
int i = 1; i < sorted.size() - 2; i++ )
109 if ( ( i * ( mMaximumSize - 2 ) / ( sorted.size() - 2 ) ) > j )
112 sample[ j + 2 ] = sorted[ i ];
118 sample = values.toVector();
121 const int n = sample.size();
124 std::sort( sample.begin(), sample.end() );
126 QVector< QVector<int> > matrixOne( n + 1 );
127 QVector< QVector<double> > matrixTwo( n + 1 );
129 for (
int i = 0; i <= n; i++ )
131 matrixOne[i].resize( nclasses + 1 );
132 matrixTwo[i].resize( nclasses + 1 );
135 for (
int i = 1; i <= nclasses; i++ )
139 matrixTwo[0][i] = 0.0;
140 for (
int j = 2; j <= n; j++ )
142 matrixTwo[j][i] = std::numeric_limits<double>::max();
146 for (
int l = 2; l <= n; l++ )
154 for (
int m = 1; m <= l; m++ )
156 const int i3 = l - m + 1;
158 const double val = sample[ i3 - 1 ];
164 v = s2 - ( s1 * s1 ) /
static_cast< double >( w );
165 const int i4 = i3 - 1;
168 for (
int j = 2; j <= nclasses; j++ )
170 if ( matrixTwo[l][j] >= v + matrixTwo[i4][j - 1] )
172 matrixOne[l][j] = i4;
173 matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
182 QVector<double> breaks( nclasses );
183 breaks[nclasses - 1] = sample[n - 1];
185 for (
int j = nclasses, k = n; j >= 2; j-- )
187 const int id = matrixOne[k][j] - 1;
188 breaks[j - 2] = sample[
id];
189 k = matrixOne[k][j] - 1;
192 return breaks.toList();
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QIcon icon() const override
The icon of the method.
QString name() const override
The readable and translate name of the method.
std::unique_ptr< 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.
QgsClassificationMethod(MethodProperties properties=NoFlag, int codeComplexity=1)
Creates a classification method.
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)