QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  {
55  QString function;
56  QString name;
57  QSet<QVariant::Type> supportedTypes;
58  };
59 
64  enum Aggregate
65  {
69  Min,
70  Max,
71  Sum,
72  Mean,
87  StringConcatenateUnique
88  };
89 
92  {
93 
100  QString filter;
101 
107  QString delimiter;
108 
114  };
115 
120  QgsAggregateCalculator( const QgsVectorLayer *layer );
121 
125  const QgsVectorLayer *layer() const;
126 
131  void setParameters( const AggregateParameters &parameters );
132 
138  void setFilter( const QString &filterExpression ) { mFilterExpression = filterExpression; }
139 
146  void setFidsFilter( const QgsFeatureIds &fids );
147 
152  QString filter() const { return mFilterExpression; }
153 
159  void setDelimiter( const QString &delimiter ) { mDelimiter = delimiter; }
160 
165  QString delimiter() const { return mDelimiter; }
166 
176  QVariant calculate( Aggregate aggregate, const QString &fieldOrExpression,
177  QgsExpressionContext *context = nullptr, bool *ok = nullptr ) const;
178 
185  static Aggregate stringToAggregate( const QString &string, bool *ok = nullptr );
186 
192  static QList< QgsAggregateCalculator::AggregateInfo > aggregates();
193 
194  private:
195 
197  const QgsVectorLayer *mLayer = nullptr;
198 
200  QString mFilterExpression;
201 
204 
206  QString mDelimiter;
207 
209  QgsFeatureIds mFidsFilter;
210 
211  //trigger variable
212  bool mFidsSet = false;
213 
214  static QgsStatisticalSummary::Statistic numericStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
215  static QgsStringStatisticalSummary::Statistic stringStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
216  static QgsDateTimeStatisticalSummary::Statistic dateTimeStatFromAggregate( Aggregate aggregate, bool *ok = nullptr );
217 
218  static QVariant calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
220 
221  static QVariant calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
223 
224  static QVariant calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
226  static QVariant calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context );
227 
228  static QVariant calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
229  QgsExpressionContext *context );
230 
231  static QVariant calculate( Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
232  int attr, QgsExpression *expression,
233  const QString &delimiter,
234  QgsExpressionContext *context, bool *ok = nullptr );
235  static QVariant concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
236  QgsExpressionContext *context, const QString &delimiter, bool unique = false );
237 
238  QVariant defaultValue( Aggregate aggregate ) const;
239 };
240 
241 #endif //QGSAGGREGATECALCULATOR_H
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)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
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.
Minimum length of string (string fields only)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
QString name
A translated, human readable name.
Statistic
Enumeration of flags that specify statistics to be calculated.
QString filter() const
Returns the filter which limits the features used during the aggregate calculation.
Majority of values (numeric fields only)
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
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)
Create a multipart geometry from aggregated geometries.
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.
Represents a list of OrderByClauses, with the most important first and the least important last...
A bundle of parameters controlling aggregate calculation.