QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsstringstatisticalsummary.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstringstatisticalsummary.h
3  -----------------------------
4  Date : May 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSSTRINGSTATISTICALSUMMARY_H
17 #define QGSSTRINGSTATISTICALSUMMARY_H
18 
19 #include <QSet>
20 #include <QVariantList>
21 
22 #include "qgis_core.h"
23 #include "qgis.h"
24 
25 /***************************************************************************
26  * This class is considered CRITICAL and any change MUST be accompanied with
27  * full unit tests in test_qgsstringstatisticalsummary.py.
28  * See details in QEP #17
29  ****************************************************************************/
30 
44 class CORE_EXPORT QgsStringStatisticalSummary
45 {
46  public:
47 
49  enum Statistic
50  {
51  Count = 1,
52  CountDistinct = 2,
53  CountMissing = 4,
54  Min = 8,
55  Max = 16,
56  MinimumLength = 32,
57  MaximumLength = 64,
58  MeanLength = 128,
59  Minority = 256,
60  Majority = 512,
61  All = Count | CountDistinct | CountMissing | Min | Max | MinimumLength | MaximumLength | MeanLength | Minority | Majority,
62  };
63  Q_DECLARE_FLAGS( Statistics, Statistic )
64 
65 
69  QgsStringStatisticalSummary( QgsStringStatisticalSummary::Statistics stats = QgsStringStatisticalSummary::All );
70 
76  Statistics statistics() const { return mStatistics; }
77 
84  void setStatistics( QgsStringStatisticalSummary::Statistics stats ) { mStatistics = stats; }
85 
89  void reset();
90 
97  void calculate( const QStringList &values );
98 
106  void calculateFromVariants( const QVariantList &values );
107 
122  void addString( const QString &string );
123 
137  void addValue( const QVariant &value );
138 
144  void finalize();
145 
151  QVariant statistic( QgsStringStatisticalSummary::Statistic stat ) const;
152 
156  int count() const { return mCount; }
157 
162  int countDistinct() const { return mValues.keys().count(); }
163 
168  QSet< QString > distinctValues() const { return qgis::listToSet( mValues.keys() ); }
169 
173  int countMissing() const { return mCountMissing; }
174 
178  QString min() const { return mMin; }
179 
183  QString max() const { return mMax; }
184 
188  int minLength() const { return mMinLength; }
189 
193  int maxLength() const { return mMaxLength; }
194 
199  double meanLength() const { return mMeanLength; }
200 
209  QString minority() const { return mMinority; }
210 
219  QString majority() const { return mMajority; }
220 
225  static QString displayName( QgsStringStatisticalSummary::Statistic statistic );
226 
227  private:
228 
229  Statistics mStatistics;
230 
231  int mCount;
232  QMap< QString, int > mValues;
233  int mCountMissing;
234  QString mMin;
235  QString mMax;
236  int mMinLength;
237  int mMaxLength;
238  long mSumLengths;
239  double mMeanLength;
240  QString mMinority;
241  QString mMajority;
242 
243  void testString( const QString &string );
244 };
245 
246 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsStringStatisticalSummary::Statistics )
247 
248 #endif // QGSSTRINGSTATISTICALSUMMARY_H
QgsStringStatisticalSummary::count
int count() const
Returns the calculated count of values.
Definition: qgsstringstatisticalsummary.h:156
QgsStringStatisticalSummary::distinctValues
QSet< QString > distinctValues() const
Returns the set of distinct string values.
Definition: qgsstringstatisticalsummary.h:168
qgis.h
QgsStringStatisticalSummary::min
QString min() const
Returns the minimum (non-null) string value.
Definition: qgsstringstatisticalsummary.h:178
QgsStringStatisticalSummary::minLength
int minLength() const
Returns the minimum length of strings.
Definition: qgsstringstatisticalsummary.h:188
QgsStringStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstringstatisticalsummary.h:49
QgsStringStatisticalSummary::max
QString max() const
Returns the maximum (non-null) string value.
Definition: qgsstringstatisticalsummary.h:183
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsStringStatisticalSummary::setStatistics
void setStatistics(QgsStringStatisticalSummary::Statistics stats)
Sets flags which specify which statistics will be calculated.
Definition: qgsstringstatisticalsummary.h:84
QgsStringStatisticalSummary::statistics
Statistics statistics() const
Returns flags which specify which statistics will be calculated.
Definition: qgsstringstatisticalsummary.h:76
QgsStringStatisticalSummary::majority
QString majority() const
Returns the most common string.
Definition: qgsstringstatisticalsummary.h:219
QgsStringStatisticalSummary::countMissing
int countMissing() const
Returns the number of missing (null) string values.
Definition: qgsstringstatisticalsummary.h:173
QgsStringStatisticalSummary::minority
QString minority() const
Returns the least common string.
Definition: qgsstringstatisticalsummary.h:209
QgsStringStatisticalSummary::countDistinct
int countDistinct() const
Returns the number of distinct string values.
Definition: qgsstringstatisticalsummary.h:162
QgsStringStatisticalSummary::All
@ All
All statistics.
Definition: qgsstringstatisticalsummary.h:61
QgsStringStatisticalSummary::maxLength
int maxLength() const
Returns the maximum length of strings.
Definition: qgsstringstatisticalsummary.h:193
QgsStringStatisticalSummary
Calculator for summary statistics and aggregates for a list of strings.
Definition: qgsstringstatisticalsummary.h:44
QgsStringStatisticalSummary::meanLength
double meanLength() const
Returns the mean length of strings.
Definition: qgsstringstatisticalsummary.h:199