27#include <QRegularExpression>
30using namespace Qt::StringLiterals;
39 values.reserve(
classes.count() );
40 for (
int i = 0 ; i <
classes.count(); i++ )
41 values <<
classes.at( i ).upperBound();
46 : mFlags( properties )
48 , mLabelFormat( u
"%1 - %2"_s )
54 qDeleteAll( mParameters );
59 c->setSymmetricMode( mSymmetricEnabled, mSymmetryPoint, mSymmetryAstride );
60 c->setLabelFormat( mLabelFormat );
61 c->setLabelPrecision( mLabelPrecision );
62 c->setLabelTrimTrailingZeroes( mLabelTrimTrailingZeroes );
63 c->setParameterValues( mParameterValues );
68 const QString methodId = element.attribute( u
"id"_s );
72 QDomElement symmetricModeElem = element.firstChildElement( u
"symmetricMode"_s );
73 if ( !symmetricModeElem.isNull() )
75 bool symmetricEnabled = symmetricModeElem.attribute( u
"enabled"_s ).toInt() == 1;
76 double symmetricPoint = symmetricModeElem.attribute( u
"symmetrypoint"_s ).toDouble();
77 bool astride = symmetricModeElem.attribute( u
"astride"_s ).toInt() == 1;
78 method->setSymmetricMode( symmetricEnabled, symmetricPoint, astride );
82 QDomElement labelFormatElem = element.firstChildElement( u
"labelFormat"_s );
83 if ( !labelFormatElem.isNull() )
85 QString format = labelFormatElem.attribute( u
"format"_s,
"%1" + u
" - "_s +
"%2" );
86 int precision = labelFormatElem.attribute( u
"labelprecision"_s, u
"4"_s ).toInt();
87 bool trimTrailingZeroes = labelFormatElem.attribute( u
"trimtrailingzeroes"_s, u
"false"_s ) ==
"true"_L1;
88 method->setLabelFormat( format );
89 method->setLabelPrecision( precision );
90 method->setLabelTrimTrailingZeroes( trimTrailingZeroes );
94 QDomElement parametersElem = element.firstChildElement( u
"parameters"_s );
99 QDomElement extraElem = element.firstChildElement( u
"extraInformation"_s );
100 if ( !extraElem.isNull() )
101 method->readXml( extraElem, context );
108 QDomElement methodElem = doc.createElement( u
"classificationMethod"_s );
110 methodElem.setAttribute( u
"id"_s,
id() );
113 QDomElement symmetricModeElem = doc.createElement( u
"symmetricMode"_s );
115 symmetricModeElem.setAttribute( u
"symmetrypoint"_s,
symmetryPoint() );
116 symmetricModeElem.setAttribute( u
"astride"_s, mSymmetryAstride ? 1 : 0 );
117 methodElem.appendChild( symmetricModeElem );
120 QDomElement labelFormatElem = doc.createElement( u
"labelFormat"_s );
121 labelFormatElem.setAttribute( u
"format"_s,
labelFormat() );
122 labelFormatElem.setAttribute( u
"labelprecision"_s,
labelPrecision() );
124 methodElem.appendChild( labelFormatElem );
127 QDomElement parametersElem = doc.createElement( u
"parameters"_s );
129 methodElem.appendChild( parametersElem );
132 QDomElement extraElem = doc.createElement( u
"extraInformation"_s );
134 methodElem.appendChild( extraElem );
142 mSymmetricEnabled = enabled;
144 mSymmetryAstride = astride;
151 mLabelPrecision = precision;
152 mLabelNumberScale = 1.0;
153 mLabelNumberSuffix.clear();
154 while ( precision < 0 )
157 mLabelNumberScale /= 10.0;
158 mLabelNumberSuffix.append(
'0' );
164 static const QRegularExpression RE_TRAILING_ZEROES = QRegularExpression(
"[.,]?0*$" );
165 static const QRegularExpression RE_NEGATIVE_ZERO = QRegularExpression(
"^\\-0(?:[.,]0*)?$" );
166 if ( mLabelPrecision > 0 )
168 QString valueStr = QLocale().toString( value,
'f', mLabelPrecision );
169 if ( mLabelTrimTrailingZeroes )
170 valueStr = valueStr.remove( RE_TRAILING_ZEROES );
171 if ( RE_NEGATIVE_ZERO.match( valueStr ).hasMatch() )
172 valueStr = valueStr.mid( 1 );
177 QString valueStr = QLocale().toString( value * mLabelNumberScale,
'f', 0 );
178 if ( valueStr ==
"-0"_L1 )
180 if ( valueStr !=
"0"_L1 )
181 valueStr = valueStr + mLabelNumberSuffix;
188 mParameters.append( definition );
195 if ( def->name() == parameterName )
204 mParameterValues = values;
205 for (
auto it = mParameterValues.constBegin(); it != mParameterValues.constEnd(); ++it )
217 return classesV2( layer, expression, nclasses, error );
222 if ( expression.isEmpty() )
223 return QList<QgsClassificationRange>();
228 QList<double> values;
239 if ( !ok || values.isEmpty() )
240 return QList<QgsClassificationRange>();
242 auto result = std::minmax_element( values.begin(), values.end() );
243 minimum = *result.first;
244 maximum = *result.second;
251 minimum = minVal.toDouble();
252 maximum = maxVal.toDouble();
256 QList<double> breaks = calculateBreaks( minimum, maximum, values, nclasses, error );
257 breaks.insert( 0, minimum );
259 return breaksToClasses( breaks );
264 auto result = std::minmax_element( values.begin(), values.end() );
265 double minimum = *result.first;
266 double maximum = *result.second;
270 QList<double> breaks = calculateBreaks( minimum, maximum, values, nclasses, error );
273 breaks.insert( 0, minimum );
275 return breaksToClasses( breaks );
282 QgsDebugError( u
"The classification method %1 tries to calculate classes without values while they are required."_s.arg(
name() ) );
287 QList<double> breaks = calculateBreaks( minimum, maximum, QList<double>(), nclasses, error );
290 breaks.insert( 0, minimum );
292 return breaksToClasses( breaks );
295QList<QgsClassificationRange> QgsClassificationMethod::breaksToClasses(
const QList<double> &breaks )
const
297 QList<QgsClassificationRange>
classes;
299 for (
int i = 1; i < breaks.count(); i++ )
302 const double lowerValue = breaks.at( i - 1 );
303 const double upperValue = breaks.at( i );
308 else if ( i == breaks.count() - 1 )
311 QString label =
labelForRange( lowerValue, upperValue, pos );
325 if ( breaks.count() < 2 )
328 std::sort( breaks.begin(), breaks.end() );
330 double distBelowSymmetricValue = std::fabs( breaks[0] -
symmetryPoint );
331 double distAboveSymmetricValue = std::fabs( breaks[ breaks.size() - 2 ] -
symmetryPoint ) ;
332 double absMin = std::min( distAboveSymmetricValue, distBelowSymmetricValue );
335 for (
int i = 0; i <= breaks.size() - 2; ++i )
338 if ( std::fabs( breaks.at( i ) -
symmetryPoint ) >= ( absMin - std::fabs( breaks[0] - breaks[1] ) / 100. ) )
340 breaks.removeAt( i );
360 const QString lowerLabel = valueToLabel( lowerValue );
361 const QString upperLabel = valueToLabel( upperValue );
363 return labelFormat().replace(
"%1"_L1, lowerLabel ).replace(
"%2"_L1, upperLabel );
static QgsClassificationMethodRegistry * classificationMethodRegistry()
Returns the application's classification methods registry, used in graduated renderer.
std::unique_ptr< QgsClassificationMethod > method(const QString &id)
Returns a new instance of the method for the given id.
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
int codeComplexity() const
Code complexity as the exponent in Big O notation.
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
static void makeBreaksSymmetric(QList< double > &breaks, double symmetryPoint, bool astride)
Remove the breaks that are above the existing opposite sign classes to keep colors symmetrically bala...
static std::unique_ptr< QgsClassificationMethod > create(const QDomElement &element, const QgsReadWriteContext &context)
Reads the DOM element and return a new classification method from it.
QFlags< MethodProperty > MethodProperties
Q_DECL_DEPRECATED QList< QgsClassificationRange > classes(const QgsVectorLayer *layer, const QString &expression, int nclasses)
This will calculate the classes for a given layer to define the classes.
int labelPrecision() const
Returns the precision for the formatting of the labels.
QVariantMap parameterValues() const
Returns the values of the processing parameters.
void setSymmetricMode(bool enabled, double symmetryPoint=0, bool symmetryAstride=false)
Defines if the symmetric mode is enables and configures its parameters.
ClassPosition
Defines the class position.
@ LowerBound
The class is at the lower bound.
@ UpperBound
The class is at the upper bound.
@ Inner
The class is not at a bound.
static const int MIN_PRECISION
void addParameter(QgsProcessingParameterDefinition *definition)
Add a parameter to the method.
static QList< double > rangesToBreaks(const QList< QgsClassificationRange > &classes)
Transforms a list of classes to a list of breaks.
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes extra information about the method.
QString labelFormat() const
Returns the format of the label for the classes.
virtual bool valuesRequired() const
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
void setParameterValues(const QVariantMap &values)
Defines the values of the additional parameters.
virtual QString name() const =0
The readable and translate name of the method.
QgsClassificationMethod(MethodProperties properties=NoFlag, int codeComplexity=1)
Creates a classification method.
const QgsProcessingParameterDefinition * parameterDefinition(const QString ¶meterName) const
Returns the parameter from its name.
QString formatNumber(double value) const
Format the number according to label properties.
static const int MAX_PRECISION
bool labelTrimTrailingZeroes() const
Returns if the trailing 0 are trimmed in the label.
void setLabelPrecision(int labelPrecision)
Defines the precision for the formatting of the labels.
QList< QgsClassificationRange > classesV2(const QgsVectorLayer *layer, const QString &expression, int nclasses, QString &error)
This will calculate the classes for a given layer to define the classes.
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation).
virtual QString labelForRange(double lowerValue, double upperValue, ClassPosition position=Inner) const
Returns the label for a range.
virtual ~QgsClassificationMethod()
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const
Saves the method to a DOM element and return it.
Contains the information about a classification range.
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Base class for the definition of processing parameters.
A container for the context for various read/write operations on objects.
Represents a value range for a QgsGraduatedSymbolRenderer.
double upperValue() const
Returns the upper bound of the range.
double lowerValue() const
Returns the lower bound of the range.
static QList< double > getDoubleValues(const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, int *nullCount=nullptr, QgsFeedback *feedback=nullptr)
Fetches all double values from a specified field name or expression.
Represents a vector layer which manages a vector based dataset.
void minimumAndMaximumValue(int index, QVariant &minimum, QVariant &maximum) const
Calculates both the minimum and maximum value for an attribute column.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
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 QgsDebugError(str)