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;
115 return defaultValue( aggregate );
120 QVariant v = expression->evaluate( context );
121 resultType = v.type();
127 return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
132 QString normalized =
string.trimmed().toLower();
137 if ( normalized == QLatin1String(
"count" ) )
139 else if ( normalized == QLatin1String(
"count_distinct" ) )
141 else if ( normalized == QLatin1String(
"count_missing" ) )
143 else if ( normalized == QLatin1String(
"min" ) )
145 else if ( normalized == QLatin1String(
"max" ) )
147 else if ( normalized == QLatin1String(
"sum" ) )
149 else if ( normalized == QLatin1String(
"mean" ) )
151 else if ( normalized == QLatin1String(
"median" ) )
153 else if ( normalized == QLatin1String(
"stdev" ) )
155 else if ( normalized == QLatin1String(
"stdevsample" ) )
157 else if ( normalized == QLatin1String(
"range" ) )
159 else if ( normalized == QLatin1String(
"minority" ) )
161 else if ( normalized == QLatin1String(
"majority" ) )
163 else if ( normalized == QLatin1String(
"q1" ) )
165 else if ( normalized == QLatin1String(
"q3" ) )
167 else if ( normalized == QLatin1String(
"iqr" ) )
169 else if ( normalized == QLatin1String(
"min_length" ) )
171 else if ( normalized == QLatin1String(
"max_length" ) )
173 else if ( normalized == QLatin1String(
"concatenate" ) )
175 else if ( normalized == QLatin1String(
"concatenate_unique" ) )
177 else if ( normalized == QLatin1String(
"collect" ) )
179 else if ( normalized == QLatin1String(
"array_agg" ) )
194 QStringLiteral(
"count" ),
195 QCoreApplication::tr(
"Count" ),
196 QSet<QVariant::Type>()
197 << QVariant::DateTime
201 << QVariant::LongLong
202 << QVariant::ULongLong
207 QStringLiteral(
"count_distinct" ),
208 QCoreApplication::tr(
"Count Distinct" ),
209 QSet<QVariant::Type>()
210 << QVariant::DateTime
214 << QVariant::LongLong
215 << QVariant::ULongLong
220 QStringLiteral(
"count_missing" ),
221 QCoreApplication::tr(
"Count Missing" ),
222 QSet<QVariant::Type>()
223 << QVariant::DateTime
227 << QVariant::LongLong
232 QStringLiteral(
"min" ),
233 QCoreApplication::tr(
"Min" ),
234 QSet<QVariant::Type>()
235 << QVariant::DateTime
239 << QVariant::LongLong
240 << QVariant::ULongLong
246 QStringLiteral(
"max" ),
247 QCoreApplication::tr(
"Max" ),
248 QSet<QVariant::Type>()
249 << QVariant::DateTime
253 << QVariant::LongLong
254 << QVariant::ULongLong
260 QStringLiteral(
"sum" ),
261 QCoreApplication::tr(
"Sum" ),
262 QSet<QVariant::Type>()
265 << QVariant::LongLong
266 << QVariant::ULongLong
271 QStringLiteral(
"mean" ),
272 QCoreApplication::tr(
"Mean" ),
273 QSet<QVariant::Type>()
276 << QVariant::LongLong
277 << QVariant::ULongLong
282 QStringLiteral(
"median" ),
283 QCoreApplication::tr(
"Median" ),
284 QSet<QVariant::Type>()
291 QStringLiteral(
"stdev" ),
292 QCoreApplication::tr(
"Stdev" ),
293 QSet<QVariant::Type>()
296 << QVariant::LongLong
297 << QVariant::ULongLong
302 QStringLiteral(
"stdevsample" ),
303 QCoreApplication::tr(
"Stdev Sample" ),
304 QSet<QVariant::Type>()
307 << QVariant::LongLong
308 << QVariant::ULongLong
313 QStringLiteral(
"range" ),
314 QCoreApplication::tr(
"Range" ),
315 QSet<QVariant::Type>()
317 << QVariant::DateTime
320 << QVariant::LongLong
321 << QVariant::ULongLong
326 QStringLiteral(
"minority" ),
327 QCoreApplication::tr(
"Minority" ),
328 QSet<QVariant::Type>()
331 << QVariant::LongLong
332 << QVariant::ULongLong
337 QStringLiteral(
"majority" ),
338 QCoreApplication::tr(
"Majority" ),
339 QSet<QVariant::Type>()
342 << QVariant::LongLong
343 << QVariant::ULongLong
348 QStringLiteral(
"q1" ),
349 QCoreApplication::tr(
"Q1" ),
350 QSet<QVariant::Type>()
353 << QVariant::LongLong
354 << QVariant::ULongLong
359 QStringLiteral(
"q3" ),
360 QCoreApplication::tr(
"Q3" ),
361 QSet<QVariant::Type>()
364 << QVariant::LongLong
365 << QVariant::ULongLong
370 QStringLiteral(
"iqr" ),
371 QCoreApplication::tr(
"InterQuartileRange" ),
372 QSet<QVariant::Type>()
375 << QVariant::LongLong
376 << QVariant::ULongLong
381 QStringLiteral(
"min_length" ),
382 QCoreApplication::tr(
"Min Length" ),
383 QSet<QVariant::Type>()
388 QStringLiteral(
"max_length" ),
389 QCoreApplication::tr(
"Max Length" ),
390 QSet<QVariant::Type>()
395 QStringLiteral(
"concatenate" ),
396 QCoreApplication::tr(
"Concatenate" ),
397 QSet<QVariant::Type>()
402 QStringLiteral(
"collect" ),
403 QCoreApplication::tr(
"Collect" ),
404 QSet<QVariant::Type>()
408 QStringLiteral(
"array_agg" ),
409 QCoreApplication::tr(
"Array Aggregate" ),
410 QSet<QVariant::Type>()
426 return calculateArrayAggregate( fit, attr, expression, context );
429 switch ( resultType )
433 case QVariant::LongLong:
434 case QVariant::ULongLong:
435 case QVariant::Double:
444 return calculateNumericAggregate( fit, attr, expression, context, stat );
448 case QVariant::DateTime:
457 return calculateDateTimeAggregate( fit, attr, expression, context, stat );
460 case QVariant::UserType:
466 return calculateGeometryAggregate( fit, expression, context );
482 return concatenateStrings( fit, attr, expression, context,
delimiter );
489 return concatenateStrings( fit, attr, expression, context,
delimiter,
true );
499 return calculateStringAggregate( fit, attr, expression, context, stat );
665 Q_ASSERT( expression || attr >= 0 );
676 QVariant v = expression->
evaluate( context );
686 return std::isnan( val ) ? QVariant() : val;
692 Q_ASSERT( expression || attr >= 0 );
703 QVariant v = expression->
evaluate( context );
717 Q_ASSERT( expression );
720 QVector< QgsGeometry > geometries;
725 QVariant v = expression->
evaluate( context );
738 Q_ASSERT( expression || attr >= 0 );
749 QVariant v = expression->
evaluate( context );
750 result = v.toString();
757 if ( !unique || !results.contains( result ) )
780 return QVariantList();
807 Q_ASSERT( expression || attr >= 0 );
818 QVariant v = expression->
evaluate( context );
833 Q_ASSERT( expression || attr >= 0 );
845 QVariant v = expression->
evaluate( context );
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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
Median of values (numeric fields only)
const QgsVectorLayer * layer() const
Returns the associated vector layer.
void setFidsFilter(const QgsFeatureIds &fids)
Sets a filter to limit the features used during the aggregate calculation.
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.
QVariant evaluate()
Evaluate the feature and return the result.
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.
Concatenate unique values with a joining string (string fields only). Specify the delimiter using set...
QgsField at(int i) const
Gets 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 FINAL
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.
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
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 an array of values.
Minimum (earliest) datetime value.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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...
Number of distinct values.
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...
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
Mean of values (numeric fields only)
Create a multipart geometry from aggregated geometries.
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Minimum length of string.
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
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)
Sets flags that affect how features will be fetched.
A bundle of parameters controlling aggregate calculation.
Number of distinct datetime values.