Class: QgsAggregateCalculator

class qgis.core.QgsAggregateCalculator

Bases: sip.wrapper

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.

QgsAggregateCalculator(layer: QgsVectorLayer) Constructor for QgsAggregateCalculator.

Parameters:

layer – vector layer to calculate aggregate from

QgsAggregateCalculator(QgsAggregateCalculator)

Enums

Aggregate

Bases: enum.IntEnum

Methods

aggregates

Structured information for available aggregates.

calculate

Calculates the value of an aggregate.

delimiter

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

displayName

Returns the friendly display name for a aggregate.

filter

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

lastError

Returns the last error encountered during the aggregate calculation.

layer

Returns the associated vector layer.

setDelimiter

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

setFidsFilter

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

setFilter

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

setParameters

Sets all aggregate parameters from a parameter bundle.

stringToAggregate

Converts a string to a aggregate type.

class Aggregate(value)

Bases: enum.IntEnum

Available aggregates to calculate. Not all aggregates are available for all field types.

Added in version 3.36..

  • 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().

baseClass

alias of Qgis

class AggregateInfo
class AggregateInfo(QgsAggregateCalculator.AggregateInfo)

Bases: sip.wrapper

function
name
supportedTypes
class AggregateParameters
class AggregateParameters(QgsAggregateCalculator.AggregateParameters)

Bases: sip.wrapper

delimiter
filter
orderBy
aggregates() List[QgsAggregateCalculator.AggregateInfo]

Structured information for available aggregates.

Added in version 3.2.

Return type:

List[QgsAggregateCalculator.AggregateInfo]

calculate(self, aggregate: Qgis.Aggregate, fieldOrExpression: str, context: QgsExpressionContext = None, feedback: QgsFeedback = None) Tuple[Any, bool]

Calculates the value of an aggregate.

Parameters:
  • aggregate (Qgis.Aggregate) – aggregate to calculate

  • fieldOrExpression (str) – source field or expression to use as basis for aggregated values. If an expression is used, then the context parameter must be set.

  • context (QgsExpressionContext = None) – expression context for evaluating expressions

  • ok – if specified, will be set to True if aggregate calculation was successful. If ok is False then lastError() can be used to retrieve a descriptive error message.

  • feedback (QgsFeedback = None) – optional feedback argument for early cancellation (since QGIS 3.22). If set, this will take precedence over any feedback object set on the expression context.

Return type:

Tuple[Any, bool]

Returns:

calculated aggregate value

delimiter(self) str

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

See also

setDelimiter()

Return type:

str

displayName(aggregate: Qgis.Aggregate) str

Returns the friendly display name for a aggregate.

Added in version 3.22.

Parameters:

aggregate (Qgis.Aggregate)

Return type:

str

filter(self) str

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

See also

setFilter()

Return type:

str

lastError(self) str

Returns the last error encountered during the aggregate calculation.

Added in version 3.22.

Return type:

str

layer(self) QgsVectorLayer

Returns the associated vector layer.

Return type:

QgsVectorLayer

setDelimiter(self, delimiter: str)

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

Parameters:

delimiter (str) – string delimiter

See also

delimiter()

setFidsFilter(self, fids: object)

Sets a filter to limit the features used during the aggregate calculation. If an expression filter is set, it will override this filter.

Parameters:

fids (object) – feature ids for feature filtering, and empty list will return no features.

See also

filter()

setFilter(self, filterExpression: str)

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

Parameters:

filterExpression (str) – expression for filtering features, or empty string to remove filter

See also

filter()

setParameters(self, parameters: QgsAggregateCalculator.AggregateParameters)

Sets all aggregate parameters from a parameter bundle.

Parameters:

parameters (QgsAggregateCalculator.AggregateParameters) – aggregate parameters

stringToAggregate(string: str) Tuple[Qgis.Aggregate, bool]

Converts a string to a aggregate type.

Parameters:
  • string (str) – string to convert

  • ok – if specified, will be set to True if conversion was successful

Return type:

Tuple[Qgis.Aggregate, bool]

Returns:

aggregate type