QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsaggregatecalculator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaggregatecalculator.h
3  ------------------------
4  begin : May 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSAGGREGATECALCULATOR_H
19 #define QGSAGGREGATECALCULATOR_H
20 
21 #include "qgis_core.h"
22 #include "qgsstatisticalsummary.h"
25 #include "qgsfeaturerequest.h"
26 #include <QVariant>
27 #include "qgsfeatureid.h"
28 
29 
30 class QgsFeatureIterator;
31 class QgsExpression;
32 class QgsVectorLayer;
34 
44 class CORE_EXPORT QgsAggregateCalculator
45 {
46  public:
47 
54  {
56  QString function;
58  QString name;
60  QSet<QVariant::Type> supportedTypes;
61  };
62 
67  enum Aggregate
68  {
72  Min,
73  Max,
74  Sum,
75  Mean,
90  StringConcatenateUnique
91  };
92 
95  {
96 
103  QString filter;
104 
110  QString delimiter;
111 
117  };
118 
123  QgsAggregateCalculator( const QgsVectorLayer *layer );
124 
130  QString lastError() const { return mLastError; }
131 
135  const QgsVectorLayer *layer() const;
136 
141  void setParameters( const AggregateParameters &parameters );
142 
148  void setFilter( const QString &filterExpression ) { mFilterExpression = filterExpression; }
149 
156  void setFidsFilter( const QgsFeatureIds &fids );
157 
162  QString filter() const { return mFilterExpression; }
163 
169  void setDelimiter( const QString &delimiter ) { mDelimiter = delimiter; }
170 
175  QString delimiter() const { return mDelimiter; }
176 
188  QVariant calculate( Aggregate aggregate, const QString &fieldOrExpression,
189  QgsExpressionContext *context = nullptr, bool *ok = nullptr, QgsFeedback *feedback = nullptr ) const;
190 
197  static Aggregate stringToAggregate( const QString &string, bool *ok = nullptr );
198 
203  static QString displayName( Aggregate aggregate );
204 
210  static QList< QgsAggregateCalculator::AggregateInfo > aggregates();
211 
212  private:
213 
215  const QgsVectorLayer *mLayer = nullptr;
216 
218  QString mFilterExpression;
219 
222 
224  QString mDelimiter;
225 
227  QgsFeatureIds mFidsFilter;
228 
229  //trigger variable
230  bool mFidsSet = false;
231 
232  mutable QString mLastError;
233 
234  static QgsStatisticalSummary::Statistic numericStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
235  static QgsStringStatisticalSummary::Statistic stringStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
236  static QgsDateTimeStatisticalSummary::Statistic dateTimeStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
237 
238  static QVariant calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
240 
241  static QVariant calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
243 
244  static QVariant calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
246  static QVariant calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context );
247 
248  static QVariant calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
249  QgsExpressionContext *context );
250 
251  static QVariant calculate( Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType, int userType,
252  int attr, QgsExpression *expression,
253  const QString &delimiter,
254  QgsExpressionContext *context, bool *ok = nullptr, QString *error = nullptr );
255  static QVariant concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
256  QgsExpressionContext *context, const QString &delimiter, bool unique = false );
257 
258  QVariant defaultValue( Aggregate aggregate ) const;
259 };
260 
261 #endif //QGSAGGREGATECALCULATOR_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsAggregateCalculator::AggregateParameters::delimiter
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
Definition: qgsaggregatecalculator.h:110
QgsAggregateCalculator::Range
@ Range
Range of values (max - min) (numeric and datetime fields only)
Definition: qgsaggregatecalculator.h:79
QgsAggregateCalculator::AggregateInfo::name
QString name
A translated, human readable name.
Definition: qgsaggregatecalculator.h:58
QgsAggregateCalculator
Utility class for calculating aggregates for a field (or expression) over the features from a vector ...
Definition: qgsaggregatecalculator.h:44
qgsfeaturerequest.h
QgsAggregateCalculator::filter
QString filter() const
Returns the filter which limits the features used during the aggregate calculation.
Definition: qgsaggregatecalculator.h:162
QgsAggregateCalculator::Minority
@ Minority
Minority of values.
Definition: qgsaggregatecalculator.h:80
QgsAggregateCalculator::AggregateParameters
A bundle of parameters controlling aggregate calculation.
Definition: qgsaggregatecalculator.h:94
QgsAggregateCalculator::CountDistinct
@ CountDistinct
Number of distinct values.
Definition: qgsaggregatecalculator.h:70
QgsAggregateCalculator::Min
@ Min
Min of values.
Definition: qgsaggregatecalculator.h:72
QgsAggregateCalculator::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsaggregatecalculator.h:71
QgsAggregateCalculator::ArrayAggregate
@ ArrayAggregate
Create an array of values.
Definition: qgsaggregatecalculator.h:89
QgsAggregateCalculator::Count
@ Count
Count.
Definition: qgsaggregatecalculator.h:69
qgsfeatureid.h
QgsAggregateCalculator::AggregateParameters::orderBy
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
Definition: qgsaggregatecalculator.h:116
qgsstatisticalsummary.h
QgsAggregateCalculator::InterQuartileRange
@ InterQuartileRange
Inter quartile range (IQR) (numeric fields only)
Definition: qgsaggregatecalculator.h:84
qgsdatetimestatisticalsummary.h
QgsAggregateCalculator::StDevSample
@ StDevSample
Sample standard deviation of values (numeric fields only)
Definition: qgsaggregatecalculator.h:78
QgsAggregateCalculator::delimiter
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
Definition: qgsaggregatecalculator.h:175
QgsAggregateCalculator::setFilter
void setFilter(const QString &filterExpression)
Sets a filter to limit the features used during the aggregate calculation.
Definition: qgsaggregatecalculator.h:148
QgsAggregateCalculator::StDev
@ StDev
Standard deviation of values (numeric fields only)
Definition: qgsaggregatecalculator.h:77
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsStringStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstringstatisticalsummary.h:49
QgsAggregateCalculator::StringMinimumLength
@ StringMinimumLength
Minimum length of string (string fields only)
Definition: qgsaggregatecalculator.h:85
QgsDateTimeStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsdatetimestatisticalsummary.h:50
QgsAggregateCalculator::AggregateInfo::supportedTypes
QSet< QVariant::Type > supportedTypes
This aggregate function can only be used with these datatypes.
Definition: qgsaggregatecalculator.h:60
QgsAggregateCalculator::Median
@ Median
Median of values (numeric fields only)
Definition: qgsaggregatecalculator.h:76
QgsAggregateCalculator::Max
@ Max
Max of values.
Definition: qgsaggregatecalculator.h:73
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsAggregateCalculator::StringConcatenate
@ StringConcatenate
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
Definition: qgsaggregatecalculator.h:87
QgsAggregateCalculator::Mean
@ Mean
Mean of values (numeric fields only)
Definition: qgsaggregatecalculator.h:75
QgsAggregateCalculator::lastError
QString lastError() const
Returns the last error encountered during the aggregate calculation.
Definition: qgsaggregatecalculator.h:130
QgsStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstatisticalsummary.h:48
QgsAggregateCalculator::ThirdQuartile
@ ThirdQuartile
Third quartile (numeric fields only)
Definition: qgsaggregatecalculator.h:83
QgsAggregateCalculator::Aggregate
Aggregate
Available aggregates to calculate.
Definition: qgsaggregatecalculator.h:67
QgsFeatureRequest::OrderBy
Represents a list of OrderByClauses, with the most important first and the least important last.
Definition: qgsfeaturerequest.h:264
QgsAggregateCalculator::Majority
@ Majority
Majority of values.
Definition: qgsaggregatecalculator.h:81
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsstringstatisticalsummary.h
QgsAggregateCalculator::AggregateInfo
Structured information about the available aggregates.
Definition: qgsaggregatecalculator.h:53
QgsAggregateCalculator::FirstQuartile
@ FirstQuartile
First quartile (numeric fields only)
Definition: qgsaggregatecalculator.h:82
QgsAggregateCalculator::Sum
@ Sum
Sum of values.
Definition: qgsaggregatecalculator.h:74
QgsAggregateCalculator::AggregateParameters::filter
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
Definition: qgsaggregatecalculator.h:103
QgsAggregateCalculator::GeometryCollect
@ GeometryCollect
Create a multipart geometry from aggregated geometries.
Definition: qgsaggregatecalculator.h:88
QgsAggregateCalculator::setDelimiter
void setDelimiter(const QString &delimiter)
Sets the delimiter to use for joining values with the StringConcatenate aggregate.
Definition: qgsaggregatecalculator.h:169
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsAggregateCalculator::StringMaximumLength
@ StringMaximumLength
Maximum length of string (string fields only)
Definition: qgsaggregatecalculator.h:86