QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
30QgsStringStatisticalSummary::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
51void 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
63void QgsStringStatisticalSummary::addString( const QString &string )
64{
65 testString( string );
66}
67
68void 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
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
112void 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, static_cast<int>( string.length() ) );
148 mMaxLength = std::max( mMaxLength, static_cast<int>( 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 QSet< QString > res;
184 res.reserve( mValues.size() );
185 for ( auto it = mValues.begin(); it != mValues.end(); ++it )
186 {
187 res.insert( it.key() );
188 }
189 return res;
190}
191
193{
194 switch ( statistic )
195 {
196 case Count:
197 return QObject::tr( "Count" );
198 case CountDistinct:
199 return QObject::tr( "Count (distinct)" );
200 case CountMissing:
201 return QObject::tr( "Count (missing)" );
202 case Min:
203 return QObject::tr( "Minimum" );
204 case Max:
205 return QObject::tr( "Maximum" );
206 case MinimumLength:
207 return QObject::tr( "Minimum length" );
208 case MaximumLength:
209 return QObject::tr( "Maximum length" );
210 case MeanLength:
211 return QObject::tr( "Mean length" );
212 case Minority:
213 return QObject::tr( "Minority" );
214 case Majority:
215 return QObject::tr( "Majority" );
216 case All:
217 return QString();
218 }
219 return QString();
220}
221
static QString displayName(QgsStringStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
void addString(const QString &string)
Adds a single string to the statistics calculation.
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
QSet< QString > distinctValues() const
Returns the set of distinct string values.
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
QgsStringStatisticalSummary(QgsStringStatisticalSummary::Statistics stats=QgsStringStatisticalSummary::All)
Constructor for QgsStringStatistics.
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.
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
void reset()
Resets the calculated values.