QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
40 mFilterExpression = parameters.
filter;
63 context = context ? context : &defaultContext;
65 std::unique_ptr<QgsExpression> expression;
76 if ( expression->hasParserError() || !expression->prepare( context ) )
82 lst.insert( fieldOrExpression );
84 lst = expression->referencedColumns();
86 request.
setFlags( ( expression && expression->needsGeometry() ) ?
94 if ( !mOrderBy.empty() )
97 if ( !mFilterExpression.isEmpty() )
102 QVariant::Type resultType = QVariant::Double;
115 return defaultValue( aggregate );
120 QVariant v = expression->evaluate( context );
121 resultType = v.type();
127 return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
132 QString normalized =
string.trimmed().toLower();
137 if ( normalized == QLatin1String(
"count" ) )
139 else if ( normalized == QLatin1String(
"count_distinct" ) )
141 else if ( normalized == QLatin1String(
"count_missing" ) )
143 else if ( normalized == QLatin1String(
"min" ) )
145 else if ( normalized == QLatin1String(
"max" ) )
147 else if ( normalized == QLatin1String(
"sum" ) )
149 else if ( normalized == QLatin1String(
"mean" ) )
151 else if ( normalized == QLatin1String(
"median" ) )
153 else if ( normalized == QLatin1String(
"stdev" ) )
155 else if ( normalized == QLatin1String(
"stdevsample" ) )
157 else if ( normalized == QLatin1String(
"range" ) )
159 else if ( normalized == QLatin1String(
"minority" ) )
161 else if ( normalized == QLatin1String(
"majority" ) )
163 else if ( normalized == QLatin1String(
"q1" ) )
165 else if ( normalized == QLatin1String(
"q3" ) )
167 else if ( normalized == QLatin1String(
"iqr" ) )
169 else if ( normalized == QLatin1String(
"min_length" ) )
171 else if ( normalized == QLatin1String(
"max_length" ) )
173 else if ( normalized == QLatin1String(
"concatenate" ) )
175 else if ( normalized == QLatin1String(
"concatenate_unique" ) )
177 else if ( normalized == QLatin1String(
"collect" ) )
179 else if ( normalized == QLatin1String(
"array_agg" ) )
194 QStringLiteral(
"count" ),
195 QCoreApplication::tr(
"Count" ),
196 QSet<QVariant::Type>()
197 << QVariant::DateTime
201 << QVariant::LongLong
202 << QVariant::ULongLong
207 QStringLiteral(
"count_distinct" ),
208 QCoreApplication::tr(
"Count Distinct" ),
209 QSet<QVariant::Type>()
210 << QVariant::DateTime
214 << QVariant::LongLong
215 << QVariant::ULongLong
220 QStringLiteral(
"count_missing" ),
221 QCoreApplication::tr(
"Count Missing" ),
222 QSet<QVariant::Type>()
223 << QVariant::DateTime
227 << QVariant::LongLong
232 QStringLiteral(
"min" ),
233 QCoreApplication::tr(
"Min" ),
234 QSet<QVariant::Type>()
235 << QVariant::DateTime
239 << QVariant::LongLong
240 << QVariant::ULongLong
246 QStringLiteral(
"max" ),
247 QCoreApplication::tr(
"Max" ),
248 QSet<QVariant::Type>()
249 << QVariant::DateTime
253 << QVariant::LongLong
254 << QVariant::ULongLong
260 QStringLiteral(
"sum" ),
261 QCoreApplication::tr(
"Sum" ),
262 QSet<QVariant::Type>()
265 << QVariant::LongLong
266 << QVariant::ULongLong
271 QStringLiteral(
"mean" ),
272 QCoreApplication::tr(
"Mean" ),
273 QSet<QVariant::Type>()
276 << QVariant::LongLong
277 << QVariant::ULongLong
282 QStringLiteral(
"median" ),
283 QCoreApplication::tr(
"Median" ),
284 QSet<QVariant::Type>()
291 QStringLiteral(
"stdev" ),
292 QCoreApplication::tr(
"Stdev" ),
293 QSet<QVariant::Type>()
296 << QVariant::LongLong
297 << QVariant::ULongLong
302 QStringLiteral(
"stdevsample" ),
303 QCoreApplication::tr(
"Stdev Sample" ),
304 QSet<QVariant::Type>()
307 << QVariant::LongLong
308 << QVariant::ULongLong
313 QStringLiteral(
"range" ),
314 QCoreApplication::tr(
"Range" ),
315 QSet<QVariant::Type>()
317 << QVariant::DateTime
320 << QVariant::LongLong
321 << QVariant::ULongLong
326 QStringLiteral(
"minority" ),
327 QCoreApplication::tr(
"Minority" ),
328 QSet<QVariant::Type>()
331 << QVariant::LongLong
332 << QVariant::ULongLong
338 QStringLiteral(
"majority" ),
339 QCoreApplication::tr(
"Majority" ),
340 QSet<QVariant::Type>()
343 << QVariant::LongLong
344 << QVariant::ULongLong
350 QStringLiteral(
"q1" ),
351 QCoreApplication::tr(
"Q1" ),
352 QSet<QVariant::Type>()
355 << QVariant::LongLong
356 << QVariant::ULongLong
361 QStringLiteral(
"q3" ),
362 QCoreApplication::tr(
"Q3" ),
363 QSet<QVariant::Type>()
366 << QVariant::LongLong
367 << QVariant::ULongLong
372 QStringLiteral(
"iqr" ),
373 QCoreApplication::tr(
"InterQuartileRange" ),
374 QSet<QVariant::Type>()
377 << QVariant::LongLong
378 << QVariant::ULongLong
383 QStringLiteral(
"min_length" ),
384 QCoreApplication::tr(
"Min Length" ),
385 QSet<QVariant::Type>()
390 QStringLiteral(
"max_length" ),
391 QCoreApplication::tr(
"Max Length" ),
392 QSet<QVariant::Type>()
397 QStringLiteral(
"concatenate" ),
398 QCoreApplication::tr(
"Concatenate" ),
399 QSet<QVariant::Type>()
404 QStringLiteral(
"collect" ),
405 QCoreApplication::tr(
"Collect" ),
406 QSet<QVariant::Type>()
410 QStringLiteral(
"array_agg" ),
411 QCoreApplication::tr(
"Array Aggregate" ),
412 QSet<QVariant::Type>()
428 return calculateArrayAggregate( fit, attr, expression, context );
431 switch ( resultType )
435 case QVariant::LongLong:
436 case QVariant::ULongLong:
437 case QVariant::Double:
446 return calculateNumericAggregate( fit, attr, expression, context, stat );
450 case QVariant::DateTime:
459 return calculateDateTimeAggregate( fit, attr, expression, context, stat );
462 case QVariant::UserType:
468 return calculateGeometryAggregate( fit, expression, context );
484 return concatenateStrings( fit, attr, expression, context,
delimiter );
491 return concatenateStrings( fit, attr, expression, context,
delimiter,
true );
501 return calculateStringAggregate( fit, attr, expression, context, stat );
669 Q_ASSERT( expression || attr >= 0 );
680 QVariant v = expression->
evaluate( context );
689 double val = s.statistic( stat );
690 return std::isnan( val ) ? QVariant() : val;
696 Q_ASSERT( expression || attr >= 0 );
707 QVariant v = expression->
evaluate( context );
716 return s.statistic( stat );
721 Q_ASSERT( expression );
724 QVector< QgsGeometry > geometries;
729 QVariant v = expression->
evaluate( context );
742 Q_ASSERT( expression || attr >= 0 );
753 QVariant v = expression->
evaluate( context );
754 result = v.toString();
761 if ( !unique || !results.contains( result ) )
784 return QVariantList();
811 Q_ASSERT( expression || attr >= 0 );
822 QVariant v = expression->
evaluate( context );
831 return s.statistic( stat );
837 Q_ASSERT( expression || attr >= 0 );
849 QVariant v = expression->
evaluate( context );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
@ Range
Range of values (max - min) (numeric and datetime fields only)
@ CountMissing
Number of missing (null) values.
Calculator for summary statistics for a list of doubles.
@ Minority
Minority of strings.
@ StDev
Standard deviation of values.
@ Minority
Minority of values.
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr) const
Calculates the value of an aggregate.
A bundle of parameters controlling aggregate calculation.
@ CountDistinct
Number of distinct values.
@ StringConcatenateUnique
Concatenate unique values with a joining string (string fields only). Specify the delimiter using set...
@ CountMissing
Number of missing (null) values.
@ InterQuartileRange
Inter quartile range (IQR)
@ ArrayAggregate
Create an array of values.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
@ ThirdQuartile
Third quartile.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
@ InterQuartileRange
Inter quartile range (IQR) (numeric fields only)
This class wraps a request for features to a vector layer (or directly its vector data provider).
@ StDevSample
Sample standard deviation of values (numeric fields only)
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
@ Minority
Minority of values.
@ StDev
Standard deviation of values (numeric fields only)
@ Max
Maximum string value.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ StDevSample
Sample standard deviation of values.
@ StringMinimumLength
Minimum length of string (string fields only)
@ CountDistinct
Number of distinct string values.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Range
Range of values (max - min)
@ Max
Maximum (latest) datetime value.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
@ Majority
Majority of values.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
@ Median
Median of values (numeric fields only)
@ CountMissing
Number of missing (null) values.
QSet< QgsFeatureId > QgsFeatureIds
@ StringConcatenate
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
QVariant evaluate()
Evaluate the feature and return the result.
@ Mean
Mean of values (numeric fields only)
Statistic
Enumeration of flags that specify statistics to be calculated.
@ ThirdQuartile
Third quartile (numeric fields only)
@ Median
Median of values.
QgsAggregateCalculator(const QgsVectorLayer *layer)
Constructor for QgsAggregateCalculator.
Aggregate
Available aggregates to calculate.
bool nextFeature(QgsFeature &f)
A geometry is the spatial representation of a feature.
@ Majority
Majority of values.
Represents a vector layer which manages a vector based data sets.
@ MaximumLength
Maximum length of string.
const QgsVectorLayer * layer() const
Returns the associated vector layer.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
void setFidsFilter(const QgsFeatureIds &fids)
Sets a filter to limit the features used during the aggregate calculation.
void setParameters(const AggregateParameters ¶meters)
Sets all aggregate parameters from a parameter bundle.
@ CountMissing
Number of missing (null) values.
@ FirstQuartile
First quartile.
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
@ Min
Minimum (earliest) datetime value.
Structured information about the available aggregates.
@ FirstQuartile
First quartile (numeric fields only)
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
@ GeometryCollect
Create a multipart geometry from aggregated geometries.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Class for parsing and evaluation of expressions (formerly called "search strings").
@ CountDistinct
Number of distinct datetime values.
@ MinimumLength
Minimum length of string.
@ Range
Interval between earliest and latest datetime value.
Wrapper for iterator of features from vector data provider or vector layer.
@ Majority
Majority of strings.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
@ StringMaximumLength
Maximum length of string (string fields only)
Calculator for summary statistics and aggregates for a list of strings.
Calculator for summary statistics and aggregates for a list of datetimes.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
@ Min
Minimum string value.
@ Variety
Variety (count of distinct) values.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.