QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 finalize();
76}
77
79{
80 mMeanLength = mSumLengths / static_cast< double >( mCount );
81
82 if ( mStatistics & Qgis::StringStatistic::Minority || mStatistics & Qgis::StringStatistic::Majority )
83 {
84 QList<int> valueCounts = mValues.values();
85
86 if ( mStatistics & Qgis::StringStatistic::Minority )
87 {
88 mMinority = mValues.key( *std::min_element( valueCounts.begin(), valueCounts.end() ) );
89 }
90 if ( mStatistics & Qgis::StringStatistic::Majority )
91 {
92 mMajority = mValues.key( *std::max_element( valueCounts.begin(), valueCounts.end() ) );
93 }
94 }
95}
96
98{
99 reset();
100
101 const auto constValues = values;
102 for ( const QVariant &variant : constValues )
103 {
104 if ( QgsVariantUtils::isNull( variant ) || variant.userType() == QMetaType::Type::QString )
105 {
106 testString( variant.toString() );
107 }
108 }
109
110 finalize();
111}
112
113void QgsStringStatisticalSummary::testString( const QString &string )
114{
115 mCount++;
116
117 if ( string.isEmpty() )
118 mCountMissing++;
119
121 {
122 mValues[string]++;
123 }
124 if ( mStatistics & Qgis::StringStatistic::Min )
125 {
126 if ( !mMin.isEmpty() && !string.isEmpty() )
127 {
128 mMin = std::min( mMin, string );
129 }
130 else if ( mMin.isEmpty() && !string.isEmpty() )
131 {
132 mMin = string;
133 }
134 }
135 if ( mStatistics & Qgis::StringStatistic::Max )
136 {
137 if ( !mMax.isEmpty() && !string.isEmpty() )
138 {
139 mMax = std::max( mMax, string );
140 }
141 else if ( mMax.isEmpty() && !string.isEmpty() )
142 {
143 mMax = string;
144 }
145 }
146 if ( mStatistics & Qgis::StringStatistic::MeanLength )
147 mSumLengths += string.length();
148 mMinLength = std::min( mMinLength, static_cast<int>( string.length() ) );
149 mMaxLength = std::max( mMaxLength, static_cast<int>( string.length() ) );
150}
151
153{
154 switch ( stat )
155 {
157 return mCount;
159 return mValues.count();
161 return mCountMissing;
163 return mMin;
165 return mMax;
167 return mMinLength;
169 return mMaxLength;
171 return mMeanLength;
173 return mMinority;
175 return mMajority;
177 return 0;
178 }
179 return 0;
180}
181
183{
184 QSet< QString > res;
185 res.reserve( mValues.size() );
186 for ( auto it = mValues.begin(); it != mValues.end(); ++it )
187 {
188 res.insert( it.key() );
189 }
190 return res;
191}
192
194{
195 switch ( statistic )
196 {
198 return QObject::tr( "Count" );
200 return QObject::tr( "Count (distinct)" );
202 return QObject::tr( "Count (missing)" );
204 return QObject::tr( "Minimum" );
206 return QObject::tr( "Maximum" );
208 return QObject::tr( "Minimum length" );
210 return QObject::tr( "Maximum length" );
212 return QObject::tr( "Mean length" );
214 return QObject::tr( "Minority" );
216 return QObject::tr( "Majority" );
218 return QString();
219 }
220 return QString();
221}
222
QFlags< StringStatistic > StringStatistics
Statistics to be calculated for string values.
Definition qgis.h:5528
StringStatistic
Available string statistics.
Definition qgis.h:5508
@ 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.