QgsAggregateCalculator class

Utility class for calculating aggregates for a field (or expression) over the features from a vector layer.

It is recommended that QgsVectorLayer::aggregate() is used rather then directly using this class, as the QgsVectorLayer method can handle delegating aggregate calculation to a data provider for remote calculation.

Public types

struct AggregateInfo
Structured information about the available aggregates.
struct AggregateParameters
A bundle of parameters controlling aggregate calculation.
enum Aggregate { Count, CountDistinct, CountMissing, Min, Max, Sum, Mean, Median, StDev, StDevSample, Range, Minority, Majority, FirstQuartile, ThirdQuartile, InterQuartileRange, StringMinimumLength, StringMaximumLength, StringConcatenate, GeometryCollect, ArrayAggregate }
Available aggregates to calculate.

Public static functions

static auto aggregates() -> QList<QgsAggregateCalculator::AggregateInfo>
Structured information for available aggregates.
static auto stringToAggregate(const QString& string, bool* ok = nullptr) -> Aggregate
Converts a string to a aggregate type.

Constructors, destructors, conversion operators

QgsAggregateCalculator(const QgsVectorLayer* layer)
Constructor for QgsAggregateCalculator.

Public functions

auto calculate(Aggregate aggregate, const QString& fieldOrExpression, QgsExpressionContext* context = nullptr, bool* ok = nullptr) const -> QVariant
Calculates the value of an aggregate.
auto delimiter() const -> QString
Returns the delimiter used for joining values with the StringConcatenate aggregate.
auto filter() const -> QString
Returns the filter which limits the features used during the aggregate calculation.
auto layer() const -> const QgsVectorLayer*
Returns the associated vector layer.
void setDelimiter(const QString& delimiter)
Sets the delimiter to use for joining values with the StringConcatenate aggregate.
void setFilter(const QString& filterExpression)
Sets a filter to limit the features used during the aggregate calculation.
void setParameters(const AggregateParameters& parameters)
Sets all aggregate parameters from a parameter bundle.

Enum documentation

enum QgsAggregateCalculator::Aggregate

Available aggregates to calculate.

Not all aggregates are available for all field types.

Enumerators
Count

Count.

CountDistinct

Number of distinct values.

CountMissing

Number of missing (null) values.

Min

Min of values.

Max

Max of values.

Sum

Sum of values.

Mean

Mean of values (numeric fields only)

Median

Median of values (numeric fields only)

StDev

Standard deviation of values (numeric fields only)

StDevSample

Sample standard deviation of values (numeric fields only)

Range

Range of values (max - min) (numeric and datetime fields only)

Minority

Minority of values (numeric fields only)

Majority

Majority of values (numeric fields only)

FirstQuartile

First quartile (numeric fields only)

ThirdQuartile

Third quartile (numeric fields only)

InterQuartileRange

Inter quartile range (IQR) (numeric fields only)

StringMinimumLength

Minimum length of string (string fields only)

StringMaximumLength

Maximum length of string (string fields only)

StringConcatenate
GeometryCollect

Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimiter().

ArrayAggregate

Create a multipart geometry from aggregated geometries.

Create an array of values

Function documentation

static QList<QgsAggregateCalculator::AggregateInfo> QgsAggregateCalculator::aggregates()

Structured information for available aggregates.

static Aggregate QgsAggregateCalculator::stringToAggregate(const QString& string, bool* ok = nullptr)

Converts a string to a aggregate type.

Parameters
string string to convert
ok if specified, will be set to true if conversion was successful
Returns aggregate type

QgsAggregateCalculator::QgsAggregateCalculator(const QgsVectorLayer* layer)

Constructor for QgsAggregateCalculator.

Parameters
layer vector layer to calculate aggregate from

QVariant QgsAggregateCalculator::calculate(Aggregate aggregate, const QString& fieldOrExpression, QgsExpressionContext* context = nullptr, bool* ok = nullptr) const

Calculates the value of an aggregate.

Parameters
aggregate aggregate to calculate
fieldOrExpression source field or expression to use as basis for aggregated values. If an expression is used, then the context parameter must be set.
context expression context for evaluating expressions
ok if specified, will be set to true if aggregate calculation was successful
Returns calculated aggregate value

QString QgsAggregateCalculator::delimiter() const

Returns the delimiter used for joining values with the StringConcatenate aggregate.

QString QgsAggregateCalculator::filter() const

Returns the filter which limits the features used during the aggregate calculation.

void QgsAggregateCalculator::setDelimiter(const QString& delimiter)

Sets the delimiter to use for joining values with the StringConcatenate aggregate.

Parameters
delimiter string delimiter

void QgsAggregateCalculator::setFilter(const QString& filterExpression)

Sets a filter to limit the features used during the aggregate calculation.

Parameters
filterExpression expression for filtering features, or empty string to remove filter

void QgsAggregateCalculator::setParameters(const AggregateParameters& parameters)

Sets all aggregate parameters from a parameter bundle.

Parameters
parameters aggregate parameters