QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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
16
#include "
qgsstringstatisticalsummary.h
"
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
34
QgsStringStatisticalSummary::QgsStringStatisticalSummary
(
Qgis::StringStatistics
stats )
35
: mStatistics( stats )
36
{
37
reset
();
38
}
39
40
void
QgsStringStatisticalSummary::reset
()
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
55
void
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
67
void
QgsStringStatisticalSummary::addString
(
const
QString &
string
)
68
{
69
testString(
string
);
70
}
71
72
void
QgsStringStatisticalSummary::addValue
(
const
QVariant &value )
73
{
74
if
(
QgsVariantUtils::isNull
( value ) || value.userType() == QMetaType::Type::QString )
75
{
76
testString( value.toString() );
77
}
78
}
79
80
void
QgsStringStatisticalSummary::finalize
()
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
99
void
QgsStringStatisticalSummary::calculateFromVariants
(
const
QVariantList &values )
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
115
void
QgsStringStatisticalSummary::testString(
const
QString &
string
)
116
{
117
mCount++;
118
119
if
(
string
.isEmpty() )
120
mCountMissing++;
121
122
if
( mStatistics &
Qgis::StringStatistic::CountDistinct
|| mStatistics &
Qgis::StringStatistic::Majority
|| mStatistics &
Qgis::StringStatistic::Minority
)
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
154
QVariant
QgsStringStatisticalSummary::statistic
(
Qgis::StringStatistic
stat )
const
155
{
156
switch
( stat )
157
{
158
case
Qgis::StringStatistic::Count
:
159
return
mCount;
160
case
Qgis::StringStatistic::CountDistinct
:
161
return
mValues.count();
162
case
Qgis::StringStatistic::CountMissing
:
163
return
mCountMissing;
164
case
Qgis::StringStatistic::Min
:
165
return
mMin;
166
case
Qgis::StringStatistic::Max
:
167
return
mMax;
168
case
Qgis::StringStatistic::MinimumLength
:
169
return
mMinLength;
170
case
Qgis::StringStatistic::MaximumLength
:
171
return
mMaxLength;
172
case
Qgis::StringStatistic::MeanLength
:
173
return
mMeanLength;
174
case
Qgis::StringStatistic::Minority
:
175
return
mMinority;
176
case
Qgis::StringStatistic::Majority
:
177
return
mMajority;
178
case
Qgis::StringStatistic::All
:
179
return
0;
180
}
181
return
0;
182
}
183
184
QSet<QString>
QgsStringStatisticalSummary::distinctValues
()
const
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
195
QString
QgsStringStatisticalSummary::displayName
(
Qgis::StringStatistic
statistic
)
196
{
197
switch
(
statistic
)
198
{
199
case
Qgis::StringStatistic::Count
:
200
return
QObject::tr(
"Count"
);
201
case
Qgis::StringStatistic::CountDistinct
:
202
return
QObject::tr(
"Count (distinct)"
);
203
case
Qgis::StringStatistic::CountMissing
:
204
return
QObject::tr(
"Count (missing)"
);
205
case
Qgis::StringStatistic::Min
:
206
return
QObject::tr(
"Minimum"
);
207
case
Qgis::StringStatistic::Max
:
208
return
QObject::tr(
"Maximum"
);
209
case
Qgis::StringStatistic::MinimumLength
:
210
return
QObject::tr(
"Minimum length"
);
211
case
Qgis::StringStatistic::MaximumLength
:
212
return
QObject::tr(
"Maximum length"
);
213
case
Qgis::StringStatistic::MeanLength
:
214
return
QObject::tr(
"Mean length"
);
215
case
Qgis::StringStatistic::Minority
:
216
return
QObject::tr(
"Minority"
);
217
case
Qgis::StringStatistic::Majority
:
218
return
QObject::tr(
"Majority"
);
219
case
Qgis::StringStatistic::All
:
220
return
QString();
221
}
222
return
QString();
223
}
Qgis::StringStatistics
QFlags< StringStatistic > StringStatistics
Statistics to be calculated for string values.
Definition
qgis.h:6752
Qgis::StringStatistic
StringStatistic
Available string statistics.
Definition
qgis.h:6732
Qgis::StringStatistic::Max
@ Max
Maximum string value.
Definition
qgis.h:6737
Qgis::StringStatistic::Min
@ Min
Minimum string value.
Definition
qgis.h:6736
Qgis::StringStatistic::MaximumLength
@ MaximumLength
Maximum length of string.
Definition
qgis.h:6739
Qgis::StringStatistic::MeanLength
@ MeanLength
Mean length of strings.
Definition
qgis.h:6740
Qgis::StringStatistic::Minority
@ Minority
Minority of strings.
Definition
qgis.h:6741
Qgis::StringStatistic::CountMissing
@ CountMissing
Number of missing (null) values.
Definition
qgis.h:6735
Qgis::StringStatistic::All
@ All
All statistics.
Definition
qgis.h:6743
Qgis::StringStatistic::Majority
@ Majority
Majority of strings.
Definition
qgis.h:6742
Qgis::StringStatistic::Count
@ Count
Count.
Definition
qgis.h:6733
Qgis::StringStatistic::CountDistinct
@ CountDistinct
Number of distinct string values.
Definition
qgis.h:6734
Qgis::StringStatistic::MinimumLength
@ MinimumLength
Minimum length of string.
Definition
qgis.h:6738
QgsStringStatisticalSummary::QgsStringStatisticalSummary
QgsStringStatisticalSummary(Qgis::StringStatistics stats=Qgis::StringStatistic::All)
Constructor for QgsStringStatistics.
Definition
qgsstringstatisticalsummary.cpp:34
QgsStringStatisticalSummary::displayName
static QString displayName(Qgis::StringStatistic statistic)
Returns the friendly display name for a statistic.
Definition
qgsstringstatisticalsummary.cpp:195
QgsStringStatisticalSummary::finalize
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
Definition
qgsstringstatisticalsummary.cpp:80
QgsStringStatisticalSummary::addString
void addString(const QString &string)
Adds a single string to the statistics calculation.
Definition
qgsstringstatisticalsummary.cpp:67
QgsStringStatisticalSummary::distinctValues
QSet< QString > distinctValues() const
Returns the set of distinct string values.
Definition
qgsstringstatisticalsummary.cpp:184
QgsStringStatisticalSummary::addValue
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
Definition
qgsstringstatisticalsummary.cpp:72
QgsStringStatisticalSummary::statistic
QVariant statistic(Qgis::StringStatistic stat) const
Returns the value of a specified statistic.
Definition
qgsstringstatisticalsummary.cpp:154
QgsStringStatisticalSummary::calculate
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
Definition
qgsstringstatisticalsummary.cpp:55
QgsStringStatisticalSummary::calculateFromVariants
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
Definition
qgsstringstatisticalsummary.cpp:99
QgsStringStatisticalSummary::reset
void reset()
Resets the calculated values.
Definition
qgsstringstatisticalsummary.cpp:40
QgsVariantUtils::isNull
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Definition
qgsvariantutils.cpp:176
qgsstringstatisticalsummary.h
qgsvariantutils.h
Generated on
for QGIS API Documentation by
1.17.0