QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  mOrderBy = parameters.orderBy;
43 }
44 
46 {
47  mFidsSet = true;
48  mFidsFilter = fids;
49 }
50 
52  const QString &fieldOrExpression, QgsExpressionContext *context, bool *ok ) const
53 {
54  if ( ok )
55  *ok = false;
56 
58 
59  if ( !mLayer )
60  return QVariant();
61 
62  QgsExpressionContext defaultContext = mLayer->createExpressionContext();
63  context = context ? context : &defaultContext;
64 
65  std::unique_ptr<QgsExpression> expression;
66 
67  int attrNum = mLayer->fields().lookupField( fieldOrExpression );
68 
69  if ( attrNum == -1 )
70  {
71  Q_ASSERT( context );
72  context->setFields( mLayer->fields() );
73  // try to use expression
74  expression.reset( new QgsExpression( fieldOrExpression ) );
75 
76  if ( expression->hasParserError() || !expression->prepare( context ) )
77  return QVariant();
78  }
79 
80  QSet<QString> lst;
81  if ( !expression )
82  lst.insert( fieldOrExpression );
83  else
84  lst = expression->referencedColumns();
85 
86  request.setFlags( ( expression && expression->needsGeometry() ) ?
89  .setSubsetOfAttributes( lst, mLayer->fields() );
90 
91  if ( mFidsSet )
92  request.setFilterFids( mFidsFilter );
93 
94  if ( !mOrderBy.empty() )
95  request.setOrderBy( mOrderBy );
96 
97  if ( !mFilterExpression.isEmpty() )
98  request.setFilterExpression( mFilterExpression );
99  if ( context )
100  request.setExpressionContext( *context );
101  //determine result type
102  QVariant::Type resultType = QVariant::Double;
103  if ( attrNum == -1 )
104  {
105  // evaluate first feature, check result type
106  QgsFeatureRequest testRequest( request );
107  testRequest.setLimit( 1 );
108  QgsFeature f;
109  QgsFeatureIterator fit = mLayer->getFeatures( testRequest );
110  if ( !fit.nextFeature( f ) )
111  {
112  //no matching features
113  if ( ok )
114  *ok = true;
115  return defaultValue( aggregate );
116  }
117 
118  if ( context )
119  context->setFeature( f );
120  QVariant v = expression->evaluate( context );
121  resultType = v.type();
122  }
123  else
124  resultType = mLayer->fields().at( attrNum ).type();
125 
126  QgsFeatureIterator fit = mLayer->getFeatures( request );
127  return calculate( aggregate, fit, resultType, attrNum, expression.get(), mDelimiter, context, ok );
128 }
129 
131 {
132  QString normalized = string.trimmed().toLower();
133 
134  if ( ok )
135  *ok = true;
136 
137  if ( normalized == QLatin1String( "count" ) )
138  return Count;
139  else if ( normalized == QLatin1String( "count_distinct" ) )
140  return CountDistinct;
141  else if ( normalized == QLatin1String( "count_missing" ) )
142  return CountMissing;
143  else if ( normalized == QLatin1String( "min" ) )
144  return Min;
145  else if ( normalized == QLatin1String( "max" ) )
146  return Max;
147  else if ( normalized == QLatin1String( "sum" ) )
148  return Sum;
149  else if ( normalized == QLatin1String( "mean" ) )
150  return Mean;
151  else if ( normalized == QLatin1String( "median" ) )
152  return Median;
153  else if ( normalized == QLatin1String( "stdev" ) )
154  return StDev;
155  else if ( normalized == QLatin1String( "stdevsample" ) )
156  return StDevSample;
157  else if ( normalized == QLatin1String( "range" ) )
158  return Range;
159  else if ( normalized == QLatin1String( "minority" ) )
160  return Minority;
161  else if ( normalized == QLatin1String( "majority" ) )
162  return Majority;
163  else if ( normalized == QLatin1String( "q1" ) )
164  return FirstQuartile;
165  else if ( normalized == QLatin1String( "q3" ) )
166  return ThirdQuartile;
167  else if ( normalized == QLatin1String( "iqr" ) )
168  return InterQuartileRange;
169  else if ( normalized == QLatin1String( "min_length" ) )
170  return StringMinimumLength;
171  else if ( normalized == QLatin1String( "max_length" ) )
172  return StringMaximumLength;
173  else if ( normalized == QLatin1String( "concatenate" ) )
174  return StringConcatenate;
175  else if ( normalized == QLatin1String( "concatenate_unique" ) )
177  else if ( normalized == QLatin1String( "collect" ) )
178  return GeometryCollect;
179  else if ( normalized == QLatin1String( "array_agg" ) )
180  return ArrayAggregate;
181 
182  if ( ok )
183  *ok = false;
184 
185  return Count;
186 }
187 
188 QList<QgsAggregateCalculator::AggregateInfo> QgsAggregateCalculator::aggregates()
189 {
190  QList< AggregateInfo > aggregates;
191  aggregates
192  << AggregateInfo
193  {
194  QStringLiteral( "count" ),
195  QCoreApplication::tr( "Count" ),
196  QSet<QVariant::Type>()
197  << QVariant::DateTime
198  << QVariant::Date
199  << QVariant::Int
200  << QVariant::UInt
201  << QVariant::LongLong
202  << QVariant::ULongLong
203  << QVariant::String
204  }
205  << AggregateInfo
206  {
207  QStringLiteral( "count_distinct" ),
208  QCoreApplication::tr( "Count Distinct" ),
209  QSet<QVariant::Type>()
210  << QVariant::DateTime
211  << QVariant::Date
212  << QVariant::UInt
213  << QVariant::Int
214  << QVariant::LongLong
215  << QVariant::ULongLong
216  << QVariant::String
217  }
218  << AggregateInfo
219  {
220  QStringLiteral( "count_missing" ),
221  QCoreApplication::tr( "Count Missing" ),
222  QSet<QVariant::Type>()
223  << QVariant::DateTime
224  << QVariant::Date
225  << QVariant::Int
226  << QVariant::UInt
227  << QVariant::LongLong
228  << QVariant::String
229  }
230  << AggregateInfo
231  {
232  QStringLiteral( "min" ),
233  QCoreApplication::tr( "Min" ),
234  QSet<QVariant::Type>()
235  << QVariant::DateTime
236  << QVariant::Date
237  << QVariant::Int
238  << QVariant::UInt
239  << QVariant::LongLong
240  << QVariant::ULongLong
241  << QVariant::Double
242  << QVariant::String
243  }
244  << AggregateInfo
245  {
246  QStringLiteral( "max" ),
247  QCoreApplication::tr( "Max" ),
248  QSet<QVariant::Type>()
249  << QVariant::DateTime
250  << QVariant::Date
251  << QVariant::Int
252  << QVariant::UInt
253  << QVariant::LongLong
254  << QVariant::ULongLong
255  << QVariant::Double
256  << QVariant::String
257  }
258  << AggregateInfo
259  {
260  QStringLiteral( "sum" ),
261  QCoreApplication::tr( "Sum" ),
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( "mean" ),
272  QCoreApplication::tr( "Mean" ),
273  QSet<QVariant::Type>()
274  << QVariant::Int
275  << QVariant::UInt
276  << QVariant::LongLong
277  << QVariant::ULongLong
278  << QVariant::Double
279  }
280  << AggregateInfo
281  {
282  QStringLiteral( "median" ),
283  QCoreApplication::tr( "Median" ),
284  QSet<QVariant::Type>()
285  << QVariant::Int
286  << QVariant::UInt
287  << QVariant::Double
288  }
289  << AggregateInfo
290  {
291  QStringLiteral( "stdev" ),
292  QCoreApplication::tr( "Stdev" ),
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( "stdevsample" ),
303  QCoreApplication::tr( "Stdev Sample" ),
304  QSet<QVariant::Type>()
305  << QVariant::Int
306  << QVariant::UInt
307  << QVariant::LongLong
308  << QVariant::ULongLong
309  << QVariant::Double
310  }
311  << AggregateInfo
312  {
313  QStringLiteral( "range" ),
314  QCoreApplication::tr( "Range" ),
315  QSet<QVariant::Type>()
316  << QVariant::Date
317  << QVariant::DateTime
318  << QVariant::Int
319  << QVariant::UInt
320  << QVariant::LongLong
321  << QVariant::ULongLong
322  << QVariant::Double
323  }
324  << AggregateInfo
325  {
326  QStringLiteral( "minority" ),
327  QCoreApplication::tr( "Minority" ),
328  QSet<QVariant::Type>()
329  << QVariant::Int
330  << QVariant::UInt
331  << QVariant::LongLong
332  << QVariant::ULongLong
333  << QVariant::Double
334  << QVariant::String
335  }
336  << AggregateInfo
337  {
338  QStringLiteral( "majority" ),
339  QCoreApplication::tr( "Majority" ),
340  QSet<QVariant::Type>()
341  << QVariant::Int
342  << QVariant::UInt
343  << QVariant::LongLong
344  << QVariant::ULongLong
345  << QVariant::Double
346  << QVariant::String
347  }
348  << AggregateInfo
349  {
350  QStringLiteral( "q1" ),
351  QCoreApplication::tr( "Q1" ),
352  QSet<QVariant::Type>()
353  << QVariant::Int
354  << QVariant::UInt
355  << QVariant::LongLong
356  << QVariant::ULongLong
357  << QVariant::Double
358  }
359  << AggregateInfo
360  {
361  QStringLiteral( "q3" ),
362  QCoreApplication::tr( "Q3" ),
363  QSet<QVariant::Type>()
364  << QVariant::Int
365  << QVariant::UInt
366  << QVariant::LongLong
367  << QVariant::ULongLong
368  << QVariant::Double
369  }
370  << AggregateInfo
371  {
372  QStringLiteral( "iqr" ),
373  QCoreApplication::tr( "InterQuartileRange" ),
374  QSet<QVariant::Type>()
375  << QVariant::Int
376  << QVariant::UInt
377  << QVariant::LongLong
378  << QVariant::ULongLong
379  << QVariant::Double
380  }
381  << AggregateInfo
382  {
383  QStringLiteral( "min_length" ),
384  QCoreApplication::tr( "Min Length" ),
385  QSet<QVariant::Type>()
386  << QVariant::String
387  }
388  << AggregateInfo
389  {
390  QStringLiteral( "max_length" ),
391  QCoreApplication::tr( "Max Length" ),
392  QSet<QVariant::Type>()
393  << QVariant::String
394  }
395  << AggregateInfo
396  {
397  QStringLiteral( "concatenate" ),
398  QCoreApplication::tr( "Concatenate" ),
399  QSet<QVariant::Type>()
400  << QVariant::String
401  }
402  << AggregateInfo
403  {
404  QStringLiteral( "collect" ),
405  QCoreApplication::tr( "Collect" ),
406  QSet<QVariant::Type>()
407  }
408  << AggregateInfo
409  {
410  QStringLiteral( "array_agg" ),
411  QCoreApplication::tr( "Array Aggregate" ),
412  QSet<QVariant::Type>()
413  };
414 
415  return aggregates;
416 }
417 
418 QVariant QgsAggregateCalculator::calculate( QgsAggregateCalculator::Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
419  int attr, QgsExpression *expression, const QString &delimiter, QgsExpressionContext *context, bool *ok )
420 {
421  if ( ok )
422  *ok = false;
423 
424  if ( aggregate == QgsAggregateCalculator::ArrayAggregate )
425  {
426  if ( ok )
427  *ok = true;
428  return calculateArrayAggregate( fit, attr, expression, context );
429  }
430 
431  switch ( resultType )
432  {
433  case QVariant::Int:
434  case QVariant::UInt:
435  case QVariant::LongLong:
436  case QVariant::ULongLong:
437  case QVariant::Double:
438  {
439  bool statOk = false;
440  QgsStatisticalSummary::Statistic stat = numericStatFromAggregate( aggregate, &statOk );
441  if ( !statOk )
442  return QVariant();
443 
444  if ( ok )
445  *ok = true;
446  return calculateNumericAggregate( fit, attr, expression, context, stat );
447  }
448 
449  case QVariant::Date:
450  case QVariant::DateTime:
451  {
452  bool statOk = false;
453  QgsDateTimeStatisticalSummary::Statistic stat = dateTimeStatFromAggregate( aggregate, &statOk );
454  if ( !statOk )
455  return QVariant();
456 
457  if ( ok )
458  *ok = true;
459  return calculateDateTimeAggregate( fit, attr, expression, context, stat );
460  }
461 
462  case QVariant::UserType:
463  {
464  if ( aggregate == GeometryCollect )
465  {
466  if ( ok )
467  *ok = true;
468  return calculateGeometryAggregate( fit, expression, context );
469  }
470  else
471  {
472  return QVariant();
473  }
474  }
475 
476  default:
477  {
478  // treat as string
479  if ( aggregate == StringConcatenate )
480  {
481  //special case
482  if ( ok )
483  *ok = true;
484  return concatenateStrings( fit, attr, expression, context, delimiter );
485  }
486  else if ( aggregate == StringConcatenateUnique )
487  {
488  //special case
489  if ( ok )
490  *ok = true;
491  return concatenateStrings( fit, attr, expression, context, delimiter, true );
492  }
493 
494  bool statOk = false;
495  QgsStringStatisticalSummary::Statistic stat = stringStatFromAggregate( aggregate, &statOk );
496  if ( !statOk )
497  return QVariant();
498 
499  if ( ok )
500  *ok = true;
501  return calculateStringAggregate( fit, attr, expression, context, stat );
502  }
503  }
504 
505 #ifndef _MSC_VER
506  return QVariant();
507 #endif
508 }
509 
510 QgsStatisticalSummary::Statistic QgsAggregateCalculator::numericStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
511 {
512  if ( ok )
513  *ok = true;
514 
515  switch ( aggregate )
516  {
517  case Count:
519  case CountDistinct:
521  case CountMissing:
523  case Min:
525  case Max:
527  case Sum:
529  case Mean:
531  case Median:
533  case StDev:
535  case StDevSample:
537  case Range:
539  case Minority:
541  case Majority:
543  case FirstQuartile:
545  case ThirdQuartile:
547  case InterQuartileRange:
549  case StringMinimumLength:
550  case StringMaximumLength:
551  case StringConcatenate:
553  case GeometryCollect:
554  case ArrayAggregate:
555  {
556  if ( ok )
557  *ok = false;
559  }
560  }
561 
562  if ( ok )
563  *ok = false;
565 }
566 
567 QgsStringStatisticalSummary::Statistic QgsAggregateCalculator::stringStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
568 {
569  if ( ok )
570  *ok = true;
571 
572  switch ( aggregate )
573  {
574  case Count:
576  case CountDistinct:
578  case CountMissing:
580  case Min:
582  case Max:
584  case StringMinimumLength:
586  case StringMaximumLength:
588  case Minority:
590  case Majority:
592 
593  case Sum:
594  case Mean:
595  case Median:
596  case StDev:
597  case StDevSample:
598  case Range:
599  case FirstQuartile:
600  case ThirdQuartile:
601  case InterQuartileRange:
602  case StringConcatenate:
604  case GeometryCollect:
605  case ArrayAggregate:
606  {
607  if ( ok )
608  *ok = false;
610  }
611  }
612 
613  if ( ok )
614  *ok = false;
616 }
617 
618 QgsDateTimeStatisticalSummary::Statistic QgsAggregateCalculator::dateTimeStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
619 {
620  if ( ok )
621  *ok = true;
622 
623  switch ( aggregate )
624  {
625  case Count:
627  case CountDistinct:
629  case CountMissing:
631  case Min:
633  case Max:
635  case Range:
637 
638  case Sum:
639  case Mean:
640  case Median:
641  case StDev:
642  case StDevSample:
643  case Minority:
644  case Majority:
645  case FirstQuartile:
646  case ThirdQuartile:
647  case InterQuartileRange:
648  case StringMinimumLength:
649  case StringMaximumLength:
650  case StringConcatenate:
652  case GeometryCollect:
653  case ArrayAggregate:
654  {
655  if ( ok )
656  *ok = false;
658  }
659  }
660 
661  if ( ok )
662  *ok = false;
664 }
665 
666 QVariant QgsAggregateCalculator::calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
668 {
669  Q_ASSERT( expression || attr >= 0 );
670 
671  QgsStatisticalSummary s( stat );
672  QgsFeature f;
673 
674  while ( fit.nextFeature( f ) )
675  {
676  if ( expression )
677  {
678  Q_ASSERT( context );
679  context->setFeature( f );
680  QVariant v = expression->evaluate( context );
681  s.addVariant( v );
682  }
683  else
684  {
685  s.addVariant( f.attribute( attr ) );
686  }
687  }
688  s.finalize();
689  double val = s.statistic( stat );
690  return std::isnan( val ) ? QVariant() : val;
691 }
692 
693 QVariant QgsAggregateCalculator::calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
695 {
696  Q_ASSERT( expression || attr >= 0 );
697 
698  QgsStringStatisticalSummary s( stat );
699  QgsFeature f;
700 
701  while ( fit.nextFeature( f ) )
702  {
703  if ( expression )
704  {
705  Q_ASSERT( context );
706  context->setFeature( f );
707  QVariant v = expression->evaluate( context );
708  s.addValue( v );
709  }
710  else
711  {
712  s.addValue( f.attribute( attr ) );
713  }
714  }
715  s.finalize();
716  return s.statistic( stat );
717 }
718 
719 QVariant QgsAggregateCalculator::calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context )
720 {
721  Q_ASSERT( expression );
722 
723  QgsFeature f;
724  QVector< QgsGeometry > geometries;
725  while ( fit.nextFeature( f ) )
726  {
727  Q_ASSERT( context );
728  context->setFeature( f );
729  QVariant v = expression->evaluate( context );
730  if ( v.canConvert<QgsGeometry>() )
731  {
732  geometries << v.value<QgsGeometry>();
733  }
734  }
735 
736  return QVariant::fromValue( QgsGeometry::collectGeometry( geometries ) );
737 }
738 
739 QVariant QgsAggregateCalculator::concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
740  QgsExpressionContext *context, const QString &delimiter, bool unique )
741 {
742  Q_ASSERT( expression || attr >= 0 );
743 
744  QgsFeature f;
745  QStringList results;
746  while ( fit.nextFeature( f ) )
747  {
748  QString result;
749  if ( expression )
750  {
751  Q_ASSERT( context );
752  context->setFeature( f );
753  QVariant v = expression->evaluate( context );
754  result = v.toString();
755  }
756  else
757  {
758  result = f.attribute( attr ).toString();
759  }
760 
761  if ( !unique || !results.contains( result ) )
762  results << result;
763  }
764 
765  return results.join( delimiter );
766 }
767 
768 QVariant QgsAggregateCalculator::defaultValue( QgsAggregateCalculator::Aggregate aggregate ) const
769 {
770  // value to return when NO features are aggregated:
771  switch ( aggregate )
772  {
773  // sensible values:
774  case Count:
775  case CountDistinct:
776  case CountMissing:
777  return 0;
778 
779  case StringConcatenate:
781  return ""; // zero length string - not null!
782 
783  case ArrayAggregate:
784  return QVariantList(); // empty list
785 
786  // undefined - nothing makes sense here
787  case Sum:
788  case Min:
789  case Max:
790  case Mean:
791  case Median:
792  case StDev:
793  case StDevSample:
794  case Range:
795  case Minority:
796  case Majority:
797  case FirstQuartile:
798  case ThirdQuartile:
799  case InterQuartileRange:
800  case StringMinimumLength:
801  case StringMaximumLength:
802  case GeometryCollect:
803  return QVariant();
804  }
805  return QVariant();
806 }
807 
808 QVariant QgsAggregateCalculator::calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
810 {
811  Q_ASSERT( expression || attr >= 0 );
812 
814  QgsFeature f;
815 
816  while ( fit.nextFeature( f ) )
817  {
818  if ( expression )
819  {
820  Q_ASSERT( context );
821  context->setFeature( f );
822  QVariant v = expression->evaluate( context );
823  s.addValue( v );
824  }
825  else
826  {
827  s.addValue( f.attribute( attr ) );
828  }
829  }
830  s.finalize();
831  return s.statistic( stat );
832 }
833 
834 QVariant QgsAggregateCalculator::calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
835  QgsExpressionContext *context )
836 {
837  Q_ASSERT( expression || attr >= 0 );
838 
839  QgsFeature f;
840 
841  QVariantList array;
842 
843  while ( fit.nextFeature( f ) )
844  {
845  if ( expression )
846  {
847  Q_ASSERT( context );
848  context->setFeature( f );
849  QVariant v = expression->evaluate( context );
850  array.append( v );
851  }
852  else
853  {
854  array.append( f.attribute( attr ) );
855  }
856  }
857  return array;
858 }
859 
QgsFeatureRequest::NoGeometry
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition: qgsfeaturerequest.h:107
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsAggregateCalculator::AggregateParameters::delimiter
QString delimiter
Delimiter to use for joining values with the StringConcatenate aggregate.
Definition: qgsaggregatecalculator.h:107
QgsAggregateCalculator::Range
@ Range
Range of values (max - min) (numeric and datetime fields only)
Definition: qgsaggregatecalculator.h:76
QgsDateTimeStatisticalSummary::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsdatetimestatisticalsummary.h:54
QgsStatisticalSummary
Calculator for summary statistics for a list of doubles.
Definition: qgsstatisticalsummary.h:43
qgsfeaturerequest.h
QgsStringStatisticalSummary::Minority
@ Minority
Minority of strings.
Definition: qgsstringstatisticalsummary.h:59
QgsStatisticalSummary::StDev
@ StDev
Standard deviation of values.
Definition: qgsstatisticalsummary.h:55
QgsAggregateCalculator::Minority
@ Minority
Minority of values.
Definition: qgsaggregatecalculator.h:77
QgsAggregateCalculator::calculate
QVariant calculate(Aggregate aggregate, const QString &fieldOrExpression, QgsExpressionContext *context=nullptr, bool *ok=nullptr) const
Calculates the value of an aggregate.
Definition: qgsaggregatecalculator.cpp:51
QgsAggregateCalculator::AggregateParameters
A bundle of parameters controlling aggregate calculation.
Definition: qgsaggregatecalculator.h:91
QgsAggregateCalculator::CountDistinct
@ CountDistinct
Number of distinct values.
Definition: qgsaggregatecalculator.h:67
QgsAggregateCalculator::Min
@ Min
Min of values.
Definition: qgsaggregatecalculator.h:69
qgsfeatureiterator.h
QgsAggregateCalculator::StringConcatenateUnique
@ StringConcatenateUnique
Concatenate unique values with a joining string (string fields only). Specify the delimiter using set...
Definition: qgsaggregatecalculator.h:87
QgsAggregateCalculator::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsaggregatecalculator.h:68
qgsfeature.h
QgsStatisticalSummary::InterQuartileRange
@ InterQuartileRange
Inter quartile range (IQR)
Definition: qgsstatisticalsummary.h:65
QgsAggregateCalculator::ArrayAggregate
@ ArrayAggregate
Create an array of values.
Definition: qgsaggregatecalculator.h:86
QgsAggregateCalculator::Count
@ Count
Count.
Definition: qgsaggregatecalculator.h:66
QgsExpressionContext::setFields
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Definition: qgsexpressioncontext.cpp:553
QgsStatisticalSummary::ThirdQuartile
@ ThirdQuartile
Third quartile.
Definition: qgsstatisticalsummary.h:64
QgsFeatureRequest::setSubsetOfAttributes
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Definition: qgsfeaturerequest.cpp:190
QgsAggregateCalculator::AggregateParameters::orderBy
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
Definition: qgsaggregatecalculator.h:113
QgsAggregateCalculator::aggregates
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
Definition: qgsaggregatecalculator.cpp:188
QgsFeatureRequest::setExpressionContext
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.cpp:149
qgsaggregatecalculator.h
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:129
QgsStatisticalSummary::Sum
@ Sum
Sum of values.
Definition: qgsstatisticalsummary.h:52
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsAggregateCalculator::InterQuartileRange
@ InterQuartileRange
Inter quartile range (IQR) (numeric fields only)
Definition: qgsaggregatecalculator.h:81
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsAggregateCalculator::StDevSample
@ StDevSample
Sample standard deviation of values (numeric fields only)
Definition: qgsaggregatecalculator.h:75
QgsAggregateCalculator::delimiter
QString delimiter() const
Returns the delimiter used for joining values with the StringConcatenate aggregate.
Definition: qgsaggregatecalculator.h:165
QgsFeatureRequest::NoFlags
@ NoFlags
Definition: qgsfeaturerequest.h:106
QgsStatisticalSummary::Minority
@ Minority
Minority of values.
Definition: qgsstatisticalsummary.h:60
QgsAggregateCalculator::StDev
@ StDev
Standard deviation of values (numeric fields only)
Definition: qgsaggregatecalculator.h:74
QgsStringStatisticalSummary::Max
@ Max
Maximum string value.
Definition: qgsstringstatisticalsummary.h:55
QgsStringStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstringstatisticalsummary.h:49
QgsStatisticalSummary::StDevSample
@ StDevSample
Sample standard deviation of values.
Definition: qgsstatisticalsummary.h:56
QgsAggregateCalculator::StringMinimumLength
@ StringMinimumLength
Minimum length of string (string fields only)
Definition: qgsaggregatecalculator.h:82
QgsStringStatisticalSummary::CountDistinct
@ CountDistinct
Number of distinct string values.
Definition: qgsstringstatisticalsummary.h:52
QgsStringStatisticalSummary::Count
@ Count
Count.
Definition: qgsstringstatisticalsummary.h:51
QgsDateTimeStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsdatetimestatisticalsummary.h:50
QgsStatisticalSummary::Range
@ Range
Range of values (max - min)
Definition: qgsstatisticalsummary.h:59
QgsDateTimeStatisticalSummary::Max
@ Max
Maximum (latest) datetime value.
Definition: qgsdatetimestatisticalsummary.h:56
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QgsStatisticalSummary::Majority
@ Majority
Majority of values.
Definition: qgsstatisticalsummary.h:61
QgsFeatureRequest::setFilterFids
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
Definition: qgsfeaturerequest.cpp:110
QgsAggregateCalculator::Median
@ Median
Median of values (numeric fields only)
Definition: qgsaggregatecalculator.h:73
QgsAggregateCalculator::Max
@ Max
Max of values.
Definition: qgsaggregatecalculator.h:70
QgsStatisticalSummary::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsstatisticalsummary.h:51
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsAggregateCalculator::StringConcatenate
@ StringConcatenate
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
Definition: qgsaggregatecalculator.h:84
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:346
qgsvectorlayer.h
QgsAggregateCalculator::Mean
@ Mean
Mean of values (numeric fields only)
Definition: qgsaggregatecalculator.h:72
QgsStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstatisticalsummary.h:48
QgsAggregateCalculator::ThirdQuartile
@ ThirdQuartile
Third quartile (numeric fields only)
Definition: qgsaggregatecalculator.h:80
QgsStatisticalSummary::Median
@ Median
Median of values.
Definition: qgsstatisticalsummary.h:54
QgsAggregateCalculator::QgsAggregateCalculator
QgsAggregateCalculator(const QgsVectorLayer *layer)
Constructor for QgsAggregateCalculator.
Definition: qgsaggregatecalculator.cpp:27
qgsgeometry.h
QgsAggregateCalculator::Aggregate
Aggregate
Available aggregates to calculate.
Definition: qgsaggregatecalculator.h:64
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsGeometry
Definition: qgsgeometry.h:122
QgsAggregateCalculator::Majority
@ Majority
Majority of values.
Definition: qgsaggregatecalculator.h:78
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsStatisticalSummary::Min
@ Min
Min of values.
Definition: qgsstatisticalsummary.h:57
QgsStringStatisticalSummary::MaximumLength
@ MaximumLength
Maximum length of string.
Definition: qgsstringstatisticalsummary.h:57
QgsAggregateCalculator::layer
const QgsVectorLayer * layer() const
Returns the associated vector layer.
Definition: qgsaggregatecalculator.cpp:33
QgsFeatureRequest::setLimit
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
Definition: qgsfeaturerequest.cpp:178
QgsAggregateCalculator::setFidsFilter
void setFidsFilter(const QgsFeatureIds &fids)
Sets a filter to limit the features used during the aggregate calculation.
Definition: qgsaggregatecalculator.cpp:45
QgsDateTimeStatisticalSummary::Count
@ Count
Count.
Definition: qgsdatetimestatisticalsummary.h:52
QgsAggregateCalculator::setParameters
void setParameters(const AggregateParameters &parameters)
Sets all aggregate parameters from a parameter bundle.
Definition: qgsaggregatecalculator.cpp:38
QgsStringStatisticalSummary::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsstringstatisticalsummary.h:53
QgsStatisticalSummary::FirstQuartile
@ FirstQuartile
First quartile.
Definition: qgsstatisticalsummary.h:63
QgsFeatureRequest::setOrderBy
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
Definition: qgsfeaturerequest.cpp:172
QgsAggregateCalculator::stringToAggregate
static Aggregate stringToAggregate(const QString &string, bool *ok=nullptr)
Converts a string to a aggregate type.
Definition: qgsaggregatecalculator.cpp:130
QgsDateTimeStatisticalSummary::Min
@ Min
Minimum (earliest) datetime value.
Definition: qgsdatetimestatisticalsummary.h:55
QgsAggregateCalculator::AggregateInfo
Structured information about the available aggregates.
Definition: qgsaggregatecalculator.h:53
QgsStatisticalSummary::Mean
@ Mean
Mean of values.
Definition: qgsstatisticalsummary.h:53
QgsAggregateCalculator::FirstQuartile
@ FirstQuartile
First quartile (numeric fields only)
Definition: qgsaggregatecalculator.h:79
QgsStatisticalSummary::Max
@ Max
Max of values.
Definition: qgsstatisticalsummary.h:58
QgsAggregateCalculator::Sum
@ Sum
Sum of values.
Definition: qgsaggregatecalculator.h:71
QgsAggregateCalculator::AggregateParameters::filter
QString filter
Optional filter for calculating aggregate over a subset of features, or an empty string to use all fe...
Definition: qgsaggregatecalculator.h:100
QgsAggregateCalculator::GeometryCollect
@ GeometryCollect
Create a multipart geometry from aggregated geometries.
Definition: qgsaggregatecalculator.h:85
QgsFeature
Definition: qgsfeature.h:55
QgsStatisticalSummary::Count
@ Count
Count.
Definition: qgsstatisticalsummary.h:50
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:324
QgsVectorLayer::createExpressionContext
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsvectorlayer.cpp:4933
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsExpression
Definition: qgsexpression.h:113
QgsDateTimeStatisticalSummary::CountDistinct
@ CountDistinct
Number of distinct datetime values.
Definition: qgsdatetimestatisticalsummary.h:53
QgsStringStatisticalSummary::MinimumLength
@ MinimumLength
Minimum length of string.
Definition: qgsstringstatisticalsummary.h:56
QgsDateTimeStatisticalSummary::Range
@ Range
Interval between earliest and latest datetime value.
Definition: qgsdatetimestatisticalsummary.h:57
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsStringStatisticalSummary::Majority
@ Majority
Majority of strings.
Definition: qgsstringstatisticalsummary.h:60
QgsFeatureRequest::setFlags
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
Definition: qgsfeaturerequest.cpp:184
QgsAggregateCalculator::StringMaximumLength
@ StringMaximumLength
Maximum length of string (string fields only)
Definition: qgsaggregatecalculator.h:83
QgsField::type
QVariant::Type type
Definition: qgsfield.h:57
QgsStringStatisticalSummary
Calculator for summary statistics and aggregates for a list of strings.
Definition: qgsstringstatisticalsummary.h:44
QgsDateTimeStatisticalSummary
Calculator for summary statistics and aggregates for a list of datetimes.
Definition: qgsdatetimestatisticalsummary.h:45
QgsGeometry::collectGeometry
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
Definition: qgsgeometry.cpp:247
QgsStringStatisticalSummary::Min
@ Min
Minimum string value.
Definition: qgsstringstatisticalsummary.h:54
QgsStatisticalSummary::Variety
@ Variety
Variety (count of distinct) values.
Definition: qgsstatisticalsummary.h:62
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:521