QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include <limits>
19
20#include "qgsvariantutils.h"
21
22#include <QObject>
23#include <QString>
24#include <QStringList>
25#include <QVariant>
26#include <QVariantList>
27
28/***************************************************************************
29 * This class is considered CRITICAL and any change MUST be accompanied with
30 * full unit tests in test_qgsstringstatisticalsummary.py.
31 * See details in QEP #17
32 ****************************************************************************/
33
39
41{
42 mCount = 0;
43 mValues.clear();
44 mCountMissing = 0;
45 mMin.clear();
46 mMax.clear();
47 mMinLength = std::numeric_limits<int>::max();
48 mMaxLength = 0;
49 mSumLengths = 0;
50 mMeanLength = 0;
51 mMinority = QString();
52 mMajority = QString();
53}
54
55void QgsStringStatisticalSummary::calculate( const QStringList &values )
56{
57 reset();
58
59 const auto constValues = values;
60 for ( const QString &string : constValues )
61 {
62 testString( string );
63 }
64 finalize();
65}
66
67void QgsStringStatisticalSummary::addString( const QString &string )
68{
69 testString( string );
70}
71
72void QgsStringStatisticalSummary::addValue( const QVariant &value )
73{
74 if ( QgsVariantUtils::isNull( value ) || value.userType() == QMetaType::Type::QString )
75 {
76 testString( value.toString() );
77 }
78}
79
81{
82 mMeanLength = mSumLengths / static_cast< double >( mCount );
83
84 if ( mStatistics & Qgis::StringStatistic::Minority || mStatistics & Qgis::StringStatistic::Majority )
85 {
86 QList<int> valueCounts = mValues.values();
87
88 if ( mStatistics & Qgis::StringStatistic::Minority )
89 {
90 mMinority = mValues.key( *std::min_element( valueCounts.begin(), valueCounts.end() ) );
91 }
92 if ( mStatistics & Qgis::StringStatistic::Majority )
93 {
94 mMajority = mValues.key( *std::max_element( valueCounts.begin(), valueCounts.end() ) );
95 }
96 }
97}
98
100{
101 reset();
102
103 const auto constValues = values;
104 for ( const QVariant &variant : constValues )
105 {
106 if ( QgsVariantUtils::isNull( variant ) || variant.userType() == QMetaType::Type::QString )
107 {
108 testString( variant.toString() );
109 }
110 }
111
112 finalize();
113}
114
115void QgsStringStatisticalSummary::testString( const QString &string )
116{
117 mCount++;
118
119 if ( string.isEmpty() )
120 mCountMissing++;
121
123 {
124 mValues[string]++;
125 }
126 if ( mStatistics & Qgis::StringStatistic::Min )
127 {
128 if ( !mMin.isEmpty() && !string.isEmpty() )
129 {
130 mMin = std::min( mMin, string );
131 }
132 else if ( mMin.isEmpty() && !string.isEmpty() )
133 {
134 mMin = string;
135 }
136 }
137 if ( mStatistics & Qgis::StringStatistic::Max )
138 {
139 if ( !mMax.isEmpty() && !string.isEmpty() )
140 {
141 mMax = std::max( mMax, string );
142 }
143 else if ( mMax.isEmpty() && !string.isEmpty() )
144 {
145 mMax = string;
146 }
147 }
148 if ( mStatistics & Qgis::StringStatistic::MeanLength )
149 mSumLengths += string.length();
150 mMinLength = std::min( mMinLength, static_cast<int>( string.length() ) );
151 mMaxLength = std::max( mMaxLength, static_cast<int>( string.length() ) );
152}
153
155{
156 switch ( stat )
157 {
159 return mCount;
161 return mValues.count();
163 return mCountMissing;
165 return mMin;
167 return mMax;
169 return mMinLength;
171 return mMaxLength;
173 return mMeanLength;
175 return mMinority;
177 return mMajority;
179 return 0;
180 }
181 return 0;
182}
183
185{
186 QSet< QString > res;
187 res.reserve( mValues.size() );
188 for ( auto it = mValues.begin(); it != mValues.end(); ++it )
189 {
190 res.insert( it.key() );
191 }
192 return res;
193}
194
196{
197 switch ( statistic )
198 {
200 return QObject::tr( "Count" );
202 return QObject::tr( "Count (distinct)" );
204 return QObject::tr( "Count (missing)" );
206 return QObject::tr( "Minimum" );
208 return QObject::tr( "Maximum" );
210 return QObject::tr( "Minimum length" );
212 return QObject::tr( "Maximum length" );
214 return QObject::tr( "Mean length" );
216 return QObject::tr( "Minority" );
218 return QObject::tr( "Majority" );
220 return QString();
221 }
222 return QString();
223}
224
QFlags< StringStatistic > StringStatistics
Statistics to be calculated for string values.
Definition qgis.h:5930
StringStatistic
Available string statistics.
Definition qgis.h:5910
@ Max
Maximum string value.
Definition qgis.h:5915
@ Min
Minimum string value.
Definition qgis.h:5914
@ MaximumLength
Maximum length of string.
Definition qgis.h:5917
@ MeanLength
Mean length of strings.
Definition qgis.h:5918
@ Minority
Minority of strings.
Definition qgis.h:5919
@ CountMissing
Number of missing (null) values.
Definition qgis.h:5913
@ All
All statistics.
Definition qgis.h:5921
@ Majority
Majority of strings.
Definition qgis.h:5920
@ CountDistinct
Number of distinct string values.
Definition qgis.h:5912
@ MinimumLength
Minimum length of string.
Definition qgis.h:5916
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.