QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  }
335  << AggregateInfo
336  {
337  QStringLiteral( "majority" ),
338  QCoreApplication::tr( "Majority" ),
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( "q1" ),
349  QCoreApplication::tr( "Q1" ),
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( "q3" ),
360  QCoreApplication::tr( "Q3" ),
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( "iqr" ),
371  QCoreApplication::tr( "InterQuartileRange" ),
372  QSet<QVariant::Type>()
373  << QVariant::Int
374  << QVariant::UInt
375  << QVariant::LongLong
376  << QVariant::ULongLong
377  << QVariant::Double
378  }
379  << AggregateInfo
380  {
381  QStringLiteral( "min_length" ),
382  QCoreApplication::tr( "Min Length" ),
383  QSet<QVariant::Type>()
384  << QVariant::String
385  }
386  << AggregateInfo
387  {
388  QStringLiteral( "max_length" ),
389  QCoreApplication::tr( "Max Length" ),
390  QSet<QVariant::Type>()
391  << QVariant::String
392  }
393  << AggregateInfo
394  {
395  QStringLiteral( "concatenate" ),
396  QCoreApplication::tr( "Concatenate" ),
397  QSet<QVariant::Type>()
398  << QVariant::String
399  }
400  << AggregateInfo
401  {
402  QStringLiteral( "collect" ),
403  QCoreApplication::tr( "Collect" ),
404  QSet<QVariant::Type>()
405  }
406  << AggregateInfo
407  {
408  QStringLiteral( "array_agg" ),
409  QCoreApplication::tr( "Array Aggregate" ),
410  QSet<QVariant::Type>()
411  };
412 
413  return aggregates;
414 }
415 
416 QVariant QgsAggregateCalculator::calculate( QgsAggregateCalculator::Aggregate aggregate, QgsFeatureIterator &fit, QVariant::Type resultType,
417  int attr, QgsExpression *expression, const QString &delimiter, QgsExpressionContext *context, bool *ok )
418 {
419  if ( ok )
420  *ok = false;
421 
422  if ( aggregate == QgsAggregateCalculator::ArrayAggregate )
423  {
424  if ( ok )
425  *ok = true;
426  return calculateArrayAggregate( fit, attr, expression, context );
427  }
428 
429  switch ( resultType )
430  {
431  case QVariant::Int:
432  case QVariant::UInt:
433  case QVariant::LongLong:
434  case QVariant::ULongLong:
435  case QVariant::Double:
436  {
437  bool statOk = false;
438  QgsStatisticalSummary::Statistic stat = numericStatFromAggregate( aggregate, &statOk );
439  if ( !statOk )
440  return QVariant();
441 
442  if ( ok )
443  *ok = true;
444  return calculateNumericAggregate( fit, attr, expression, context, stat );
445  }
446 
447  case QVariant::Date:
448  case QVariant::DateTime:
449  {
450  bool statOk = false;
451  QgsDateTimeStatisticalSummary::Statistic stat = dateTimeStatFromAggregate( aggregate, &statOk );
452  if ( !statOk )
453  return QVariant();
454 
455  if ( ok )
456  *ok = true;
457  return calculateDateTimeAggregate( fit, attr, expression, context, stat );
458  }
459 
460  case QVariant::UserType:
461  {
462  if ( aggregate == GeometryCollect )
463  {
464  if ( ok )
465  *ok = true;
466  return calculateGeometryAggregate( fit, expression, context );
467  }
468  else
469  {
470  return QVariant();
471  }
472  }
473 
474  default:
475  {
476  // treat as string
477  if ( aggregate == StringConcatenate )
478  {
479  //special case
480  if ( ok )
481  *ok = true;
482  return concatenateStrings( fit, attr, expression, context, delimiter );
483  }
484  else if ( aggregate == StringConcatenateUnique )
485  {
486  //special case
487  if ( ok )
488  *ok = true;
489  return concatenateStrings( fit, attr, expression, context, delimiter, true );
490  }
491 
492  bool statOk = false;
493  QgsStringStatisticalSummary::Statistic stat = stringStatFromAggregate( aggregate, &statOk );
494  if ( !statOk )
495  return QVariant();
496 
497  if ( ok )
498  *ok = true;
499  return calculateStringAggregate( fit, attr, expression, context, stat );
500  }
501  }
502 
503 #ifndef _MSC_VER
504  return QVariant();
505 #endif
506 }
507 
508 QgsStatisticalSummary::Statistic QgsAggregateCalculator::numericStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
509 {
510  if ( ok )
511  *ok = true;
512 
513  switch ( aggregate )
514  {
515  case Count:
517  case CountDistinct:
519  case CountMissing:
521  case Min:
523  case Max:
525  case Sum:
527  case Mean:
529  case Median:
531  case StDev:
533  case StDevSample:
535  case Range:
537  case Minority:
539  case Majority:
541  case FirstQuartile:
543  case ThirdQuartile:
545  case InterQuartileRange:
547  case StringMinimumLength:
548  case StringMaximumLength:
549  case StringConcatenate:
551  case GeometryCollect:
552  case ArrayAggregate:
553  {
554  if ( ok )
555  *ok = false;
557  }
558  }
559 
560  if ( ok )
561  *ok = false;
563 }
564 
565 QgsStringStatisticalSummary::Statistic QgsAggregateCalculator::stringStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
566 {
567  if ( ok )
568  *ok = true;
569 
570  switch ( aggregate )
571  {
572  case Count:
574  case CountDistinct:
576  case CountMissing:
578  case Min:
580  case Max:
582  case StringMinimumLength:
584  case StringMaximumLength:
586 
587  case Sum:
588  case Mean:
589  case Median:
590  case StDev:
591  case StDevSample:
592  case Range:
593  case Minority:
594  case Majority:
595  case FirstQuartile:
596  case ThirdQuartile:
597  case InterQuartileRange:
598  case StringConcatenate:
600  case GeometryCollect:
601  case ArrayAggregate:
602  {
603  if ( ok )
604  *ok = false;
606  }
607  }
608 
609  if ( ok )
610  *ok = false;
612 }
613 
614 QgsDateTimeStatisticalSummary::Statistic QgsAggregateCalculator::dateTimeStatFromAggregate( QgsAggregateCalculator::Aggregate aggregate, bool *ok )
615 {
616  if ( ok )
617  *ok = true;
618 
619  switch ( aggregate )
620  {
621  case Count:
623  case CountDistinct:
625  case CountMissing:
627  case Min:
629  case Max:
631  case Range:
633 
634  case Sum:
635  case Mean:
636  case Median:
637  case StDev:
638  case StDevSample:
639  case Minority:
640  case Majority:
641  case FirstQuartile:
642  case ThirdQuartile:
643  case InterQuartileRange:
644  case StringMinimumLength:
645  case StringMaximumLength:
646  case StringConcatenate:
648  case GeometryCollect:
649  case ArrayAggregate:
650  {
651  if ( ok )
652  *ok = false;
654  }
655  }
656 
657  if ( ok )
658  *ok = false;
660 }
661 
662 QVariant QgsAggregateCalculator::calculateNumericAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
664 {
665  Q_ASSERT( expression || attr >= 0 );
666 
667  QgsStatisticalSummary s( stat );
668  QgsFeature f;
669 
670  while ( fit.nextFeature( f ) )
671  {
672  if ( expression )
673  {
674  Q_ASSERT( context );
675  context->setFeature( f );
676  QVariant v = expression->evaluate( context );
677  s.addVariant( v );
678  }
679  else
680  {
681  s.addVariant( f.attribute( attr ) );
682  }
683  }
684  s.finalize();
685  double val = s.statistic( stat );
686  return std::isnan( val ) ? QVariant() : val;
687 }
688 
689 QVariant QgsAggregateCalculator::calculateStringAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
691 {
692  Q_ASSERT( expression || attr >= 0 );
693 
694  QgsStringStatisticalSummary s( stat );
695  QgsFeature f;
696 
697  while ( fit.nextFeature( f ) )
698  {
699  if ( expression )
700  {
701  Q_ASSERT( context );
702  context->setFeature( f );
703  QVariant v = expression->evaluate( context );
704  s.addValue( v );
705  }
706  else
707  {
708  s.addValue( f.attribute( attr ) );
709  }
710  }
711  s.finalize();
712  return s.statistic( stat );
713 }
714 
715 QVariant QgsAggregateCalculator::calculateGeometryAggregate( QgsFeatureIterator &fit, QgsExpression *expression, QgsExpressionContext *context )
716 {
717  Q_ASSERT( expression );
718 
719  QgsFeature f;
720  QVector< QgsGeometry > geometries;
721  while ( fit.nextFeature( f ) )
722  {
723  Q_ASSERT( context );
724  context->setFeature( f );
725  QVariant v = expression->evaluate( context );
726  if ( v.canConvert<QgsGeometry>() )
727  {
728  geometries << v.value<QgsGeometry>();
729  }
730  }
731 
732  return QVariant::fromValue( QgsGeometry::collectGeometry( geometries ) );
733 }
734 
735 QVariant QgsAggregateCalculator::concatenateStrings( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
736  QgsExpressionContext *context, const QString &delimiter, bool unique )
737 {
738  Q_ASSERT( expression || attr >= 0 );
739 
740  QgsFeature f;
741  QStringList results;
742  while ( fit.nextFeature( f ) )
743  {
744  QString result;
745  if ( expression )
746  {
747  Q_ASSERT( context );
748  context->setFeature( f );
749  QVariant v = expression->evaluate( context );
750  result = v.toString();
751  }
752  else
753  {
754  result = f.attribute( attr ).toString();
755  }
756 
757  if ( !unique || !results.contains( result ) )
758  results << result;
759  }
760 
761  return results.join( delimiter );
762 }
763 
764 QVariant QgsAggregateCalculator::defaultValue( QgsAggregateCalculator::Aggregate aggregate ) const
765 {
766  // value to return when NO features are aggregated:
767  switch ( aggregate )
768  {
769  // sensible values:
770  case Count:
771  case CountDistinct:
772  case CountMissing:
773  return 0;
774 
775  case StringConcatenate:
777  return ""; // zero length string - not null!
778 
779  case ArrayAggregate:
780  return QVariantList(); // empty list
781 
782  // undefined - nothing makes sense here
783  case Sum:
784  case Min:
785  case Max:
786  case Mean:
787  case Median:
788  case StDev:
789  case StDevSample:
790  case Range:
791  case Minority:
792  case Majority:
793  case FirstQuartile:
794  case ThirdQuartile:
795  case InterQuartileRange:
796  case StringMinimumLength:
797  case StringMaximumLength:
798  case GeometryCollect:
799  return QVariant();
800  }
801  return QVariant();
802 }
803 
804 QVariant QgsAggregateCalculator::calculateDateTimeAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
806 {
807  Q_ASSERT( expression || attr >= 0 );
808 
810  QgsFeature f;
811 
812  while ( fit.nextFeature( f ) )
813  {
814  if ( expression )
815  {
816  Q_ASSERT( context );
817  context->setFeature( f );
818  QVariant v = expression->evaluate( context );
819  s.addValue( v );
820  }
821  else
822  {
823  s.addValue( f.attribute( attr ) );
824  }
825  }
826  s.finalize();
827  return s.statistic( stat );
828 }
829 
830 QVariant QgsAggregateCalculator::calculateArrayAggregate( QgsFeatureIterator &fit, int attr, QgsExpression *expression,
831  QgsExpressionContext *context )
832 {
833  Q_ASSERT( expression || attr >= 0 );
834 
835  QgsFeature f;
836 
837  QVariantList array;
838 
839  while ( fit.nextFeature( f ) )
840  {
841  if ( expression )
842  {
843  Q_ASSERT( context );
844  context->setFeature( f );
845  QVariant v = expression->evaluate( context );
846  array.append( v );
847  }
848  else
849  {
850  array.append( f.attribute( attr ) );
851  }
852  }
853  return array;
854 }
855 
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:324
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
Third quartile (numeric fields only)
Inter quartile range (IQR) (numeric fields only)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
Median of values (numeric fields only)
const QgsVectorLayer * layer() const
Returns the associated vector layer.
void setFidsFilter(const QgsFeatureIds &fids)
Sets a filter to limit the features used during the aggregate calculation.
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.
QVariant evaluate()
Evaluate the feature and return the result.
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:55
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.
Concatenate unique values with a joining string (string fields only). Specify the delimiter using set...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
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 FINAL
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.
QgsFeatureRequest::OrderBy orderBy
Optional order by clauses.
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.
Minimum (earliest) datetime value.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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...
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...
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
Mean of values (numeric fields only)
Create a multipart geometry from aggregated geometries.
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimit...
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
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:262
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:56
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)
Sets flags that affect how features will be fetched.
A bundle of parameters controlling aggregate calculation.