QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
qgsclassificationlogarithmic.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsclassificationlogarithmic.h
3 ---------------------
4 begin : September 2019
5 copyright : (C) 2019 by Denis Rouzaud
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#include <QObject>
17
19#include "qgssymbollayerutils.h"
20#include "qgsapplication.h"
22
23
25 : QgsClassificationMethod( NoFlag, 0 )
26{
27 QgsProcessingParameterEnum *param = new QgsProcessingParameterEnum( QStringLiteral( "ZERO_NEG_VALUES_HANDLE" ), QObject::tr( "Handling of 0 or negative values" ), QStringList() << QObject::tr( "no handling (faster)" ) << QObject::tr( "discard (slower)" ) << QObject::tr( "prepend range (slower)" ), false, 0 );
28 addParameter( param );
29}
30
31
38
40{
41 return QObject::tr( "Logarithmic Scale" );
42}
43
45{
46 return QStringLiteral( "Logarithmic" );
47}
48
50{
51 return QgsApplication::getThemeIcon( "classification_methods/mClassificationLogarithmic.svg" );
52}
53
54QList<double> QgsClassificationLogarithmic::calculateBreaks( double &minimum, double &maximum, const QList<double> &values, int nclasses, QString &error )
55{
56 Q_UNUSED( error )
57 const QgsProcessingContext context;
58 const QgsProcessingParameterDefinition *def = parameterDefinition( QStringLiteral( "ZERO_NEG_VALUES_HANDLE" ) );
60
61 double positiveMinimum = std::numeric_limits<double>::max();
62 if ( nvh != NegativeValueHandling::NoHandling && minimum <= 0 )
63 {
64 Q_ASSERT( values.count() );
65 if ( maximum > 0 )
66 {
67 for ( int i = 0; i < values.count(); i++ )
68 {
69 if ( values.at( i ) > 0 )
70 positiveMinimum = std::min( positiveMinimum, values.at( i ) );
71 }
72 }
73 if ( positiveMinimum == std::numeric_limits<double>::max() )
74 {
75 // there is no usable values
77 return QList<double>( {0} );
78 else
79 return QList<double>();
80 }
81 }
82
83 QList<double> breaks;
84
85 if ( positiveMinimum != std::numeric_limits<double>::max() )
86 {
88 breaks << std::floor( std::log10( positiveMinimum ) );
89 else if ( nvh == NegativeValueHandling::Discard )
90 minimum = positiveMinimum; // the minimum gets updated
91 }
92 else
93 {
94 positiveMinimum = minimum;
95 }
96
97 // get the min/max in log10 scale
98 const double actualLogMin { std::log10( positiveMinimum ) };
99 double logMin = std::floor( actualLogMin );
100 const double logMax = std::ceil( std::log10( maximum ) );
101
102 // calculate pretty breaks
103 QList<double> prettyBreaks { QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses ) };
104
105 // If case the first class greater than the actual log min increase the minimum log
106 while ( ! prettyBreaks.isEmpty() && prettyBreaks.first() < actualLogMin )
107 {
108 logMin += 1.0;
109 prettyBreaks = QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses );
110 }
111
112 breaks.append( prettyBreaks );
113
114 // create the value
115 for ( int i = 0; i < breaks.count(); i++ )
116 {
117 breaks[i] = std::pow( 10, breaks.at( i ) );
118 }
119
120 return breaks;
121}
122
123QString QgsClassificationLogarithmic::valueToLabel( double value ) const
124{
125 if ( value <= 0 )
126 {
127 return QLocale().toString( value );
128 }
129 else
130 {
131 if ( std::isnan( value ) )
132 {
133 return QObject::tr( "invalid (0 or negative values in the data)" );
134 }
135 else
136 {
137 return QString( QStringLiteral( "10^%L1" ) ).arg( std::log10( value ) );
138 }
139 }
140}
141
142QString QgsClassificationLogarithmic::labelForRange( double lowerValue, double upperValue, QgsClassificationMethod::ClassPosition position ) const
143{
144 QString lowerLabel;
145 const QString upperLabel = valueToLabel( upperValue );
146
147 switch ( position )
148 {
149 case LowerBound:
150 lowerLabel = formatNumber( lowerValue ); // avoid to have float exponent for the minimum value
151 break;
152 case Inner:
153 case UpperBound:
154 lowerLabel = valueToLabel( lowerValue );
155 break;
156 }
157
158 return labelFormat().arg( lowerLabel, upperLabel );
159}
160
162{
163 const QgsProcessingContext context;
164 const QgsProcessingParameterDefinition *def = parameterDefinition( QStringLiteral( "ZERO_NEG_VALUES_HANDLE" ) );
166
168}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Implementation of a logarithmic scale method.
bool valuesRequired() const override
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
NegativeValueHandling
Handling of negative and 0 values in the method.
@ PrependBreak
Prepend an extra break to include negative values - this will require all values.
@ Discard
Negative values are discarded - this will require all values.
QString labelForRange(double lowerValue, double upperValue, ClassPosition position) const override
Returns the label for a range.
QString id() const override
The id of the method as saved in the project, must be unique in registry.
QString name() const override
The readable and translate name of the method.
QgsClassificationMethod * clone() const override
Returns a clone of the method.
QIcon icon() const override
The icon of the method.
QgsClassificationMethod is an abstract class for implementations of classification methods.
QVariantMap parameterValues() const
Returns the values of the processing 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.
void addParameter(QgsProcessingParameterDefinition *definition)
Add a parameter to the method.
QString labelFormat() const
Returns the format of the label for the classes.
const QgsProcessingParameterDefinition * parameterDefinition(const QString &parameterName) const
Returns the parameter from its name.
QString formatNumber(double value) const
Format the number according to label properties.
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about 'classes' equally spaced round values which cover the range of values fr...
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