QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
6  email : [email protected]
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"
21 #include "qgsprocessingcontext.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 
33 {
35  copyBase( c );
36  return c;
37 }
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 
54 QList<double> QgsClassificationLogarithmic::calculateBreaks( double &minimum, double &maximum, const QList<double> &values, int nclasses )
55 {
56  QgsProcessingContext context;
57  const QgsProcessingParameterDefinition *def = parameterDefinition( QStringLiteral( "ZERO_NEG_VALUES_HANDLE" ) );
59 
60  double positiveMinimum = std::numeric_limits<double>::max();
61  if ( nvh != NegativeValueHandling::NoHandling && minimum <= 0 )
62  {
63  Q_ASSERT( values.count() );
64  if ( maximum > 0 )
65  {
66  for ( int i = 0; i < values.count(); i++ )
67  {
68  if ( values.at( i ) > 0 )
69  positiveMinimum = std::min( positiveMinimum, values.at( i ) );
70  }
71  }
72  if ( positiveMinimum == std::numeric_limits<double>::max() )
73  {
74  // there is no usable values
75  if ( nvh == NegativeValueHandling::PrependBreak )
76  return QList<double>( {0} );
77  else
78  return QList<double>();
79  }
80  }
81 
82  QList<double> breaks;
83 
84  if ( positiveMinimum != std::numeric_limits<double>::max() )
85  {
86  if ( nvh == NegativeValueHandling::PrependBreak )
87  breaks << std::floor( std::log10( positiveMinimum ) );
88  else if ( nvh == NegativeValueHandling::Discard )
89  minimum = positiveMinimum; // the minimum gets updated
90  }
91  else
92  {
93  positiveMinimum = minimum;
94  }
95 
96  // get the min/max in log10 scale
97  double logMin = std::floor( std::log10( positiveMinimum ) );
98  double logMax = std::ceil( std::log10( maximum ) );
99 
100  // calculate pretty breaks
101  breaks.append( QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses ) );
102 
103  // create the value
104  for ( int i = 0; i < breaks.count(); i++ )
105  breaks[i] = std::pow( 10, breaks.at( i ) );
106 
107  return breaks;
108 }
109 
110 QString QgsClassificationLogarithmic::valueToLabel( double value ) const
111 {
112  if ( value <= 0 )
113  {
114  return QString::number( value );
115  }
116  else
117  {
118  if ( std::isnan( value ) )
119  {
120  return QObject::tr( "invalid (0 or negative values in the data)" );
121  }
122  else
123  {
124  return QString( QStringLiteral( "10^%1" ) ).arg( std::log10( value ) );
125  }
126  }
127 }
128 
129 QString QgsClassificationLogarithmic::labelForRange( double lowerValue, double upperValue, QgsClassificationMethod::ClassPosition position ) const
130 {
131  QString lowerLabel;
132  const QString upperLabel = valueToLabel( upperValue );
133 
134  switch ( position )
135  {
136  case LowerBound:
137  lowerLabel = formatNumber( lowerValue ); // avoid to have float exponent for the minimum value
138  break;
139  case Inner:
140  case UpperBound:
141  lowerLabel = valueToLabel( lowerValue );
142  break;
143  }
144 
145  return labelFormat().arg( lowerLabel ).arg( upperLabel );
146 }
147 
149 {
150  QgsProcessingContext context;
151  const QgsProcessingParameterDefinition *def = parameterDefinition( QStringLiteral( "ZERO_NEG_VALUES_HANDLE" ) );
153 
154  return nvh != NegativeValueHandling::NoHandling;
155 }
QgsClassificationMethod
QgsClassificationMethod is an abstract class for implementations of classification methods.
Definition: qgsclassificationmethod.h:88
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsClassificationLogarithmic::name
QString name() const override
The readable and translate name of the method.
Definition: qgsclassificationlogarithmic.cpp:39
QgsClassificationMethod::UpperBound
@ UpperBound
The class is at the upper bound.
Definition: qgsclassificationmethod.h:124
QgsClassificationLogarithmic::labelForRange
QString labelForRange(double lowerValue, double upperValue, ClassPosition position) const override
Returns the label for a range.
Definition: qgsclassificationlogarithmic.cpp:129
QgsClassificationMethod::formatNumber
QString formatNumber(double value) const
Format the number according to label properties.
Definition: qgsclassificationmethod.cpp:157
qgssymbollayerutils.h
qgsclassificationlogarithmic.h
QgsClassificationMethod::addParameter
void addParameter(QgsProcessingParameterDefinition *definition)
Add a parameter to the method.
Definition: qgsclassificationmethod.cpp:181
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:331
QgsClassificationMethod::parameterDefinition
const QgsProcessingParameterDefinition * parameterDefinition(const QString &parameterName) const
Returns the parameter from its name.
Definition: qgsclassificationmethod.cpp:186
qgsapplication.h
QgsClassificationMethod::LowerBound
@ LowerBound
The class is at the lower bound.
Definition: qgsclassificationmethod.h:122
QgsClassificationLogarithmic::id
QString id() const override
The id of the method as saved in the project, must be unique in registry.
Definition: qgsclassificationlogarithmic.cpp:44
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsClassificationLogarithmic::NegativeValueHandling
NegativeValueHandling
Handling of negative and 0 values in the method.
Definition: qgsclassificationlogarithmic.h:37
QgsSymbolLayerUtils::prettyBreaks
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...
Definition: qgssymbollayerutils.cpp:4266
QgsClassificationMethod::copyBase
void copyBase(QgsClassificationMethod *c) const
Copy the parameters (shall be used in clone implementation)
Definition: qgsclassificationmethod.cpp:52
QgsClassificationMethod::Inner
@ Inner
The class is not at a bound.
Definition: qgsclassificationmethod.h:123
QgsClassificationMethod::labelFormat
QString labelFormat() const
Returns the format of the label for the classes.
Definition: qgsclassificationmethod.h:207
QgsClassificationLogarithmic::QgsClassificationLogarithmic
QgsClassificationLogarithmic()
Definition: qgsclassificationlogarithmic.cpp:24
QgsClassificationLogarithmic
Implementation of a logarithmic scale method.
Definition: qgsclassificationlogarithmic.h:29
c
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
Definition: porting_processing.dox:1
QgsClassificationMethod::ClassPosition
ClassPosition
Defines the class position.
Definition: qgsclassificationmethod.h:121
qgsprocessingcontext.h
QgsClassificationLogarithmic::valuesRequired
bool valuesRequired() const override
Returns if the method requires values to calculate the classes If not, bounds are sufficient.
Definition: qgsclassificationlogarithmic.cpp:148
QgsProcessingParameterEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparameters.h:2256
QgsClassificationLogarithmic::clone
QgsClassificationMethod * clone() const override
Returns a clone of the method.
Definition: qgsclassificationlogarithmic.cpp:32
QgsProcessingParameters::parameterAsEnum
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
Definition: qgsprocessingparameters.cpp:423
QgsClassificationMethod::parameterValues
QVariantMap parameterValues() const
Returns the values of the processing parameters.
Definition: qgsclassificationmethod.h:297
QgsClassificationLogarithmic::icon
QIcon icon() const override
The icon of the method.
Definition: qgsclassificationlogarithmic.cpp:49