QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
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. More...
#include <qgsaggregatecalculator.h>
Classes | |
struct | AggregateInfo |
Structured information about the available aggregates. More... | |
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, GeometryCollect, ArrayAggregate, StringConcatenateUnique } |
Available aggregates to calculate. More... | |
Public Member Functions | |
QgsAggregateCalculator (const 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... | |
const QgsVectorLayer * | layer () 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 | setFidsFilter (const QgsFeatureIds &fids) |
Sets a filter to limit the features used during the aggregate calculation. More... | |
void | setFilter (const QString &filterExpression) |
Sets a filter to limit the features used during the aggregate calculation. More... | |
void | setParameters (const AggregateParameters ¶meters) |
Sets all aggregate parameters from a parameter bundle. More... | |
Static Public Member Functions | |
static QList< QgsAggregateCalculator::AggregateInfo > | aggregates () |
Structured information for available aggregates. More... | |
static Aggregate | stringToAggregate (const QString &string, bool *ok=nullptr) |
Converts a string to a aggregate type. More... | |
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.
Definition at line 44 of file qgsaggregatecalculator.h.
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. |
Majority | Majority of values. |
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 | Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimiter(). |
GeometryCollect | Create a multipart geometry from aggregated geometries. |
ArrayAggregate | Create an array of values. |
StringConcatenateUnique | Concatenate unique values with a joining string (string fields only). Specify the delimiter using setDelimiter(). |
Definition at line 64 of file qgsaggregatecalculator.h.
QgsAggregateCalculator::QgsAggregateCalculator | ( | const QgsVectorLayer * | layer | ) |
Constructor for QgsAggregateCalculator.
layer | vector layer to calculate aggregate from |
Definition at line 27 of file qgsaggregatecalculator.cpp.
|
static |
Structured information for available aggregates.
Definition at line 188 of file qgsaggregatecalculator.cpp.
QVariant QgsAggregateCalculator::calculate | ( | QgsAggregateCalculator::Aggregate | aggregate, |
const QString & | fieldOrExpression, | ||
QgsExpressionContext * | context = nullptr , |
||
bool * | ok = nullptr |
||
) | const |
Calculates the value of an aggregate.
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 |
Definition at line 51 of file qgsaggregatecalculator.cpp.
|
inline |
Returns the delimiter used for joining values with the StringConcatenate aggregate.
Definition at line 165 of file qgsaggregatecalculator.h.
|
inline |
Returns the filter which limits the features used during the aggregate calculation.
Definition at line 152 of file qgsaggregatecalculator.h.
const QgsVectorLayer * QgsAggregateCalculator::layer | ( | ) | const |
Returns the associated vector layer.
Definition at line 33 of file qgsaggregatecalculator.cpp.
|
inline |
Sets the delimiter to use for joining values with the StringConcatenate aggregate.
delimiter | string delimiter |
Definition at line 159 of file qgsaggregatecalculator.h.
void QgsAggregateCalculator::setFidsFilter | ( | const QgsFeatureIds & | fids | ) |
Sets a filter to limit the features used during the aggregate calculation.
If an expression filter is set, it will override this filter.
fids | feature ids for feature filtering, and empty list will return no features. |
Definition at line 45 of file qgsaggregatecalculator.cpp.
|
inline |
Sets a filter to limit the features used during the aggregate calculation.
filterExpression | expression for filtering features, or empty string to remove filter |
Definition at line 138 of file qgsaggregatecalculator.h.
void QgsAggregateCalculator::setParameters | ( | const AggregateParameters & | parameters | ) |
Sets all aggregate parameters from a parameter bundle.
parameters | aggregate parameters |
Definition at line 38 of file qgsaggregatecalculator.cpp.
|
static |
Converts a string to a aggregate type.
string | string to convert |
ok | if specified, will be set to true if conversion was successful |
Definition at line 130 of file qgsaggregatecalculator.cpp.