Skip to content

Commit e99072b

Browse files
committed
Add method to QgsStatisticalSummary to get a short name corresponding to a stat
Returns a short name, suitable for use in a field name
1 parent 5f817b4 commit e99072b

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

python/core/auto_generated/qgsstatisticalsummary.sip.in

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,18 @@ be calculated.
316316

317317
static QString displayName( QgsStatisticalSummary::Statistic statistic );
318318
%Docstring
319-
Returns the friendly display name for a statistic
319+
Returns the friendly display name for a ``statistic``.
320320

321-
:param statistic: statistic to return name for
321+
.. seealso:: :py:func:`shortName`
322+
%End
323+
324+
static QString shortName( QgsStatisticalSummary::Statistic statistic );
325+
%Docstring
326+
Returns a short, friendly display name for a ``statistic``, suitable for use in a field name.
327+
328+
.. seealso:: :py:func:`displayName`
329+
330+
.. versionadded:: 3.6
322331
%End
323332

324333
};

src/core/qgsstatisticalsummary.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,49 @@ QString QgsStatisticalSummary::displayName( QgsStatisticalSummary::Statistic sta
329329
return QString();
330330
}
331331

332+
QString QgsStatisticalSummary::shortName( QgsStatisticalSummary::Statistic statistic )
333+
{
334+
switch ( statistic )
335+
{
336+
case Count:
337+
return QStringLiteral( "count" );
338+
case CountMissing:
339+
return QStringLiteral( "countmissing" );
340+
case Sum:
341+
return QStringLiteral( "sum" );
342+
case Mean:
343+
return QStringLiteral( "mean" );
344+
case Median:
345+
return QStringLiteral( "median" );
346+
case StDev:
347+
return QStringLiteral( "stdev" );
348+
case StDevSample:
349+
return QStringLiteral( "stdevsample" );
350+
case Min:
351+
return QStringLiteral( "min" );
352+
case Max:
353+
return QStringLiteral( "max" );
354+
case Range:
355+
return QStringLiteral( "range" );
356+
case Minority:
357+
return QStringLiteral( "minority" );
358+
case Majority:
359+
return QStringLiteral( "majority" );
360+
case Variety:
361+
return QStringLiteral( "variety" );
362+
case FirstQuartile:
363+
return QStringLiteral( "q1" );
364+
case ThirdQuartile:
365+
return QStringLiteral( "q3" );
366+
case InterQuartileRange:
367+
return QStringLiteral( "iqr" );
368+
case First:
369+
return QStringLiteral( "first" );
370+
case Last:
371+
return QStringLiteral( "last" );
372+
case All:
373+
return QString();
374+
}
375+
return QString();
376+
}
377+

src/core/qgsstatisticalsummary.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,18 @@ class CORE_EXPORT QgsStatisticalSummary
286286
double interQuartileRange() const { return std::isnan( mThirdQuartile ) || std::isnan( mFirstQuartile ) ? std::numeric_limits<double>::quiet_NaN() : mThirdQuartile - mFirstQuartile; }
287287

288288
/**
289-
* Returns the friendly display name for a statistic
290-
* \param statistic statistic to return name for
289+
* Returns the friendly display name for a \a statistic.
290+
* \see shortName()
291291
*/
292292
static QString displayName( QgsStatisticalSummary::Statistic statistic );
293293

294+
/**
295+
* Returns a short, friendly display name for a \a statistic, suitable for use in a field name.
296+
* \see displayName()
297+
* \since QGIS 3.6
298+
*/
299+
static QString shortName( QgsStatisticalSummary::Statistic statistic );
300+
294301
private:
295302

296303
Statistics mStatistics;

tests/src/core/testqgsstatisticalsummary.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsStatisticSummary: public QObject
3636
void maxMin();
3737
void countMissing();
3838
void noValues();
39+
void shortName();
3940

4041
private:
4142

@@ -349,5 +350,27 @@ void TestQgsStatisticSummary::noValues()
349350
QVERIFY( std::isnan( s.statistic( QgsStatisticalSummary::InterQuartileRange ) ) );
350351
}
351352

353+
void TestQgsStatisticSummary::shortName()
354+
{
355+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Count ), QStringLiteral( "count" ) );
356+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::CountMissing ), QStringLiteral( "countmissing" ) );
357+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Sum ), QStringLiteral( "sum" ) );
358+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Mean ), QStringLiteral( "mean" ) );
359+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Median ), QStringLiteral( "median" ) );
360+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::StDev ), QStringLiteral( "stdev" ) );
361+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::StDevSample ), QStringLiteral( "stdevsample" ) );
362+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Min ), QStringLiteral( "min" ) );
363+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Max ), QStringLiteral( "max" ) );
364+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Range ), QStringLiteral( "range" ) );
365+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Minority ), QStringLiteral( "minority" ) );
366+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Majority ), QStringLiteral( "majority" ) );
367+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Variety ), QStringLiteral( "variety" ) );
368+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::FirstQuartile ), QStringLiteral( "q1" ) );
369+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::ThirdQuartile ), QStringLiteral( "q3" ) );
370+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::InterQuartileRange ), QStringLiteral( "iqr" ) );
371+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::First ), QStringLiteral( "first" ) );
372+
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Last ), QStringLiteral( "last" ) );
373+
}
374+
352375
QGSTEST_MAIN( TestQgsStatisticSummary )
353376
#include "testqgsstatisticalsummary.moc"

0 commit comments

Comments
 (0)