QGIS API Documentation  3.6.0-Noosa (5873452)
qgsstringstatisticalsummary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstringstatisticalsummary.cpp
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 
17 #include <QString>
18 #include <QStringList>
19 #include <QObject>
20 #include <QVariant>
21 #include <QVariantList>
22 #include <limits>
23 
24 /***************************************************************************
25  * This class is considered CRITICAL and any change MUST be accompanied with
26  * full unit tests in test_qgsstringstatisticalsummary.py.
27  * See details in QEP #17
28  ****************************************************************************/
29 
30 QgsStringStatisticalSummary::QgsStringStatisticalSummary( QgsStringStatisticalSummary::Statistics stats )
31  : mStatistics( stats )
32 {
33  reset();
34 }
35 
37 {
38  mCount = 0;
39  mValues.clear();
40  mCountMissing = 0;
41  mMin.clear();
42  mMax.clear();
43  mMinLength = std::numeric_limits<int>::max();
44  mMaxLength = 0;
45  mSumLengths = 0;
46  mMeanLength = 0;
47 }
48 
49 void QgsStringStatisticalSummary::calculate( const QStringList &values )
50 {
51  reset();
52 
53  Q_FOREACH ( const QString &string, values )
54  {
55  testString( string );
56  }
57  finalize();
58 }
59 
60 void QgsStringStatisticalSummary::addString( const QString &string )
61 {
62  testString( string );
63 }
64 
65 void QgsStringStatisticalSummary::addValue( const QVariant &value )
66 {
67  if ( value.type() == QVariant::String )
68  {
69  testString( value.toString() );
70  }
71  finalize();
72 }
73 
75 {
76  mMeanLength = mSumLengths / static_cast< double >( mCount );
77 }
78 
79 void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList &values )
80 {
81  reset();
82 
83  Q_FOREACH ( const QVariant &variant, values )
84  {
85  if ( variant.type() == QVariant::String )
86  {
87  testString( variant.toString() );
88  }
89  }
90 }
91 
92 void QgsStringStatisticalSummary::testString( const QString &string )
93 {
94  mCount++;
95 
96  if ( string.isEmpty() )
97  mCountMissing++;
98 
99  if ( mStatistics & CountDistinct )
100  {
101  mValues << string;
102  }
103  if ( mStatistics & Min )
104  {
105  if ( !mMin.isEmpty() && !string.isEmpty() )
106  {
107  mMin = std::min( mMin, string );
108  }
109  else if ( mMin.isEmpty() && !string.isEmpty() )
110  {
111  mMin = string;
112  }
113  }
114  if ( mStatistics & Max )
115  {
116  if ( !mMax.isEmpty() && !string.isEmpty() )
117  {
118  mMax = std::max( mMax, string );
119  }
120  else if ( mMax.isEmpty() && !string.isEmpty() )
121  {
122  mMax = string;
123  }
124  }
125  if ( mStatistics & MeanLength )
126  mSumLengths += string.length();
127  mMinLength = std::min( mMinLength, string.length() );
128  mMaxLength = std::max( mMaxLength, string.length() );
129 }
130 
132 {
133  switch ( stat )
134  {
135  case Count:
136  return mCount;
137  case CountDistinct:
138  return mValues.count();
139  case CountMissing:
140  return mCountMissing;
141  case Min:
142  return mMin;
143  case Max:
144  return mMax;
145  case MinimumLength:
146  return mMinLength;
147  case MaximumLength:
148  return mMaxLength;
149  case MeanLength:
150  return mMeanLength;
151  case All:
152  return 0;
153  }
154  return 0;
155 }
156 
158 {
159  switch ( statistic )
160  {
161  case Count:
162  return QObject::tr( "Count" );
163  case CountDistinct:
164  return QObject::tr( "Count (distinct)" );
165  case CountMissing:
166  return QObject::tr( "Count (missing)" );
167  case Min:
168  return QObject::tr( "Minimum" );
169  case Max:
170  return QObject::tr( "Maximum" );
171  case MinimumLength:
172  return QObject::tr( "Minimum length" );
173  case MaximumLength:
174  return QObject::tr( "Maximum length" );
175  case MeanLength:
176  return QObject::tr( "Mean length" );
177  case All:
178  return QString();
179  }
180  return QString();
181 }
182 
Statistic
Enumeration of flags that specify statistics to be calculated.
static QString displayName(QgsStringStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
QgsStringStatisticalSummary(QgsStringStatisticalSummary::Statistics stats=QgsStringStatisticalSummary::All)
Constructor for QgsStringStatistics.
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
void addString(const QString &string)
Adds a single string to the statistics calculation.
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
void reset()
Resets the calculated values.