41 mFilterExpression = parameters.
filter;
65 context = context ? context : &defaultContext;
67 std::unique_ptr<QgsExpression> expression;
77 if ( expression->hasParserError() || !expression->prepare( context ) )
79 mLastError = !expression->parserErrorString().isEmpty() ? expression->parserErrorString() : expression->evalErrorString();
88 lst = expression->referencedColumns();
90 request.
setFlags( ( expression && expression->needsGeometry() ) ?
98 if ( !mOrderBy.empty() )
101 if ( !mFilterExpression.isEmpty() )
109 QVariant::Type resultType = QVariant::Double;
116 resultType = QVariant::UserType;
121 bool foundFeatures =
false;
123 if ( !foundFeatures )
127 return defaultValue( aggregate );
130 resultType = std::get<0>( returnType );
131 userType = std::get<1>( returnType );
132 if ( resultType == QVariant::Invalid )
176 resultType = v.type();
177 userType = v.userType();
185 return calculate( aggregate, fit, resultType, userType, attrNum, expression.get(), mDelimiter, context, ok, &mLastError );
190 const QString normalized =
string.trimmed().toLower();
195 if ( normalized == QLatin1String(
"count" ) )
197 else if ( normalized == QLatin1String(
"count_distinct" ) )
199 else if ( normalized == QLatin1String(
"count_missing" ) )
201 else if ( normalized == QLatin1String(
"min" ) )
203 else if ( normalized == QLatin1String(
"max" ) )
205 else if ( normalized == QLatin1String(
"sum" ) )
207 else if ( normalized == QLatin1String(
"mean" ) )
209 else if ( normalized == QLatin1String(
"median" ) )
211 else if ( normalized == QLatin1String(
"stdev" ) )
213 else if ( normalized == QLatin1String(
"stdevsample" ) )
215 else if ( normalized == QLatin1String(
"range" ) )
217 else if ( normalized == QLatin1String(
"minority" ) )
219 else if ( normalized == QLatin1String(
"majority" ) )
221 else if ( normalized == QLatin1String(
"q1" ) )
223 else if ( normalized == QLatin1String(
"q3" ) )
225 else if ( normalized == QLatin1String(
"iqr" ) )
227 else if ( normalized == QLatin1String(
"min_length" ) )
229 else if ( normalized == QLatin1String(
"max_length" ) )
231 else if ( normalized == QLatin1String(
"concatenate" ) )
233 else if ( normalized == QLatin1String(
"concatenate_unique" ) )
235 else if ( normalized == QLatin1String(
"collect" ) )
237 else if ( normalized == QLatin1String(
"array_agg" ) )
251 return QObject::tr(
"count" );
253 return QObject::tr(
"count distinct" );
255 return QObject::tr(
"count missing" );
257 return QObject::tr(
"minimum" );
259 return QObject::tr(
"maximum" );
261 return QObject::tr(
"sum" );
263 return QObject::tr(
"mean" );
265 return QObject::tr(
"median" );
267 return QObject::tr(
"standard deviation" );
269 return QObject::tr(
"standard deviation (sample)" );
271 return QObject::tr(
"range" );
273 return QObject::tr(
"minority" );
275 return QObject::tr(
"majority" );
277 return QObject::tr(
"first quartile" );
279 return QObject::tr(
"third quartile" );
281 return QObject::tr(
"inter quartile range" );
283 return QObject::tr(
"minimum length" );
285 return QObject::tr(
"maximum length" );
287 return QObject::tr(
"concatenate" );
289 return QObject::tr(
"collection" );
291 return QObject::tr(
"array aggregate" );
293 return QObject::tr(
"concatenate (unique)" );
304 QStringLiteral(
"count" ),
305 QCoreApplication::tr(
"Count" ),
306 QSet<QVariant::Type>()
307 << QVariant::DateTime
311 << QVariant::LongLong
312 << QVariant::ULongLong
317 QStringLiteral(
"count_distinct" ),
318 QCoreApplication::tr(
"Count Distinct" ),
319 QSet<QVariant::Type>()
320 << QVariant::DateTime
324 << QVariant::LongLong
325 << QVariant::ULongLong
330 QStringLiteral(
"count_missing" ),
331 QCoreApplication::tr(
"Count Missing" ),
332 QSet<QVariant::Type>()
333 << QVariant::DateTime
337 << QVariant::LongLong
342 QStringLiteral(
"min" ),
343 QCoreApplication::tr(
"Min" ),
344 QSet<QVariant::Type>()
345 << QVariant::DateTime
349 << QVariant::LongLong
350 << QVariant::ULongLong
356 QStringLiteral(
"max" ),
357 QCoreApplication::tr(
"Max" ),
358 QSet<QVariant::Type>()
359 << QVariant::DateTime
363 << QVariant::LongLong
364 << QVariant::ULongLong
370 QStringLiteral(
"sum" ),
371 QCoreApplication::tr(
"Sum" ),
372 QSet<QVariant::Type>()
375 << QVariant::LongLong
376 << QVariant::ULongLong
381 QStringLiteral(
"mean" ),
382 QCoreApplication::tr(
"Mean" ),
383 QSet<QVariant::Type>()
386 << QVariant::LongLong
387 << QVariant::ULongLong
392 QStringLiteral(
"median" ),
393 QCoreApplication::tr(
"Median" ),
394 QSet<QVariant::Type>()
401 QStringLiteral(
"stdev" ),
402 QCoreApplication::tr(
"Stdev" ),
403 QSet<QVariant::Type>()
406 << QVariant::LongLong
407 << QVariant::ULongLong
412 QStringLiteral(
"stdevsample" ),
413 QCoreApplication::tr(
"Stdev Sample" ),
414 QSet<QVariant::Type>()
417 << QVariant::LongLong
418 << QVariant::ULongLong
423 QStringLiteral(
"range" ),
424 QCoreApplication::tr(
"Range" ),
425 QSet<QVariant::Type>()
427 << QVariant::DateTime
430 << QVariant::LongLong
431 << QVariant::ULongLong
436 QStringLiteral(
"minority" ),
437 QCoreApplication::tr(
"Minority" ),
438 QSet<QVariant::Type>()
441 << QVariant::LongLong
442 << QVariant::ULongLong
448 QStringLiteral(
"majority" ),
449 QCoreApplication::tr(
"Majority" ),
450 QSet<QVariant::Type>()
453 << QVariant::LongLong
454 << QVariant::ULongLong
460 QStringLiteral(
"q1" ),
461 QCoreApplication::tr(
"Q1" ),
462 QSet<QVariant::Type>()
465 << QVariant::LongLong
466 << QVariant::ULongLong
471 QStringLiteral(
"q3" ),
472 QCoreApplication::tr(
"Q3" ),
473 QSet<QVariant::Type>()
476 << QVariant::LongLong
477 << QVariant::ULongLong
482 QStringLiteral(
"iqr" ),
483 QCoreApplication::tr(
"InterQuartileRange" ),
484 QSet<QVariant::Type>()
487 << QVariant::LongLong
488 << QVariant::ULongLong
493 QStringLiteral(
"min_length" ),
494 QCoreApplication::tr(
"Min Length" ),
495 QSet<QVariant::Type>()
500 QStringLiteral(
"max_length" ),
501 QCoreApplication::tr(
"Max Length" ),
502 QSet<QVariant::Type>()
507 QStringLiteral(
"concatenate" ),
508 QCoreApplication::tr(
"Concatenate" ),
509 QSet<QVariant::Type>()
514 QStringLiteral(
"collect" ),
515 QCoreApplication::tr(
"Collect" ),
516 QSet<QVariant::Type>()
520 QStringLiteral(
"array_agg" ),
521 QCoreApplication::tr(
"Array Aggregate" ),
522 QSet<QVariant::Type>()
538 return calculateArrayAggregate( fit, attr, expression, context );
541 switch ( resultType )
545 case QVariant::LongLong:
546 case QVariant::ULongLong:
547 case QVariant::Double:
554 *error = expression ? QObject::tr(
"Cannot calculate %1 on numeric values" ).arg(
displayName( aggregate ) )
555 : QObject::tr(
"Cannot calculate %1 on numeric fields" ).arg(
displayName( aggregate ) );
561 return calculateNumericAggregate( fit, attr, expression, context, stat );
565 case QVariant::DateTime:
572 *error = ( expression ? QObject::tr(
"Cannot calculate %1 on %2 values" ).arg(
displayName( aggregate ) ) :
573 QObject::tr(
"Cannot calculate %1 on %2 fields" ).arg(
displayName( aggregate ) ) ).arg( resultType == QVariant::Date ? QObject::tr(
"date" ) : QObject::tr(
"datetime" ) );
579 return calculateDateTimeAggregate( fit, attr, expression, context, stat );
582 case QVariant::UserType:
588 return calculateGeometryAggregate( fit, expression, context );
604 return concatenateStrings( fit, attr, expression, context,
delimiter );
611 return concatenateStrings( fit, attr, expression, context,
delimiter,
true );
619 if ( resultType == QVariant::Invalid )
620 typeString = QObject::tr(
"null" );
621 else if ( resultType == QVariant::UserType )
624 typeString = resultType == QVariant::String ? QObject::tr(
"string" ) : QVariant::typeToName( resultType );
627 *error = expression ? QObject::tr(
"Cannot calculate %1 on %3 values" ).arg(
displayName( aggregate ), typeString )
628 : QObject::tr(
"Cannot calculate %1 on %3 fields" ).arg(
displayName( aggregate ), typeString );
634 return calculateStringAggregate( fit, attr, expression, context, stat );
802 Q_ASSERT( expression || attr >= 0 );
813 const QVariant v = expression->
evaluate( context );
822 const double val = s.statistic( stat );
823 return std::isnan( val ) ? QVariant() : val;
829 Q_ASSERT( expression || attr >= 0 );
840 const QVariant v = expression->
evaluate( context );
849 return s.statistic( stat );
854 Q_ASSERT( expression );
857 QVector< QgsGeometry > geometries;
862 const QVariant v = expression->
evaluate( context );
875 Q_ASSERT( expression || attr >= 0 );
886 const QVariant v = expression->
evaluate( context );
887 result = v.toString();
894 if ( !unique || !results.contains( result ) )
917 return QVariantList();
944 Q_ASSERT( expression || attr >= 0 );
955 const QVariant v = expression->
evaluate( context );
964 return s.statistic( stat );
970 Q_ASSERT( expression || attr >= 0 );
982 const 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.
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr, QgsFeedback *feedback=nullptr) const
Calculates the value of an aggregate.
static QString displayName(Aggregate aggregate)
Returns the friendly display name for a aggregate.
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...
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly by the expression to check if evaluation sh...
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.
static std::tuple< QVariant::Type, int > determineResultType(const QString &expression, const QgsVectorLayer *layer, QgsFeatureRequest request=QgsFeatureRequest(), QgsExpressionContext context=QgsExpressionContext(), bool *foundFeatures=nullptr)
Returns a value type and user type for a given expression.
Class for parsing and evaluation of expressions (formerly called "search strings").
static int expressionToLayerFieldIndex(const QString &expression, const QgsVectorLayer *layer)
Attempts to resolve an expression to a field index from the given layer.
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 & setFilterFids(const QgsFeatureIds &fids)
Sets the 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.
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the iterator to check if it should be cance...
@ 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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
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.