QGIS API Documentation  3.21.0-Master (3191652dc5)
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 
128  const QgsVectorLayer *layer() const;
129 
134  void setParameters( const AggregateParameters &parameters );
135 
141  void setFilter( const QString &filterExpression ) { mFilterExpression = filterExpression; }
142 
149  void setFidsFilter( const QgsFeatureIds &fids );
150 
155  QString filter() const { return mFilterExpression; }
156 
162  void setDelimiter( const QString &delimiter ) { mDelimiter = delimiter; }
163 
168  QString delimiter() const { return mDelimiter; }
169 
181  QVariant calculate( Aggregate aggregate, const QString &fieldOrExpression,
182  QgsExpressionContext *context = nullptr, bool *ok = nullptr, QgsFeedback *feedback = nullptr ) const;
183 
190  static Aggregate stringToAggregate( const QString &string, bool *ok = nullptr );
191 
197  static QList< QgsAggregateCalculator::AggregateInfo > aggregates();
198 
199  private:
200 
202  const QgsVectorLayer *mLayer = nullptr;
203 
205  QString mFilterExpression;
206 
209 
211  QString mDelimiter;
212 
214  QgsFeatureIds mFidsFilter;
215 
216  //trigger variable
217  bool mFidsSet = false;
218 
219  static QgsStatisticalSummary::Statistic numericStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
220  static QgsStringStatisticalSummary::Statistic stringStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
221  static QgsDateTimeStatisticalSummary::Statistic dateTimeStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
222 
223  static QVariant calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
225 
226  static QVariant calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
228 
229  static QVariant calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
231  static QVariant calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context );
232 
233  static QVariant calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
234  QgsExpressionContext *context );
235 
236  static QVariant calculate( Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
237  int attr, QgsExpression *expression,
238  const QString &delimiter,
239  QgsExpressionContext *context, bool *ok = nullptr );
240  static QVariant concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
241  QgsExpressionContext *context, const QString &delimiter, bool unique = false );
242 
243  QVariant defaultValue( Aggregate aggregate ) const;
244 };
245 
246 #endif //QGSAGGREGATECALCULATOR_H
Utility class for calculating aggregates for a field (or expression) over the features from a vector ...
void setFilter(const QString &filterExpression)
Sets a filter to limit the features used during the aggregate calculation.
void setDelimiter(const QString &delimiter)
Sets the delimiter to use for joining values with the StringConcatenate aggregate.
QString filter() const
Returns the filter which limits the features used during the aggregate calculation.
Aggregate
Available aggregates to calculate.
@ StDev
Standard deviation of values (numeric fields only)
@ StringMaximumLength
Maximum length of string (string fields only)
@ ThirdQuartile
Third quartile (numeric fields only)
@ Range
Range of values (max - min) (numeric and datetime fields only)
@ ArrayAggregate
Create an array of values.
@ InterQuartileRange
Inter quartile range (IQR) (numeric fields only)
@ FirstQuartile
First quartile (numeric fields only)
@ Median
Median of values (numeric fields only)
@ GeometryCollect
Create a multipart geometry from aggregated geometries.
@ CountMissing
Number of missing (null) values.
@ StDevSample
Sample standard deviation of values (numeric fields only)
@ Majority
Majority of values.
@ StringConcatenate
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
@ Mean
Mean of values (numeric fields only)
@ StringMinimumLength
Minimum length of string (string fields only)
@ CountDistinct
Number of distinct values.
@ Minority
Minority of values.
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
Statistic
Enumeration of flags that specify statistics to be calculated.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
Represents a list of OrderByClauses, with the most important first and the least important last.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Statistic
Enumeration of flags that specify statistics to be calculated.
Statistic
Enumeration of flags that specify statistics to be calculated.
Represents a vector layer which manages a vector based data sets.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
Structured information about the available aggregates.
QString name
A translated, human readable name.
QSet< QVariant::Type > supportedTypes
This aggregate function can only be used with these datatypes.
A bundle of parameters controlling aggregate calculation.
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.