Skip to content

Commit a927d97

Browse files
committed
Allow QgsStringStatisticalSummary to calculate mean string length
1 parent 57f17e3 commit a927d97

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

python/core/qgsstringstatisticalsummary.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsStringStatisticalSummary
3131
Max, //!< Maximum string value
3232
MinimumLength, //!< Minimum length of string
3333
MaximumLength, //!< Maximum length of string
34+
MeanLength, //!< Mean length of strings
3435
All, //! All statistics
3536
};
3637
typedef QFlags<QgsStringStatisticalSummary::Statistic> Statistics;
@@ -147,6 +148,12 @@ class QgsStringStatisticalSummary
147148
*/
148149
int maxLength() const;
149150

151+
/**
152+
* Returns the mean length of strings.
153+
* @note added in QGIS 3.0
154+
*/
155+
double meanLength() const;
156+
150157
/** Returns the friendly display name for a statistic
151158
* @param statistic statistic to return name for
152159
*/

src/core/qgsstringstatisticalsummary.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ void QgsStringStatisticalSummary::reset()
4242
mMax.clear();
4343
mMinLength = INT_MAX;
4444
mMaxLength = 0;
45+
mSumLengths = 0;
46+
mMeanLength = 0;
4547
}
4648

4749
void QgsStringStatisticalSummary::calculate( const QStringList& values )
@@ -71,8 +73,7 @@ void QgsStringStatisticalSummary::addValue( const QVariant& value )
7173

7274
void QgsStringStatisticalSummary::finalize()
7375
{
74-
//nothing to do for now - this method has been added for forward compatibility
75-
//if statistics are implemented which require a post-calculation step
76+
mMeanLength = mSumLengths / static_cast< double >( mCount );
7677
}
7778

7879
void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList& values )
@@ -121,6 +122,8 @@ void QgsStringStatisticalSummary::testString( const QString& string )
121122
mMax = string;
122123
}
123124
}
125+
if ( mStatistics & MeanLength )
126+
mSumLengths += string.length();
124127
mMinLength = qMin( mMinLength, string.length() );
125128
mMaxLength = qMax( mMaxLength, string.length() );
126129
}
@@ -143,6 +146,8 @@ QVariant QgsStringStatisticalSummary::statistic( QgsStringStatisticalSummary::St
143146
return mMinLength;
144147
case MaximumLength:
145148
return mMaxLength;
149+
case MeanLength:
150+
return mMeanLength;
146151
case All:
147152
return 0;
148153
}
@@ -167,6 +172,8 @@ QString QgsStringStatisticalSummary::displayName( QgsStringStatisticalSummary::S
167172
return QObject::tr( "Minimum length" );
168173
case MaximumLength:
169174
return QObject::tr( "Maximum length" );
175+
case MeanLength:
176+
return QObject::tr( "Mean length" );
170177
case All:
171178
return QString();
172179
}

src/core/qgsstringstatisticalsummary.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class CORE_EXPORT QgsStringStatisticalSummary
5151
Max = 16, //!< Maximum string value
5252
MinimumLength = 32, //!< Minimum length of string
5353
MaximumLength = 64, //!< Maximum length of string
54-
All = Count | CountDistinct | CountMissing | Min | Max, //! All statistics
54+
MeanLength = 128, //!< Mean length of strings
55+
All = Count | CountDistinct | CountMissing | Min | Max | MinimumLength | MaximumLength | MeanLength, //! All statistics
5556
};
5657
Q_DECLARE_FLAGS( Statistics, Statistic )
5758

@@ -167,6 +168,12 @@ class CORE_EXPORT QgsStringStatisticalSummary
167168
*/
168169
int maxLength() const { return mMaxLength; }
169170

171+
/**
172+
* Returns the mean length of strings.
173+
* @note added in QGIS 3.0
174+
*/
175+
double meanLength() const { return mMeanLength; }
176+
170177
/** Returns the friendly display name for a statistic
171178
* @param statistic statistic to return name for
172179
*/
@@ -183,6 +190,8 @@ class CORE_EXPORT QgsStringStatisticalSummary
183190
QString mMax;
184191
int mMinLength;
185192
int mMaxLength;
193+
long mSumLengths;
194+
double mMeanLength;
186195

187196
void testString( const QString& string );
188197
};

tests/src/python/test_qgsstringstatisticalsummary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def testStats(self):
4848
self.assertEqual(s2.minLength(), 0)
4949
self.assertEqual(s.maxLength(), 8)
5050
self.assertEqual(s2.maxLength(), 8)
51+
self.assertEqual(s.meanLength(), 3.33333333333333333333333)
52+
self.assertEqual(s2.meanLength(), 3.33333333333333333333333)
5153

5254
#extra check for minLength without empty strings
5355
s.calculate(['1111111', '111', '11111'])
@@ -63,6 +65,7 @@ def testIndividualStats(self):
6365
{'stat': QgsStringStatisticalSummary.Max, 'expected': 'eeee'},
6466
{'stat': QgsStringStatisticalSummary.MinimumLength, 'expected': 0},
6567
{'stat': QgsStringStatisticalSummary.MaximumLength, 'expected': 8},
68+
{'stat': QgsStringStatisticalSummary.MeanLength, 'expected': 3.3333333333333335},
6669
]
6770

6871
s = QgsStringStatisticalSummary()

0 commit comments

Comments
 (0)