40 mFilterExpression = parameters.
filter;
63 context = context ? context : &defaultContext;
65 std::unique_ptr<QgsExpression> expression;
76 if ( expression->hasParserError() || !expression->prepare( context ) )
82 lst.insert( fieldOrExpression );
84 lst = expression->referencedColumns();
86 request.
setFlags( ( expression && expression->needsGeometry() ) ?
94 if ( !mOrderBy.empty() )
97 if ( !mFilterExpression.isEmpty() )
102 QVariant::Type resultType = QVariant::Double;
108 resultType = QVariant::UserType;
122 return defaultValue( aggregate );
127 QVariant v = expression->evaluate( context );
128 resultType = v.type();
135 return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
140 QString normalized =
string.trimmed().toLower();
145 if ( normalized == QLatin1String(
"count" ) )
147 else if ( normalized == QLatin1String(
"count_distinct" ) )
149 else if ( normalized == QLatin1String(
"count_missing" ) )
151 else if ( normalized == QLatin1String(
"min" ) )
153 else if ( normalized == QLatin1String(
"max" ) )
155 else if ( normalized == QLatin1String(
"sum" ) )
157 else if ( normalized == QLatin1String(
"mean" ) )
159 else if ( normalized == QLatin1String(
"median" ) )
161 else if ( normalized == QLatin1String(
"stdev" ) )
163 else if ( normalized == QLatin1String(
"stdevsample" ) )
165 else if ( normalized == QLatin1String(
"range" ) )
167 else if ( normalized == QLatin1String(
"minority" ) )
169 else if ( normalized == QLatin1String(
"majority" ) )
171 else if ( normalized == QLatin1String(
"q1" ) )
173 else if ( normalized == QLatin1String(
"q3" ) )
175 else if ( normalized == QLatin1String(
"iqr" ) )
177 else if ( normalized == QLatin1String(
"min_length" ) )
179 else if ( normalized == QLatin1String(
"max_length" ) )
181 else if ( normalized == QLatin1String(
"concatenate" ) )
183 else if ( normalized == QLatin1String(
"concatenate_unique" ) )
185 else if ( normalized == QLatin1String(
"collect" ) )
187 else if ( normalized == QLatin1String(
"array_agg" ) )
202 QStringLiteral(
"count" ),
203 QCoreApplication::tr(
"Count" ),
204 QSet<QVariant::Type>()
205 << QVariant::DateTime
209 << QVariant::LongLong
210 << QVariant::ULongLong
215 QStringLiteral(
"count_distinct" ),
216 QCoreApplication::tr(
"Count Distinct" ),
217 QSet<QVariant::Type>()
218 << QVariant::DateTime
222 << QVariant::LongLong
223 << QVariant::ULongLong
228 QStringLiteral(
"count_missing" ),
229 QCoreApplication::tr(
"Count Missing" ),
230 QSet<QVariant::Type>()
231 << QVariant::DateTime
235 << QVariant::LongLong
240 QStringLiteral(
"min" ),
241 QCoreApplication::tr(
"Min" ),
242 QSet<QVariant::Type>()
243 << QVariant::DateTime
247 << QVariant::LongLong
248 << QVariant::ULongLong
254 QStringLiteral(
"max" ),
255 QCoreApplication::tr(
"Max" ),
256 QSet<QVariant::Type>()
257 << QVariant::DateTime
261 << QVariant::LongLong
262 << QVariant::ULongLong
268 QStringLiteral(
"sum" ),
269 QCoreApplication::tr(
"Sum" ),
270 QSet<QVariant::Type>()
273 << QVariant::LongLong
274 << QVariant::ULongLong
279 QStringLiteral(
"mean" ),
280 QCoreApplication::tr(
"Mean" ),
281 QSet<QVariant::Type>()
284 << QVariant::LongLong
285 << QVariant::ULongLong
290 QStringLiteral(
"median" ),
291 QCoreApplication::tr(
"Median" ),
292 QSet<QVariant::Type>()
299 QStringLiteral(
"stdev" ),
300 QCoreApplication::tr(
"Stdev" ),
301 QSet<QVariant::Type>()
304 << QVariant::LongLong
305 << QVariant::ULongLong
310 QStringLiteral(
"stdevsample" ),
311 QCoreApplication::tr(
"Stdev Sample" ),
312 QSet<QVariant::Type>()
315 << QVariant::LongLong
316 << QVariant::ULongLong
321 QStringLiteral(
"range" ),
322 QCoreApplication::tr(
"Range" ),
323 QSet<QVariant::Type>()
325 << QVariant::DateTime
328 << QVariant::LongLong
329 << QVariant::ULongLong
334 QStringLiteral(
"minority" ),
335 QCoreApplication::tr(
"Minority" ),
336 QSet<QVariant::Type>()
339 << QVariant::LongLong
340 << QVariant::ULongLong
346 QStringLiteral(
"majority" ),
347 QCoreApplication::tr(
"Majority" ),
348 QSet<QVariant::Type>()
351 << QVariant::LongLong
352 << QVariant::ULongLong
358 QStringLiteral(
"q1" ),
359 QCoreApplication::tr(
"Q1" ),
360 QSet<QVariant::Type>()
363 << QVariant::LongLong
364 << QVariant::ULongLong
369 QStringLiteral(
"q3" ),
370 QCoreApplication::tr(
"Q3" ),
371 QSet<QVariant::Type>()
374 << QVariant::LongLong
375 << QVariant::ULongLong
380 QStringLiteral(
"iqr" ),
381 QCoreApplication::tr(
"InterQuartileRange" ),
382 QSet<QVariant::Type>()
385 << QVariant::LongLong
386 << QVariant::ULongLong
391 QStringLiteral(
"min_length" ),
392 QCoreApplication::tr(
"Min Length" ),
393 QSet<QVariant::Type>()
398 QStringLiteral(
"max_length" ),
399 QCoreApplication::tr(
"Max Length" ),
400 QSet<QVariant::Type>()
405 QStringLiteral(
"concatenate" ),
406 QCoreApplication::tr(
"Concatenate" ),
407 QSet<QVariant::Type>()
412 QStringLiteral(
"collect" ),
413 QCoreApplication::tr(
"Collect" ),
414 QSet<QVariant::Type>()
418 QStringLiteral(
"array_agg" ),
419 QCoreApplication::tr(
"Array Aggregate" ),
420 QSet<QVariant::Type>()
436 return calculateArrayAggregate( fit, attr, expression, context );
439 switch ( resultType )
443 case QVariant::LongLong:
444 case QVariant::ULongLong:
445 case QVariant::Double:
454 return calculateNumericAggregate( fit, attr, expression, context, stat );
458 case QVariant::DateTime:
467 return calculateDateTimeAggregate( fit, attr, expression, context, stat );
470 case QVariant::UserType:
476 return calculateGeometryAggregate( fit, expression, context );
492 return concatenateStrings( fit, attr, expression, context,
delimiter );
499 return concatenateStrings( fit, attr, expression, context,
delimiter,
true );
509 return calculateStringAggregate( fit, attr, expression, context, stat );
677 Q_ASSERT( expression || attr >= 0 );
688 QVariant v = expression->
evaluate( context );
697 double val = s.statistic( stat );
698 return std::isnan( val ) ? QVariant() : val;
704 Q_ASSERT( expression || attr >= 0 );
715 QVariant v = expression->
evaluate( context );
724 return s.statistic( stat );
729 Q_ASSERT( expression );
732 QVector< QgsGeometry > geometries;
737 QVariant v = expression->
evaluate( context );
750 Q_ASSERT( expression || attr >= 0 );
761 QVariant v = expression->
evaluate( context );
762 result = v.toString();
769 if ( !unique || !results.contains( result ) )
792 return QVariantList();
819 Q_ASSERT( expression || attr >= 0 );
830 QVariant v = expression->
evaluate( context );
839 return s.statistic( stat );
845 Q_ASSERT( expression || attr >= 0 );
857 QVariant v = expression->
evaluate( context );
const QgsVectorLayer * layer() const
Returns the associated vector layer.
void setParameters(const AggregateParameters ¶meters)
Sets all aggregate parameters from a parameter bundle.
void setFidsFilter(const QgsFeatureIds &fids)
Sets a filter to limit the features used during the aggregate calculation.
Aggregate
Available aggregates to calculate.
@ StringConcatenateUnique
Concatenate unique values with a joining string (string fields only). Specify the delimiter using set...
@ 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.
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr) const
Calculates the value of an aggregate.
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
QgsAggregateCalculator(const QgsVectorLayer *layer)
Constructor for QgsAggregateCalculator.
Calculator for summary statistics and aggregates for a list of datetimes.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Min
Minimum (earliest) datetime value.
@ Max
Maximum (latest) datetime value.
@ CountDistinct
Number of distinct datetime values.
@ Range
Interval between earliest and latest datetime value.
@ CountMissing
Number of missing (null) values.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
Calculator for summary statistics for a list of doubles.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ InterQuartileRange
Inter quartile range (IQR)
@ Median
Median of values.
@ Variety
Variety (count of distinct) values.
@ Minority
Minority of values.
@ ThirdQuartile
Third quartile.
@ Majority
Majority of values.
@ CountMissing
Number of missing (null) values.
@ Range
Range of values (max - min)
@ StDevSample
Sample standard deviation of values.
@ FirstQuartile
First quartile.
@ StDev
Standard deviation of values.
Calculator for summary statistics and aggregates for a list of strings.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Majority
Majority of strings.
@ Max
Maximum string value.
@ Minority
Minority of strings.
@ MaximumLength
Maximum length of string.
@ MinimumLength
Minimum length of string.
@ CountDistinct
Number of distinct string values.
@ CountMissing
Number of missing (null) values.
@ Min
Minimum string value.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QSet< QgsFeatureId > QgsFeatureIds
Structured information about the available aggregates.
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.