QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
QgsAggregateCalculator Class Reference

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

#include <qgsaggregatecalculator.h>

Classes

struct  AggregateParameters
 A bundle of parameters controlling aggregate calculation. More...
 

Public Types

enum  Aggregate {
  Count, CountDistinct, CountMissing, Min,
  Max, Sum, Mean, Median,
  StDev, StDevSample, Range, Minority,
  Majority, FirstQuartile, ThirdQuartile, InterQuartileRange,
  StringMinimumLength, StringMaximumLength, StringConcatenate
}
 Available aggregates to calculate. More...
 

Public Member Functions

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

Static Public Member Functions

static Aggregate stringToAggregate (const QString &string, bool *ok=nullptr)
 Converts a string to a aggregate type. More...
 

Detailed Description

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.

Note
added in QGIS 2.16

Definition at line 40 of file qgsaggregatecalculator.h.

Member Enumeration Documentation

◆ Aggregate

Available aggregates to calculate.

Not all aggregates are available for all field types.

Enumerator
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 

Definition at line 46 of file qgsaggregatecalculator.h.

Constructor & Destructor Documentation

◆ QgsAggregateCalculator()

QgsAggregateCalculator::QgsAggregateCalculator ( QgsVectorLayer layer)

Constructor for QgsAggregateCalculator.

Parameters
layervector layer to calculate aggregate from

Definition at line 26 of file qgsaggregatecalculator.cpp.

Member Function Documentation

◆ calculate()

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

Calculates the value of an aggregate.

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

Definition at line 43 of file qgsaggregatecalculator.cpp.

◆ delimiter()

QString QgsAggregateCalculator::delimiter ( ) const
inline

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

See also
setDelimiter()

Definition at line 120 of file qgsaggregatecalculator.h.

◆ filter()

QString QgsAggregateCalculator::filter ( ) const
inline

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

See also
setFilter()

Definition at line 109 of file qgsaggregatecalculator.h.

◆ layer()

QgsVectorLayer * QgsAggregateCalculator::layer ( ) const

Returns the associated vector layer.

Definition at line 32 of file qgsaggregatecalculator.cpp.

◆ setDelimiter()

void QgsAggregateCalculator::setDelimiter ( const QString delimiter)
inline

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

Parameters
delimiterstring delimiter
See also
delimiter()

Definition at line 115 of file qgsaggregatecalculator.h.

◆ setFilter()

void QgsAggregateCalculator::setFilter ( const QString filterExpression)
inline

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

Parameters
filterExpressionexpression for filtering features, or empty string to remove filter
See also
filter()

Definition at line 104 of file qgsaggregatecalculator.h.

◆ setParameters()

void QgsAggregateCalculator::setParameters ( const AggregateParameters parameters)

Sets all aggregate parameters from a parameter bundle.

Parameters
parametersaggregate parameters

Definition at line 37 of file qgsaggregatecalculator.cpp.

◆ stringToAggregate()

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

Converts a string to a aggregate type.

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

Definition at line 123 of file qgsaggregatecalculator.cpp.


The documentation for this class was generated from the following files: