Navigation Menu

Skip to content

Commit

Permalink
New utility class QgsAggregateCalculator, simplifies calculating
Browse files Browse the repository at this point in the history
aggregates from vector layer fields and expressions
  • Loading branch information
nyalldawson committed May 17, 2016
1 parent 1c45b94 commit 50e41c8
Show file tree
Hide file tree
Showing 7 changed files with 908 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -21,6 +21,7 @@
%Include qgsapplication.sip
%Include qgsaction.sip
%Include qgsactionmanager.sip
%Include qgsaggregatecalculator.sip
%Include qgsattributeaction.sip
%Include qgsattributetableconfig.sip
%Include qgsbrowsermodel.sip
Expand Down
72 changes: 72 additions & 0 deletions python/core/qgsaggregatecalculator.sip
@@ -0,0 +1,72 @@
/** \ingroup core
* \class QgsAggregateCalculator
* \brief Utility class for calculating aggregates for a field (or expression) over the features
* from a vector layer.
* \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)
};

/** Constructor for QgsAggregateCalculator.
* @param layer vector layer to calculate aggregate from
*/
QgsAggregateCalculator( QgsVectorLayer* layer );

/** Returns the associated vector layer.
*/
QgsVectorLayer* layer() const;

/** 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;

/** 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;


};
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -79,6 +79,7 @@ SET(QGIS_CORE_SRCS
qgsapplication.cpp
qgsaction.cpp
qgsactionmanager.cpp
qgsaggregatecalculator.cpp
qgsattributetableconfig.cpp
qgsbrowsermodel.cpp
qgscachedfeatureiterator.cpp
Expand Down Expand Up @@ -597,6 +598,7 @@ SET(QGIS_CORE_HDRS

qgis.h
qgsaction.h
qgsaggregatecalculator.h
qgsattributetableconfig.h
qgsattributeaction.h
qgscachedfeatureiterator.h
Expand Down

0 comments on commit 50e41c8

Please sign in to comment.