QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  mMinority = QString();
48  mMajority = QString();
49 }
50 
51 void QgsStringStatisticalSummary::calculate( const QStringList &values )
52 {
53  reset();
54 
55  const auto constValues = values;
56  for ( const QString &string : constValues )
57  {
58  testString( string );
59  }
60  finalize();
61 }
62 
63 void QgsStringStatisticalSummary::addString( const QString &string )
64 {
65  testString( string );
66 }
67 
68 void QgsStringStatisticalSummary::addValue( const QVariant &value )
69 {
70  if ( value.type() == QVariant::String )
71  {
72  testString( value.toString() );
73  }
74  finalize();
75 }
76 
78 {
79  mMeanLength = mSumLengths / static_cast< double >( mCount );
80 
81  if ( mStatistics & Minority || mStatistics & Majority )
82  {
83  QList<int> valueCounts = mValues.values();
84 
85  if ( mStatistics & Minority )
86  {
87  mMinority = mValues.key( *std::min_element( valueCounts.begin(), valueCounts.end() ) );
88  }
89  if ( mStatistics & Majority )
90  {
91  mMajority = mValues.key( *std::max_element( valueCounts.begin(), valueCounts.end() ) );
92  }
93  }
94 }
95 
96 void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList &values )
97 {
98  reset();
99 
100  const auto constValues = values;
101  for ( const QVariant &variant : constValues )
102  {
103  if ( variant.type() == QVariant::String )
104  {
105  testString( variant.toString() );
106  }
107  }
108 
109  finalize();
110 }
111 
112 void QgsStringStatisticalSummary::testString( const QString &string )
113 {
114  mCount++;
115 
116  if ( string.isEmpty() )
117  mCountMissing++;
118 
119  if ( mStatistics & CountDistinct || mStatistics & Majority || mStatistics & Minority )
120  {
121  mValues[string]++;
122  }
123  if ( mStatistics & Min )
124  {
125  if ( !mMin.isEmpty() && !string.isEmpty() )
126  {
127  mMin = std::min( mMin, string );
128  }
129  else if ( mMin.isEmpty() && !string.isEmpty() )
130  {
131  mMin = string;
132  }
133  }
134  if ( mStatistics & Max )
135  {
136  if ( !mMax.isEmpty() && !string.isEmpty() )
137  {
138  mMax = std::max( mMax, string );
139  }
140  else if ( mMax.isEmpty() && !string.isEmpty() )
141  {
142  mMax = string;
143  }
144  }
145  if ( mStatistics & MeanLength )
146  mSumLengths += string.length();
147  mMinLength = std::min( mMinLength, string.length() );
148  mMaxLength = std::max( mMaxLength, string.length() );
149 }
150 
152 {
153  switch ( stat )
154  {
155  case Count:
156  return mCount;
157  case CountDistinct:
158  return mValues.count();
159  case CountMissing:
160  return mCountMissing;
161  case Min:
162  return mMin;
163  case Max:
164  return mMax;
165  case MinimumLength:
166  return mMinLength;
167  case MaximumLength:
168  return mMaxLength;
169  case MeanLength:
170  return mMeanLength;
171  case Minority:
172  return mMinority;
173  case Majority:
174  return mMajority;
175  case All:
176  return 0;
177  }
178  return 0;
179 }
180 
182 {
183  switch ( statistic )
184  {
185  case Count:
186  return QObject::tr( "Count" );
187  case CountDistinct:
188  return QObject::tr( "Count (distinct)" );
189  case CountMissing:
190  return QObject::tr( "Count (missing)" );
191  case Min:
192  return QObject::tr( "Minimum" );
193  case Max:
194  return QObject::tr( "Maximum" );
195  case MinimumLength:
196  return QObject::tr( "Minimum length" );
197  case MaximumLength:
198  return QObject::tr( "Maximum length" );
199  case MeanLength:
200  return QObject::tr( "Mean length" );
201  case Minority:
202  return QObject::tr( "Minority" );
203  case Majority:
204  return QObject::tr( "Majority" );
205  case All:
206  return QString();
207  }
208  return QString();
209 }
210 
QgsStringStatisticalSummary::Minority
@ Minority
Minority of strings.
Definition: qgsstringstatisticalsummary.h:59
QgsStringStatisticalSummary::finalize
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
Definition: qgsstringstatisticalsummary.cpp:77
QgsStringStatisticalSummary::statistic
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
Definition: qgsstringstatisticalsummary.cpp:151
QgsStringStatisticalSummary::calculateFromVariants
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
Definition: qgsstringstatisticalsummary.cpp:96
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:50
QgsStringStatisticalSummary::CountDistinct
@ CountDistinct
Number of distinct string values.
Definition: qgsstringstatisticalsummary.h:52
QgsStringStatisticalSummary::Count
@ Count
Count.
Definition: qgsstringstatisticalsummary.h:51
QgsStringStatisticalSummary::MeanLength
@ MeanLength
Mean length of strings.
Definition: qgsstringstatisticalsummary.h:58
QgsStringStatisticalSummary::displayName
static QString displayName(QgsStringStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
Definition: qgsstringstatisticalsummary.cpp:181
QgsStringStatisticalSummary::addValue
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
Definition: qgsstringstatisticalsummary.cpp:68
QgsStringStatisticalSummary::reset
void reset()
Resets the calculated values.
Definition: qgsstringstatisticalsummary.cpp:36
QgsStringStatisticalSummary::MaximumLength
@ MaximumLength
Maximum length of string.
Definition: qgsstringstatisticalsummary.h:57
QgsStringStatisticalSummary::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsstringstatisticalsummary.h:53
qgsstringstatisticalsummary.h
QgsStringStatisticalSummary::All
@ All
All statistics.
Definition: qgsstringstatisticalsummary.h:61
QgsStringStatisticalSummary::calculate
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
Definition: qgsstringstatisticalsummary.cpp:51
QgsStringStatisticalSummary::QgsStringStatisticalSummary
QgsStringStatisticalSummary(QgsStringStatisticalSummary::Statistics stats=QgsStringStatisticalSummary::All)
Constructor for QgsStringStatistics.
Definition: qgsstringstatisticalsummary.cpp:30
QgsStringStatisticalSummary::MinimumLength
@ MinimumLength
Minimum length of string.
Definition: qgsstringstatisticalsummary.h:56
QgsStringStatisticalSummary::Majority
@ Majority
Majority of strings.
Definition: qgsstringstatisticalsummary.h:60
QgsStringStatisticalSummary::addString
void addString(const QString &string)
Adds a single string to the statistics calculation.
Definition: qgsstringstatisticalsummary.cpp:63
QgsStringStatisticalSummary::Min
@ Min
Minimum string value.
Definition: qgsstringstatisticalsummary.h:54