Skip to content
Permalink
Browse files

Add method to QgsStatisticalSummary to get a short name corresponding…

… to a stat

Returns a short name, suitable for use in a field name
  • Loading branch information
nyalldawson committed Jan 1, 2019
1 parent 5f817b4 commit e99072b1611703056aa2ad8a4caf4f8384e071cd
@@ -316,9 +316,18 @@ be calculated.

static QString displayName( QgsStatisticalSummary::Statistic statistic );
%Docstring
Returns the friendly display name for a statistic
Returns the friendly display name for a ``statistic``.

:param statistic: statistic to return name for
.. seealso:: :py:func:`shortName`
%End

static QString shortName( QgsStatisticalSummary::Statistic statistic );
%Docstring
Returns a short, friendly display name for a ``statistic``, suitable for use in a field name.

.. seealso:: :py:func:`displayName`

.. versionadded:: 3.6
%End

};
@@ -329,3 +329,49 @@ QString QgsStatisticalSummary::displayName( QgsStatisticalSummary::Statistic sta
return QString();
}

QString QgsStatisticalSummary::shortName( QgsStatisticalSummary::Statistic statistic )
{
switch ( statistic )
{
case Count:
return QStringLiteral( "count" );
case CountMissing:
return QStringLiteral( "countmissing" );
case Sum:
return QStringLiteral( "sum" );
case Mean:
return QStringLiteral( "mean" );
case Median:
return QStringLiteral( "median" );
case StDev:
return QStringLiteral( "stdev" );
case StDevSample:
return QStringLiteral( "stdevsample" );
case Min:
return QStringLiteral( "min" );
case Max:
return QStringLiteral( "max" );
case Range:
return QStringLiteral( "range" );
case Minority:
return QStringLiteral( "minority" );
case Majority:
return QStringLiteral( "majority" );
case Variety:
return QStringLiteral( "variety" );
case FirstQuartile:
return QStringLiteral( "q1" );
case ThirdQuartile:
return QStringLiteral( "q3" );
case InterQuartileRange:
return QStringLiteral( "iqr" );
case First:
return QStringLiteral( "first" );
case Last:
return QStringLiteral( "last" );
case All:
return QString();
}
return QString();
}

@@ -286,11 +286,18 @@ class CORE_EXPORT QgsStatisticalSummary
double interQuartileRange() const { return std::isnan( mThirdQuartile ) || std::isnan( mFirstQuartile ) ? std::numeric_limits<double>::quiet_NaN() : mThirdQuartile - mFirstQuartile; }

/**
* Returns the friendly display name for a statistic
* \param statistic statistic to return name for
* Returns the friendly display name for a \a statistic.
* \see shortName()
*/
static QString displayName( QgsStatisticalSummary::Statistic statistic );

/**
* Returns a short, friendly display name for a \a statistic, suitable for use in a field name.
* \see displayName()
* \since QGIS 3.6
*/
static QString shortName( QgsStatisticalSummary::Statistic statistic );

private:

Statistics mStatistics;
@@ -36,6 +36,7 @@ class TestQgsStatisticSummary: public QObject
void maxMin();
void countMissing();
void noValues();
void shortName();

private:

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

void TestQgsStatisticSummary::shortName()
{
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Count ), QStringLiteral( "count" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::CountMissing ), QStringLiteral( "countmissing" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Sum ), QStringLiteral( "sum" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Mean ), QStringLiteral( "mean" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Median ), QStringLiteral( "median" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::StDev ), QStringLiteral( "stdev" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::StDevSample ), QStringLiteral( "stdevsample" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Min ), QStringLiteral( "min" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Max ), QStringLiteral( "max" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Range ), QStringLiteral( "range" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Minority ), QStringLiteral( "minority" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Majority ), QStringLiteral( "majority" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Variety ), QStringLiteral( "variety" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::FirstQuartile ), QStringLiteral( "q1" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::ThirdQuartile ), QStringLiteral( "q3" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::InterQuartileRange ), QStringLiteral( "iqr" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::First ), QStringLiteral( "first" ) );
QCOMPARE( QgsStatisticalSummary::shortName( QgsStatisticalSummary::Last ), QStringLiteral( "last" ) );
}

QGSTEST_MAIN( TestQgsStatisticSummary )
#include "testqgsstatisticalsummary.moc"

0 comments on commit e99072b

Please sign in to comment.
You can’t perform that action at this time.