QGIS API Documentation 3.43.0-Master (6c62b930b02)
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 "qgsvariantutils.h"
18#include <QString>
19#include <QStringList>
20#include <QObject>
21#include <QVariant>
22#include <QVariantList>
23#include <limits>
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
36
38{
39 mCount = 0;
40 mValues.clear();
41 mCountMissing = 0;
42 mMin.clear();
43 mMax.clear();
44 mMinLength = std::numeric_limits<int>::max();
45 mMaxLength = 0;
46 mSumLengths = 0;
47 mMeanLength = 0;
48 mMinority = QString();
49 mMajority = QString();
50}
51
52void QgsStringStatisticalSummary::calculate( const QStringList &values )
53{
54 reset();
55
56 const auto constValues = values;
57 for ( const QString &string : constValues )
58 {
59 testString( string );
60 }
61 finalize();
62}
63
64void QgsStringStatisticalSummary::addString( const QString &string )
65{
66 testString( string );
67}
68
69void QgsStringStatisticalSummary::addValue( const QVariant &value )
70{
71 if ( QgsVariantUtils::isNull( value ) || value.userType() == QMetaType::Type::QString )
72 {
73 testString( value.toString() );
74 }
75}
76
78{
79 mMeanLength = mSumLengths / static_cast< double >( mCount );
80
81 if ( mStatistics & Qgis::StringStatistic::Minority || mStatistics & Qgis::StringStatistic::Majority )
82 {
83 QList<int> valueCounts = mValues.values();
84
85 if ( mStatistics & Qgis::StringStatistic::Minority )
86 {
87 mMinority = mValues.key( *std::min_element( valueCounts.begin(), valueCounts.end() ) );
88 }
89 if ( mStatistics & Qgis::StringStatistic::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 ( QgsVariantUtils::isNull( variant ) || variant.userType() == QMetaType::Type::QString )
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
120 {
121 mValues[string]++;
122 }
123 if ( mStatistics & Qgis::StringStatistic::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 & Qgis::StringStatistic::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 & Qgis::StringStatistic::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 {
156 return mCount;
158 return mValues.count();
160 return mCountMissing;
162 return mMin;
164 return mMax;
166 return mMinLength;
168 return mMaxLength;
170 return mMeanLength;
172 return mMinority;
174 return mMajority;
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 {
197 return QObject::tr( "Count" );
199 return QObject::tr( "Count (distinct)" );
201 return QObject::tr( "Count (missing)" );
203 return QObject::tr( "Minimum" );
205 return QObject::tr( "Maximum" );
207 return QObject::tr( "Minimum length" );
209 return QObject::tr( "Maximum length" );
211 return QObject::tr( "Mean length" );
213 return QObject::tr( "Minority" );
215 return QObject::tr( "Majority" );
217 return QString();
218 }
219 return QString();
220}
221
QFlags< StringStatistic > StringStatistics
Statistics to be calculated for string values.
Definition qgis.h:5776
StringStatistic
Available string statistics.
Definition qgis.h:5756
@ Max
Maximum string value.
@ Min
Minimum string value.
@ MaximumLength
Maximum length of string.
@ MeanLength
Mean length of strings.
@ Minority
Minority of strings.
@ CountMissing
Number of missing (null) values.
@ All
All statistics.
@ Majority
Majority of strings.
@ CountDistinct
Number of distinct string values.
@ MinimumLength
Minimum length of string.
QgsStringStatisticalSummary(Qgis::StringStatistics stats=Qgis::StringStatistic::All)
Constructor for QgsStringStatistics.
static QString displayName(Qgis::StringStatistic 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.
QSet< QString > distinctValues() const
Returns the set of distinct string values.
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
QVariant statistic(Qgis::StringStatistic stat) const
Returns the value of a specified statistic.
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.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.