QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 <QVariant>
26 
27 
28 class QgsFeatureIterator;
29 class QgsExpression;
30 class QgsVectorLayer;
32 
42 class CORE_EXPORT QgsAggregateCalculator
43 {
44  public:
45 
52  {
53  QString function;
54  QString name;
55  QSet<QVariant::Type> supportedTypes;
56  };
57 
62  enum Aggregate
63  {
67  Min,
68  Max,
69  Sum,
70  Mean,
84  ArrayAggregate
85  };
86 
89  {
90 
97  QString filter;
98 
104  QString delimiter;
105  };
106 
111  QgsAggregateCalculator( const QgsVectorLayer *layer );
112 
116  const QgsVectorLayer *layer() const;
117 
122  void setParameters( const AggregateParameters &parameters );
123 
129  void setFilter( const QString &filterExpression ) { mFilterExpression = filterExpression; }
130 
135  QString filter() const { return mFilterExpression; }
136 
142  void setDelimiter( const QString &delimiter ) { mDelimiter = delimiter; }
143 
148  QString delimiter() const { return mDelimiter; }
149 
159  QVariant calculate( Aggregate aggregate, const QString &fieldOrExpression,
160  QgsExpressionContext *context = nullptr, bool *ok = nullptr ) const;
161 
168  static Aggregate stringToAggregate( const QString &string, bool *ok = nullptr );
169 
175  static QList< QgsAggregateCalculator::AggregateInfo > aggregates();
176 
177  private:
178 
180  const QgsVectorLayer *mLayer = nullptr;
181 
183  QString mFilterExpression;
184 
186  QString mDelimiter;
187 
188  static QgsStatisticalSummary::Statistic numericStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
189  static QgsStringStatisticalSummary::Statistic stringStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
190  static QgsDateTimeStatisticalSummary::Statistic dateTimeStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
191 
192  static QVariant calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
194 
195  static QVariant calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
197 
198  static QVariant calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
200  static QVariant calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context );
201 
202  static QVariant calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
203  QgsExpressionContext *context );
204 
205  static QVariant calculate( Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
206  int attr, QgsExpression *expression,
207  const QString &delimiter,
208  QgsExpressionContext *context, bool *ok = nullptr );
209  static QVariant concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
210  QgsExpressionContext *context, const QString &delimiter );
211 
212  QVariant defaultValue( Aggregate aggregate ) const;
213 };
214 
215 #endif //QGSAGGREGATECALCULATOR_H
216 
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
Third quartile (numeric fields only)
Inter quartile range (IQR) (numeric fields only)
Median of values (numeric fields only)
QSet< QVariant::Type > supportedTypes
This aggregate function can only be used with these datatypes.
void setDelimiter(const QString &delimiter)
Sets the delimiter to use for joining values with the StringConcatenate aggregate.
Statistic
Enumeration of flags that specify statistics to be calculated.
void setFilter(const QString &filterExpression)
Sets a filter to limit the features used during the aggregate calculation.
First quartile (numeric fields only)
Number of missing (null) values.
Statistic
Enumeration of flags that specify statistics to be calculated.
Utility class for calculating aggregates for a field (or expression) over the features from a vector ...
Structured information about the available aggregates.
Standard deviation of values (numeric fields only)
QString delimiter
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.
Minimum length of string (string fields only)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QString name
A translated, human readable name.
Statistic
Enumeration of flags that specify statistics to be calculated.
Majority of values (numeric fields only)
Maximum length of string (string fields only)
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
Mean of values (numeric fields only)
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
Minority of values (numeric fields only)
Represents a vector layer which manages a vector based data sets.
Range of values (max - min) (numeric and datetime fields only)
Sample standard deviation of values (numeric fields only)
Aggregate
Available aggregates to calculate.
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
A bundle of parameters controlling aggregate calculation.