2016-05-16 12:01:14 +10:00
|
|
|
/** \ingroup core
|
|
|
|
* \class QgsAggregateCalculator
|
|
|
|
* \brief Utility class for calculating aggregates for a field (or expression) over the features
|
2016-05-16 14:36:04 +10:00
|
|
|
* 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.
|
2016-05-16 12:01:14 +10:00
|
|
|
* \note added in QGIS 2.16
|
|
|
|
*/
|
|
|
|
class QgsAggregateCalculator
|
|
|
|
{
|
|
|
|
|
|
|
|
%TypeHeaderCode
|
|
|
|
#include <qgsaggregatecalculator.h>
|
|
|
|
%End
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
//! Available aggregates to calculate. Not all aggregates are available for all field
|
|
|
|
//! types.
|
|
|
|
enum Aggregate
|
|
|
|
{
|
|
|
|
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)
|
[FEATURE] Aggregates for expressions
This commit adds a number of different forms of aggregates to
the expression engine.
1. Aggregates within the current layer, eg sum("passengers")
Supports sub expressions (ie sum("passengers"/2) ), group by
( sum("passengers", group_by:="line_segment") ), and optional
filters ( sum("passengers", filter:= "station_class" > 3 ) )
2. Relational aggregates, which calculate an aggregate over
all matching child features from a relation, eg
relation_aggregate( 'my_relation', 'mean', "some_child_field" )
3. A summary aggregate function, for calculating aggregates
on other layers. Eg aggregate('rail_station_layer','sum',"passengers")
The summary aggregate function supports an optional filter,
making it possible to calculate things like:
aggregate('rail_stations','sum',"passengers",
intersects(@atlas_geometry, $geometry ) )
for calculating the total number of passengers for the stations
inside the current atlas feature
In all cases the calculations are cached inside the expression
context, so they only need to be calculated once for each
set of expression evaluations.
Sponsored by Kanton of Zug, Switzerland
2016-05-16 17:30:07 +10:00
|
|
|
StringConcatenate, //! Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimiter().
|
2016-08-22 13:57:03 +07:00
|
|
|
GeometryCollect, //! Create a multipart geometry from aggregated geometries
|
[FEATURE] Aggregates for expressions
This commit adds a number of different forms of aggregates to
the expression engine.
1. Aggregates within the current layer, eg sum("passengers")
Supports sub expressions (ie sum("passengers"/2) ), group by
( sum("passengers", group_by:="line_segment") ), and optional
filters ( sum("passengers", filter:= "station_class" > 3 ) )
2. Relational aggregates, which calculate an aggregate over
all matching child features from a relation, eg
relation_aggregate( 'my_relation', 'mean', "some_child_field" )
3. A summary aggregate function, for calculating aggregates
on other layers. Eg aggregate('rail_station_layer','sum',"passengers")
The summary aggregate function supports an optional filter,
making it possible to calculate things like:
aggregate('rail_stations','sum',"passengers",
intersects(@atlas_geometry, $geometry ) )
for calculating the total number of passengers for the stations
inside the current atlas feature
In all cases the calculations are cached inside the expression
context, so they only need to be calculated once for each
set of expression evaluations.
Sponsored by Kanton of Zug, Switzerland
2016-05-16 17:30:07 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
//! A bundle of parameters controlling aggregate calculation
|
|
|
|
struct AggregateParameters
|
|
|
|
{
|
|
|
|
/** Optional filter for calculating aggregate over a subset of features, or an
|
|
|
|
* empty string to use all features.
|
|
|
|
* @see QgsAggregateCalculator::setFilter()
|
|
|
|
* @see QgsAggregateCalculator::filter()
|
|
|
|
*/
|
|
|
|
QString filter;
|
|
|
|
|
|
|
|
/** Delimiter to use for joining values with the StringConcatenate aggregate.
|
|
|
|
* @see QgsAggregateCalculator::setDelimiter()
|
|
|
|
* @see QgsAggregateCalculator::delimiter()
|
|
|
|
*/
|
|
|
|
QString delimiter;
|
2016-05-16 12:01:14 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Constructor for QgsAggregateCalculator.
|
|
|
|
* @param layer vector layer to calculate aggregate from
|
|
|
|
*/
|
2016-07-11 12:06:06 +10:00
|
|
|
QgsAggregateCalculator( const QgsVectorLayer* layer );
|
2016-05-16 12:01:14 +10:00
|
|
|
|
|
|
|
/** Returns the associated vector layer.
|
|
|
|
*/
|
2016-07-11 12:06:06 +10:00
|
|
|
const QgsVectorLayer* layer() const;
|
2016-05-16 12:01:14 +10:00
|
|
|
|
[FEATURE] Aggregates for expressions
This commit adds a number of different forms of aggregates to
the expression engine.
1. Aggregates within the current layer, eg sum("passengers")
Supports sub expressions (ie sum("passengers"/2) ), group by
( sum("passengers", group_by:="line_segment") ), and optional
filters ( sum("passengers", filter:= "station_class" > 3 ) )
2. Relational aggregates, which calculate an aggregate over
all matching child features from a relation, eg
relation_aggregate( 'my_relation', 'mean', "some_child_field" )
3. A summary aggregate function, for calculating aggregates
on other layers. Eg aggregate('rail_station_layer','sum',"passengers")
The summary aggregate function supports an optional filter,
making it possible to calculate things like:
aggregate('rail_stations','sum',"passengers",
intersects(@atlas_geometry, $geometry ) )
for calculating the total number of passengers for the stations
inside the current atlas feature
In all cases the calculations are cached inside the expression
context, so they only need to be calculated once for each
set of expression evaluations.
Sponsored by Kanton of Zug, Switzerland
2016-05-16 17:30:07 +10:00
|
|
|
/** Sets all aggregate parameters from a parameter bundle.
|
|
|
|
* @param parameters aggregate parameters
|
|
|
|
*/
|
|
|
|
void setParameters( const AggregateParameters& parameters );
|
|
|
|
|
2016-05-16 12:01:14 +10:00
|
|
|
/** Sets a filter to limit the features used during the aggregate calculation.
|
|
|
|
* @param filterExpression expression for filtering features, or empty string to remove filter
|
|
|
|
* @see filter()
|
|
|
|
*/
|
|
|
|
void setFilter( const QString& filterExpression );
|
|
|
|
|
|
|
|
/** Returns the filter which limits the features used during the aggregate calculation.
|
|
|
|
* @see setFilter()
|
|
|
|
*/
|
|
|
|
QString filter() const;
|
|
|
|
|
[FEATURE] Aggregates for expressions
This commit adds a number of different forms of aggregates to
the expression engine.
1. Aggregates within the current layer, eg sum("passengers")
Supports sub expressions (ie sum("passengers"/2) ), group by
( sum("passengers", group_by:="line_segment") ), and optional
filters ( sum("passengers", filter:= "station_class" > 3 ) )
2. Relational aggregates, which calculate an aggregate over
all matching child features from a relation, eg
relation_aggregate( 'my_relation', 'mean', "some_child_field" )
3. A summary aggregate function, for calculating aggregates
on other layers. Eg aggregate('rail_station_layer','sum',"passengers")
The summary aggregate function supports an optional filter,
making it possible to calculate things like:
aggregate('rail_stations','sum',"passengers",
intersects(@atlas_geometry, $geometry ) )
for calculating the total number of passengers for the stations
inside the current atlas feature
In all cases the calculations are cached inside the expression
context, so they only need to be calculated once for each
set of expression evaluations.
Sponsored by Kanton of Zug, Switzerland
2016-05-16 17:30:07 +10:00
|
|
|
/** Sets the delimiter to use for joining values with the StringConcatenate aggregate.
|
|
|
|
* @param delimiter string delimiter
|
|
|
|
* @see delimiter()
|
|
|
|
*/
|
|
|
|
void setDelimiter( const QString& delimiter );
|
|
|
|
|
|
|
|
/** Returns the delimiter used for joining values with the StringConcatenate aggregate.
|
|
|
|
* @see setDelimiter()
|
|
|
|
*/
|
|
|
|
QString delimiter() const;
|
|
|
|
|
2016-05-16 12:01:14 +10:00
|
|
|
/** Calculates the value of an aggregate.
|
|
|
|
* @param aggregate aggregate to calculate
|
|
|
|
* @param fieldOrExpression source field or expression to use as basis for aggregated values.
|
|
|
|
* If an expression is used, then the context parameter must be set.
|
|
|
|
* @param context expression context for evaluating expressions
|
|
|
|
* @param ok if specified, will be set to true if aggregate calculation was successful
|
|
|
|
* @returns calculated aggregate value
|
|
|
|
*/
|
|
|
|
QVariant calculate( Aggregate aggregate, const QString& fieldOrExpression,
|
|
|
|
QgsExpressionContext* context = nullptr, bool* ok = nullptr ) const;
|
|
|
|
|
2016-05-16 15:00:55 +10:00
|
|
|
/** Converts a string to a aggregate type.
|
|
|
|
* @param string string to convert
|
|
|
|
* @param ok if specified, will be set to true if conversion was successful
|
|
|
|
* @returns aggregate type
|
|
|
|
*/
|
|
|
|
static Aggregate stringToAggregate( const QString& string, bool* ok = nullptr );
|
2016-05-16 12:01:14 +10:00
|
|
|
|
|
|
|
};
|