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