QGIS API Documentation  3.0.2-Girona (307d082)
qgsaggregatecalculator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaggregatecalculator.cpp
3  --------------------------
4  begin : May 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsaggregatecalculator.h"
19 #include "qgsfeature.h"
20 #include "qgsfeaturerequest.h"
21 #include "qgsfeatureiterator.h"
22 #include "qgsgeometry.h"
23 #include "qgsvectorlayer.h"
24 
25 
26 
28  : mLayer( layer )
29 {
30 
31 }
32 
34 {
35  return mLayer;
36 }
37 
39 {
40  mFilterExpression = parameters.filter;
41  mDelimiter = parameters.delimiter;
42 }
43 
45  const QString &fieldOrExpression,
46  QgsExpressionContext *context, bool *ok ) const
47 {
48  if ( ok )
49  *ok = false;
50 
51  if ( !mLayer )
52  return QVariant();
53 
54  QgsExpressionContext defaultContext = mLayer->createExpressionContext();
55  context = context ? context : &defaultContext;
56 
57  std::unique_ptr<QgsExpression> expression;
58 
59  int attrNum = mLayer->fields().lookupField( fieldOrExpression );
60 
61  if ( attrNum == -1 )
62  {
63  Q_ASSERT( context );
64  context->setFields( mLayer->fields() );
65  // try to use expression
66  expression.reset( new QgsExpression( fieldOrExpression ) );
67 
68  if ( expression->hasParserError() || !expression->prepare( context ) )
69  {
70  return QVariant();
71  }
72  }
73 
74  QSet<QString> lst;
75  if ( !expression )
76  lst.insert( fieldOrExpression );
77  else
78  lst = expression->referencedColumns();
79 
81  .setFlags( ( expression && expression->needsGeometry() ) ?
84  .setSubsetOfAttributes( lst, mLayer->fields() );
85  if ( !mFilterExpression.isEmpty() )
86  request.setFilterExpression( mFilterExpression );
87  if ( context )
88  request.setExpressionContext( *context );
89 
90  //determine result type
91  QVariant::Type resultType = QVariant::Double;
92  if ( attrNum == -1 )
93  {
94  // evaluate first feature, check result type
95  QgsFeatureRequest testRequest( request );
96  testRequest.setLimit( 1 );
97  QgsFeature f;
98  QgsFeatureIterator fit = mLayer->getFeatures( testRequest );
99  if ( !fit.nextFeature( f ) )
100  {
101  //no matching features
102  if ( ok )
103  *ok = true;
104  return defaultValue( aggregate );
105  }
106 
107  if ( context )
108  context->setFeature( f );
109  QVariant v = expression->evaluate( context );
110  resultType = v.type();
111  }
112  else
113  {
114  resultType = mLayer->fields().at( attrNum ).type();
115  }
116 
117  QgsFeatureIterator fit = mLayer->getFeatures( request );
118  return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
119 }
120 
122 {
123  QString normalized = string.trimmed().toLower();
124 
125  if ( ok )
126  *ok = true;
127 
128  if ( normalized == QLatin1String( "count" ) )
129  return Count;
130  else if ( normalized == QLatin1String( "count_distinct" ) )
131  return CountDistinct;
132  else if ( normalized == QLatin1String( "count_missing" ) )
133  return CountMissing;
134  else if ( normalized == QLatin1String( "min" ) )
135  return Min;
136  else if ( normalized == QLatin1String( "max" ) )
137  return Max;
138  else if ( normalized == QLatin1String( "sum" ) )
139  return Sum;
140  else if ( normalized == QLatin1String( "mean" ) )
141  return Mean;
142  else if ( normalized == QLatin1String( "median" ) )
143  return Median;
144  else if ( normalized == QLatin1String( "stdev" ) )
145  return StDev;
146  else if ( normalized == QLatin1String( "stdevsample" ) )
147  return StDevSample;
148  else if ( normalized == QLatin1String( "range" ) )
149  return Range;
150  else if ( normalized == QLatin1String( "minority" ) )
151  return Minority;
152  else if ( normalized == QLatin1String( "majority" ) )
153  return Majority;
154  else if ( normalized == QLatin1String( "q1" ) )
155  return FirstQuartile;
156  else if ( normalized == QLatin1String( "q3" ) )
157  return ThirdQuartile;
158  else if ( normalized == QLatin1String( "iqr" ) )
159  return InterQuartileRange;
160  else if ( normalized == QLatin1String( "min_length" ) )
161  return StringMinimumLength;
162  else if ( normalized == QLatin1String( "max_length" ) )
163  return StringMaximumLength;
164  else if ( normalized == QLatin1String( "concatenate" ) )
165  return StringConcatenate;
166  else if ( normalized == QLatin1String( "collect" ) )
167  return GeometryCollect;
168  else if ( normalized == QLatin1String( "array_agg" ) )
169  return ArrayAggregate;
170 
171  if ( ok )
172  *ok = false;
173 
174  return Count;
175 }
176 
177 QList<QgsAggregateCalculator::AggregateInfo> QgsAggregateCalculator::aggregates()
178 {
179  QList< AggregateInfo > aggregates;
180  aggregates
181  << AggregateInfo
182  {
183  QStringLiteral( "count" ),
184  QCoreApplication::tr( "Count" ),
185  QSet<QVariant::Type>()
186  << QVariant::DateTime
187  << QVariant::Date
188  << QVariant::Int
189  << QVariant::UInt
190  << QVariant::LongLong
191  << QVariant::ULongLong
192  << QVariant::String
193  }
194  << AggregateInfo
195  {
196  QStringLiteral( "count_distinct" ),
197  QCoreApplication::tr( "Count Distinct" ),
198  QSet<QVariant::Type>()
199  << QVariant::DateTime
200  << QVariant::Date
201  << QVariant::UInt
202  << QVariant::Int
203  << QVariant::LongLong
204  << QVariant::ULongLong
205  << QVariant::String
206  }
207  << AggregateInfo
208  {
209  QStringLiteral( "count_missing" ),
210  QCoreApplication::tr( "Count Missing" ),
211  QSet<QVariant::Type>()
212  << QVariant::DateTime
213  << QVariant::Date
214  << QVariant::Int
215  << QVariant::UInt
216  << QVariant::LongLong
217  << QVariant::String
218  }
219  << AggregateInfo
220  {
221  QStringLiteral( "min" ),
222  QCoreApplication::tr( "Min" ),
223  QSet<QVariant::Type>()
224  << QVariant::DateTime
225  << QVariant::Date
226  << QVariant::Int
227  << QVariant::UInt
228  << QVariant::LongLong
229  << QVariant::ULongLong
230  << QVariant::Double
231  << QVariant::String
232  }
233  << AggregateInfo
234  {
235  QStringLiteral( "max" ),
236  QCoreApplication::tr( "Max" ),
237  QSet<QVariant::Type>()
238  << QVariant::DateTime
239  << QVariant::Date
240  << QVariant::Int
241  << QVariant::UInt
242  << QVariant::LongLong
243  << QVariant::ULongLong
244  << QVariant::Double
245  << QVariant::String
246  }
247  << AggregateInfo
248  {
249  QStringLiteral( "sum" ),
250  QCoreApplication::tr( "Sum" ),
251  QSet<QVariant::Type>()
252  << QVariant::Int
253  << QVariant::UInt
254  << QVariant::LongLong
255  << QVariant::ULongLong
256  << QVariant::Double
257  }
258  << AggregateInfo
259  {
260  QStringLiteral( "mean" ),
261  QCoreApplication::tr( "Mean" ),
262  QSet<QVariant::Type>()
263  << QVariant::Int
264  << QVariant::UInt
265  << QVariant::LongLong
266  << QVariant::ULongLong
267  << QVariant::Double
268  }
269  << AggregateInfo
270  {
271  QStringLiteral( "median" ),
272  QCoreApplication::tr( "Median" ),
273  QSet<QVariant::Type>()
274  << QVariant::Int
275  << QVariant::UInt
276  << QVariant::Double
277  }
278  << AggregateInfo
279  {
280  QStringLiteral( "stdev" ),
281  QCoreApplication::tr( "Stdev" ),
282  QSet<QVariant::Type>()
283  << QVariant::Int
284  << QVariant::UInt
285  << QVariant::LongLong
286  << QVariant::ULongLong
287  << QVariant::Double
288  }
289  << AggregateInfo
290  {
291  QStringLiteral( "stdevsample" ),
292  QCoreApplication::tr( "Stdev Sample" ),
293  QSet<QVariant::Type>()
294  << QVariant::Int
295  << QVariant::UInt
296  << QVariant::LongLong
297  << QVariant::ULongLong
298  << QVariant::Double
299  }
300  << AggregateInfo
301  {
302  QStringLiteral( "range" ),
303  QCoreApplication::tr( "Range" ),
304  QSet<QVariant::Type>()
305  << QVariant::Date
306  << QVariant::DateTime
307  << QVariant::Int
308  << QVariant::UInt
309  << QVariant::LongLong
310  << QVariant::ULongLong
311  << QVariant::Double
312  }
313  << AggregateInfo
314  {
315  QStringLiteral( "minority" ),
316  QCoreApplication::tr( "Minority" ),
317  QSet<QVariant::Type>()
318  << QVariant::Int
319  << QVariant::UInt
320  << QVariant::LongLong
321  << QVariant::ULongLong
322  << QVariant::Double
323  }
324  << AggregateInfo
325  {
326  QStringLiteral( "majority" ),
327  QCoreApplication::tr( "Majority" ),
328  QSet<QVariant::Type>()
329  << QVariant::Int
330  << QVariant::UInt
331  << QVariant::LongLong
332  << QVariant::ULongLong
333  << QVariant::Double
334  }
335  << AggregateInfo
336  {
337  QStringLiteral( "q1" ),
338  QCoreApplication::tr( "Q1" ),
339  QSet<QVariant::Type>()
340  << QVariant::Int
341  << QVariant::UInt
342  << QVariant::LongLong
343  << QVariant::ULongLong
344  << QVariant::Double
345  }
346  << AggregateInfo
347  {
348  QStringLiteral( "q3" ),
349  QCoreApplication::tr( "Q3" ),
350  QSet<QVariant::Type>()
351  << QVariant::Int
352  << QVariant::UInt
353  << QVariant::LongLong
354  << QVariant::ULongLong
355  << QVariant::Double
356  }
357  << AggregateInfo
358  {
359  QStringLiteral( "iqr" ),
360  QCoreApplication::tr( "InterQuartileRange" ),
361  QSet<QVariant::Type>()
362  << QVariant::Int
363  << QVariant::UInt
364  << QVariant::LongLong
365  << QVariant::ULongLong
366  << QVariant::Double
367  }
368  << AggregateInfo
369  {
370  QStringLiteral( "min_length" ),
371  QCoreApplication::tr( "Min Length" ),
372  QSet<QVariant::Type>()
373  << QVariant::String
374  }
375  << AggregateInfo
376  {
377  QStringLiteral( "max_length" ),
378  QCoreApplication::tr( "Max Length" ),
379  QSet<QVariant::Type>()
380  << QVariant::String
381  }
382  << AggregateInfo
383  {
384  QStringLiteral( "concatenate" ),
385  QCoreApplication::tr( "Concatenate" ),
386  QSet<QVariant::Type>()
387  << QVariant::String
388  }
389  << AggregateInfo
390  {
391  QStringLiteral( "collect" ),
392  QCoreApplication::tr( "Collect" ),
393  QSet<QVariant::Type>()
394  }
395  << AggregateInfo
396  {
397  QStringLiteral( "array_agg" ),
398  QCoreApplication::tr( "Array Aggregate" ),
399  QSet<QVariant::Type>()
400  };
401 
402  return aggregates;
403 }
404 
405 QVariant QgsAggregateCalculator::calculate( QgsAggregateCalculator::Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
406  int attr, QgsExpression *expression, const QString &delimiter, QgsExpressionContext *context, bool *ok )
407 {
408  if ( ok )
409  *ok = false;
410 
411  if ( aggregate == QgsAggregateCalculator::ArrayAggregate )
412  {
413  if ( ok )
414  *ok = true;
415  return calculateArrayAggregate( fit, attr, expression, context );
416  }
417 
418  switch ( resultType )
419  {
420  case QVariant::Int:
421  case QVariant::UInt:
422  case QVariant::LongLong:
423  case QVariant::ULongLong:
424  case QVariant::Double:
425  {
426  bool statOk = false;
427  QgsStatisticalSummary::Statistic stat = numericStatFromAggregate( aggregate, &statOk );
428  if ( !statOk )
429  return QVariant();
430 
431  if ( ok )
432  *ok = true;
433  return calculateNumericAggregate( fit, attr, expression, context, stat );
434  }
435 
436  case QVariant::Date:
437  case QVariant::DateTime:
438  {
439  bool statOk = false;
440  QgsDateTimeStatisticalSummary::Statistic stat = dateTimeStatFromAggregate( aggregate, &statOk );
441  if ( !statOk )
442  return QVariant();
443 
444  if ( ok )
445  *ok = true;
446  return calculateDateTimeAggregate( fit, attr, expression, context, stat );
447  }
448 
449  case QVariant::UserType:
450  {
451  if ( aggregate == GeometryCollect )
452  {
453  if ( ok )
454  *ok = true;
455  return calculateGeometryAggregate( fit, expression, context );
456  }
457  else
458  {
459  return QVariant();
460  }
461  }
462 
463  default:
464  {
465  // treat as string
466  if ( aggregate == StringConcatenate )
467  {
468  //special case
469  if ( ok )
470  *ok = true;
471  return concatenateStrings( fit, attr, expression, context, delimiter );
472  }
473 
474  bool statOk = false;
475  QgsStringStatisticalSummary::Statistic stat = stringStatFromAggregate( aggregate, &statOk );
476  if ( !statOk )
477  return QVariant();
478 
479  if ( ok )
480  *ok = true;
481  return calculateStringAggregate( fit, attr, expression, context, stat );
482  }
483  }
484 
485 #ifndef _MSC_VER
486  return QVariant();
487 #endif
488 }
489 
490 QgsStatisticalSummary::Statistic QgsAggregateCalculator::numericStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
491 {
492  if ( ok )
493  *ok = true;
494 
495  switch ( aggregate )
496  {
497  case Count:
499  case CountDistinct:
501  case CountMissing:
503  case Min:
505  case Max:
507  case Sum:
509  case Mean:
511  case Median:
513  case StDev:
515  case StDevSample:
517  case Range:
519  case Minority:
521  case Majority:
523  case FirstQuartile:
525  case ThirdQuartile:
527  case InterQuartileRange:
529  case StringMinimumLength:
530  case StringMaximumLength:
531  case StringConcatenate:
532  case GeometryCollect:
533  case ArrayAggregate:
534  {
535  if ( ok )
536  *ok = false;
538  }
539  }
540 
541  if ( ok )
542  *ok = false;
544 }
545 
546 QgsStringStatisticalSummary::Statistic QgsAggregateCalculator::stringStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
547 {
548  if ( ok )
549  *ok = true;
550 
551  switch ( aggregate )
552  {
553  case Count:
555  case CountDistinct:
557  case CountMissing:
559  case Min:
561  case Max:
563  case StringMinimumLength:
565  case StringMaximumLength:
567 
568  case Sum:
569  case Mean:
570  case Median:
571  case StDev:
572  case StDevSample:
573  case Range:
574  case Minority:
575  case Majority:
576  case FirstQuartile:
577  case ThirdQuartile:
578  case InterQuartileRange:
579  case StringConcatenate:
580  case GeometryCollect:
581  case ArrayAggregate:
582  {
583  if ( ok )
584  *ok = false;
586  }
587  }
588 
589  if ( ok )
590  *ok = false;
592 }
593 
594 QgsDateTimeStatisticalSummary::Statistic QgsAggregateCalculator::dateTimeStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
595 {
596  if ( ok )
597  *ok = true;
598 
599  switch ( aggregate )
600  {
601  case Count:
603  case CountDistinct:
605  case CountMissing:
607  case Min:
609  case Max:
611  case Range:
613 
614  case Sum:
615  case Mean:
616  case Median:
617  case StDev:
618  case StDevSample:
619  case Minority:
620  case Majority:
621  case FirstQuartile:
622  case ThirdQuartile:
623  case InterQuartileRange:
624  case StringMinimumLength:
625  case StringMaximumLength:
626  case StringConcatenate:
627  case GeometryCollect:
628  case ArrayAggregate:
629  {
630  if ( ok )
631  *ok = false;
633  }
634  }
635 
636  if ( ok )
637  *ok = false;
639 }
640 
641 QVariant QgsAggregateCalculator::calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
643 {
644  Q_ASSERT( expression || attr >= 0 );
645 
646  QgsStatisticalSummary s( stat );
647  QgsFeature f;
648 
649  while ( fit.nextFeature( f ) )
650  {
651  if ( expression )
652  {
653  Q_ASSERT( context );
654  context->setFeature( f );
655  QVariant v = expression->evaluate( context );
656  s.addVariant( v );
657  }
658  else
659  {
660  s.addVariant( f.attribute( attr ) );
661  }
662  }
663  s.finalize();
664  double val = s.statistic( stat );
665  return std::isnan( val ) ? QVariant() : val;
666 }
667 
668 QVariant QgsAggregateCalculator::calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
670 {
671  Q_ASSERT( expression || attr >= 0 );
672 
673  QgsStringStatisticalSummary s( stat );
674  QgsFeature f;
675 
676  while ( fit.nextFeature( f ) )
677  {
678  if ( expression )
679  {
680  Q_ASSERT( context );
681  context->setFeature( f );
682  QVariant v = expression->evaluate( context );
683  s.addValue( v );
684  }
685  else
686  {
687  s.addValue( f.attribute( attr ) );
688  }
689  }
690  s.finalize();
691  return s.statistic( stat );
692 }
693 
694 QVariant QgsAggregateCalculator::calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context )
695 {
696  Q_ASSERT( expression );
697 
698  QgsFeature f;
699  QVector< QgsGeometry > geometries;
700  while ( fit.nextFeature( f ) )
701  {
702  Q_ASSERT( context );
703  context->setFeature( f );
704  QVariant v = expression->evaluate( context );
705  if ( v.canConvert<QgsGeometry>() )
706  {
707  geometries << v.value<QgsGeometry>();
708  }
709  }
710 
711  return QVariant::fromValue( QgsGeometry::collectGeometry( geometries ) );
712 }
713 
714 QVariant QgsAggregateCalculator::concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
715  QgsExpressionContext *context, const QString &delimiter )
716 {
717  Q_ASSERT( expression || attr >= 0 );
718 
719  QgsFeature f;
720  QString result;
721  while ( fit.nextFeature( f ) )
722  {
723  if ( !result.isEmpty() )
724  result += delimiter;
725 
726  if ( expression )
727  {
728  Q_ASSERT( context );
729  context->setFeature( f );
730  QVariant v = expression->evaluate( context );
731  result += v.toString();
732  }
733  else
734  {
735  result += f.attribute( attr ).toString();
736  }
737  }
738  return result;
739 }
740 
741 QVariant QgsAggregateCalculator::defaultValue( QgsAggregateCalculator::Aggregate aggregate ) const
742 {
743  // value to return when NO features are aggregated:
744  switch ( aggregate )
745  {
746  // sensible values:
747  case Count:
748  case CountDistinct:
749  case CountMissing:
750  return 0;
751 
752  case StringConcatenate:
753  return ""; // zero length string - not null!
754 
755  case ArrayAggregate:
756  return QVariantList(); // empty list
757 
758  // undefined - nothing makes sense here
759  case Sum:
760  case Min:
761  case Max:
762  case Mean:
763  case Median:
764  case StDev:
765  case StDevSample:
766  case Range:
767  case Minority:
768  case Majority:
769  case FirstQuartile:
770  case ThirdQuartile:
771  case InterQuartileRange:
772  case StringMinimumLength:
773  case StringMaximumLength:
774  case GeometryCollect:
775  return QVariant();
776  }
777  return QVariant();
778 }
779 
780 QVariant QgsAggregateCalculator::calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
782 {
783  Q_ASSERT( expression || attr >= 0 );
784 
786  QgsFeature f;
787 
788  while ( fit.nextFeature( f ) )
789  {
790  if ( expression )
791  {
792  Q_ASSERT( context );
793  context->setFeature( f );
794  QVariant v = expression->evaluate( context );
795  s.addValue( v );
796  }
797  else
798  {
799  s.addValue( f.attribute( attr ) );
800  }
801  }
802  s.finalize();
803  return s.statistic( stat );
804 }
805 
806 QVariant QgsAggregateCalculator::calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
807  QgsExpressionContext *context )
808 {
809  Q_ASSERT( expression || attr >= 0 );
810 
811  QgsFeature f;
812 
813  QVariantList array;
814 
815  while ( fit.nextFeature( f ) )
816  {
817  if ( expression )
818  {
819  Q_ASSERT( context );
820  context->setFeature( f );
821  QVariant v = expression->evaluate( context );
822  array.append( v );
823  }
824  else
825  {
826  array.append( f.attribute( attr ) );
827  }
828  }
829  return array;
830 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:299
Wrapper for iterator of features from vector data provider or vector layer.
Third quartile (numeric fields only)
Inter quartile range (IQR) (numeric fields only)
Median of values (numeric fields only)
const QgsVectorLayer * layer() const
Returns the associated vector layer.
Statistic
Enumeration of flags that specify statistics to be calculated.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsAggregateCalculator(const QgsVectorLayer *layer)
Constructor for QgsAggregateCalculator.
First quartile (numeric fields only)
Number of missing (null) values.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
Variety (count of distinct) values.
void finalize()
Must be called after adding all values with addValues() and before retrieving any calculated statisti...
Statistic
Enumeration of flags that specify statistics to be calculated.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
Structured information about the available aggregates.
Interval between earliest and latest datetime value.
void addVariant(const QVariant &value)
Adds a single value to the statistics calculation.
Calculator for summary statistics and aggregates for a list of datetimes.
Sample standard deviation of values.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Standard deviation of values (numeric fields only)
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
void setParameters(const AggregateParameters &parameters)
Sets all aggregate parameters from a parameter bundle.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Number of missing (null) values.
QgsFields fields() const override
Returns the list of fields of this layer.
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
Minimum length of string (string fields only)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Statistic
Enumeration of flags that specify statistics to be calculated.
Create a multipart geometry from aggregated geometries.
Minimum (earliest) datetime value.
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr) const
Calculates the value of an aggregate.
Majority of values (numeric fields only)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
double statistic(QgsStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
Maximum length of string (string fields only)
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
Mean of values (numeric fields only)
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
Calculator for summary statistics and aggregates for a list of strings.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
Standard deviation of values.
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
bool nextFeature(QgsFeature &f)
Minority of values (numeric fields only)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Calculator for summary statistics for a list of doubles.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
Range of values (max - min) (numeric and datetime fields only)
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
QVariant::Type type
Definition: qgsfield.h:55
Sample standard deviation of values (numeric fields only)
Range of values (max - min)
Aggregate
Available aggregates to calculate.
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Set flags that affect how features will be fetched.
A bundle of parameters controlling aggregate calculation.