40 mFilterExpression = parameters.
filter;
45 const QString &fieldOrExpression,
55 context = context ? context : &defaultContext;
57 std::unique_ptr<QgsExpression> expression;
66 expression.reset(
new QgsExpression( fieldOrExpression ) );
68 if ( expression->hasParserError() || !expression->prepare( context ) )
76 lst.insert( fieldOrExpression );
78 lst = expression->referencedColumns();
81 .
setFlags( ( expression && expression->needsGeometry() ) ?
85 if ( !mFilterExpression.isEmpty() )
91 QVariant::Type resultType = QVariant::Double;
104 return defaultValue( aggregate );
109 QVariant v = expression->evaluate( context );
110 resultType = v.type();
118 return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
123 QString normalized =
string.trimmed().toLower();
128 if ( normalized == QLatin1String(
"count" ) )
130 else if ( normalized == QLatin1String(
"count_distinct" ) )
132 else if ( normalized == QLatin1String(
"count_missing" ) )
134 else if ( normalized == QLatin1String(
"min" ) )
136 else if ( normalized == QLatin1String(
"max" ) )
138 else if ( normalized == QLatin1String(
"sum" ) )
140 else if ( normalized == QLatin1String(
"mean" ) )
142 else if ( normalized == QLatin1String(
"median" ) )
144 else if ( normalized == QLatin1String(
"stdev" ) )
146 else if ( normalized == QLatin1String(
"stdevsample" ) )
148 else if ( normalized == QLatin1String(
"range" ) )
150 else if ( normalized == QLatin1String(
"minority" ) )
152 else if ( normalized == QLatin1String(
"majority" ) )
154 else if ( normalized == QLatin1String(
"q1" ) )
156 else if ( normalized == QLatin1String(
"q3" ) )
158 else if ( normalized == QLatin1String(
"iqr" ) )
160 else if ( normalized == QLatin1String(
"min_length" ) )
162 else if ( normalized == QLatin1String(
"max_length" ) )
164 else if ( normalized == QLatin1String(
"concatenate" ) )
166 else if ( normalized == QLatin1String(
"collect" ) )
168 else if ( normalized == QLatin1String(
"array_agg" ) )
183 QStringLiteral(
"count" ),
184 QCoreApplication::tr(
"Count" ),
185 QSet<QVariant::Type>()
186 << QVariant::DateTime
190 << QVariant::LongLong
191 << QVariant::ULongLong
196 QStringLiteral(
"count_distinct" ),
197 QCoreApplication::tr(
"Count Distinct" ),
198 QSet<QVariant::Type>()
199 << QVariant::DateTime
203 << QVariant::LongLong
204 << QVariant::ULongLong
209 QStringLiteral(
"count_missing" ),
210 QCoreApplication::tr(
"Count Missing" ),
211 QSet<QVariant::Type>()
212 << QVariant::DateTime
216 << QVariant::LongLong
221 QStringLiteral(
"min" ),
222 QCoreApplication::tr(
"Min" ),
223 QSet<QVariant::Type>()
224 << QVariant::DateTime
228 << QVariant::LongLong
229 << QVariant::ULongLong
235 QStringLiteral(
"max" ),
236 QCoreApplication::tr(
"Max" ),
237 QSet<QVariant::Type>()
238 << QVariant::DateTime
242 << QVariant::LongLong
243 << QVariant::ULongLong
249 QStringLiteral(
"sum" ),
250 QCoreApplication::tr(
"Sum" ),
251 QSet<QVariant::Type>()
254 << QVariant::LongLong
255 << QVariant::ULongLong
260 QStringLiteral(
"mean" ),
261 QCoreApplication::tr(
"Mean" ),
262 QSet<QVariant::Type>()
265 << QVariant::LongLong
266 << QVariant::ULongLong
271 QStringLiteral(
"median" ),
272 QCoreApplication::tr(
"Median" ),
273 QSet<QVariant::Type>()
280 QStringLiteral(
"stdev" ),
281 QCoreApplication::tr(
"Stdev" ),
282 QSet<QVariant::Type>()
285 << QVariant::LongLong
286 << QVariant::ULongLong
291 QStringLiteral(
"stdevsample" ),
292 QCoreApplication::tr(
"Stdev Sample" ),
293 QSet<QVariant::Type>()
296 << QVariant::LongLong
297 << QVariant::ULongLong
302 QStringLiteral(
"range" ),
303 QCoreApplication::tr(
"Range" ),
304 QSet<QVariant::Type>()
306 << QVariant::DateTime
309 << QVariant::LongLong
310 << QVariant::ULongLong
315 QStringLiteral(
"minority" ),
316 QCoreApplication::tr(
"Minority" ),
317 QSet<QVariant::Type>()
320 << QVariant::LongLong
321 << QVariant::ULongLong
326 QStringLiteral(
"majority" ),
327 QCoreApplication::tr(
"Majority" ),
328 QSet<QVariant::Type>()
331 << QVariant::LongLong
332 << QVariant::ULongLong
337 QStringLiteral(
"q1" ),
338 QCoreApplication::tr(
"Q1" ),
339 QSet<QVariant::Type>()
342 << QVariant::LongLong
343 << QVariant::ULongLong
348 QStringLiteral(
"q3" ),
349 QCoreApplication::tr(
"Q3" ),
350 QSet<QVariant::Type>()
353 << QVariant::LongLong
354 << QVariant::ULongLong
359 QStringLiteral(
"iqr" ),
360 QCoreApplication::tr(
"InterQuartileRange" ),
361 QSet<QVariant::Type>()
364 << QVariant::LongLong
365 << QVariant::ULongLong
370 QStringLiteral(
"min_length" ),
371 QCoreApplication::tr(
"Min Length" ),
372 QSet<QVariant::Type>()
377 QStringLiteral(
"max_length" ),
378 QCoreApplication::tr(
"Max Length" ),
379 QSet<QVariant::Type>()
384 QStringLiteral(
"concatenate" ),
385 QCoreApplication::tr(
"Concatenate" ),
386 QSet<QVariant::Type>()
391 QStringLiteral(
"collect" ),
392 QCoreApplication::tr(
"Collect" ),
393 QSet<QVariant::Type>()
397 QStringLiteral(
"array_agg" ),
398 QCoreApplication::tr(
"Array Aggregate" ),
399 QSet<QVariant::Type>()
415 return calculateArrayAggregate( fit, attr, expression, context );
418 switch ( resultType )
422 case QVariant::LongLong:
423 case QVariant::ULongLong:
424 case QVariant::Double:
433 return calculateNumericAggregate( fit, attr, expression, context, stat );
437 case QVariant::DateTime:
446 return calculateDateTimeAggregate( fit, attr, expression, context, stat );
449 case QVariant::UserType:
455 return calculateGeometryAggregate( fit, expression, context );
471 return concatenateStrings( fit, attr, expression, context,
delimiter );
481 return calculateStringAggregate( fit, attr, expression, context, stat );
641 QVariant QgsAggregateCalculator::calculateNumericAggregate(
QgsFeatureIterator &fit,
int attr, QgsExpression *expression,
644 Q_ASSERT( expression || attr >= 0 );
655 QVariant v = expression->evaluate( context );
665 return std::isnan( val ) ? QVariant() : val;
668 QVariant QgsAggregateCalculator::calculateStringAggregate(
QgsFeatureIterator &fit,
int attr, QgsExpression *expression,
671 Q_ASSERT( expression || attr >= 0 );
682 QVariant v = expression->evaluate( context );
696 Q_ASSERT( expression );
699 QVector< QgsGeometry > geometries;
704 QVariant v = expression->evaluate( context );
714 QVariant QgsAggregateCalculator::concatenateStrings(
QgsFeatureIterator &fit,
int attr, QgsExpression *expression,
717 Q_ASSERT( expression || attr >= 0 );
723 if ( !result.isEmpty() )
730 QVariant v = expression->evaluate( context );
731 result += v.toString();
735 result += f.
attribute( attr ).toString();
756 return QVariantList();
780 QVariant QgsAggregateCalculator::calculateDateTimeAggregate(
QgsFeatureIterator &fit,
int attr, QgsExpression *expression,
783 Q_ASSERT( expression || attr >= 0 );
794 QVariant v = expression->evaluate( context );
806 QVariant QgsAggregateCalculator::calculateArrayAggregate(
QgsFeatureIterator &fit,
int attr, QgsExpression *expression,
809 Q_ASSERT( expression || attr >= 0 );
821 QVariant v = expression->evaluate( context );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
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)
const QgsVectorLayer * layer() const
Returns the associated vector layer.
Statistic
Enumeration of flags that specify statistics to be calculated.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsAggregateCalculator(const QgsVectorLayer *layer)
Constructor for QgsAggregateCalculator.
First quartile (numeric fields only)
Number of missing (null) values.
Number of missing (null) values.
Maximum (latest) datetime value.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
Variety (count of distinct) values.
void finalize()
Must be called after adding all values with addValues() and before retrieving any calculated statisti...
Statistic
Enumeration of flags that specify statistics to be calculated.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
A geometry is the spatial representation of a feature.
Structured information about the available aggregates.
Interval between earliest and latest datetime value.
void addVariant(const QVariant &value)
Adds a single value to the statistics calculation.
Calculator for summary statistics and aggregates for a list of datetimes.
Sample standard deviation of values.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Standard deviation of values (numeric fields only)
Number of distinct string values.
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
void setParameters(const AggregateParameters ¶meters)
Sets all aggregate parameters from a parameter bundle.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Number of missing (null) values.
QgsFields fields() const override
Returns the list of fields of this layer.
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
Number of missing (null) values.
Minimum length of string (string fields only)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Inter quartile range (IQR)
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Statistic
Enumeration of flags that specify statistics to be calculated.
Create a multipart geometry from aggregated geometries.
Minimum (earliest) datetime value.
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr) const
Calculates the value of an aggregate.
Majority of values (numeric fields only)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Maximum length of string.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
Number of distinct values.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
double statistic(QgsStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
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)
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
Calculator for summary statistics and aggregates for a list of strings.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
Minimum length of string.
Standard deviation of values.
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
bool nextFeature(QgsFeature &f)
Minority of values (numeric fields only)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Calculator for summary statistics for a list of doubles.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Range of values (max - min) (numeric and datetime fields only)
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
Sample standard deviation of values (numeric fields only)
Range of values (max - min)
Aggregate
Available aggregates to calculate.
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Set flags that affect how features will be fetched.
A bundle of parameters controlling aggregate calculation.
Number of distinct datetime values.