Skip to content

Commit

Permalink
Add method to QgsStatisticalSummary to get a short name corresponding…
Browse files Browse the repository at this point in the history
… to a stat

Returns a short name, suitable for use in a field name
  • Loading branch information
nyalldawson committed Jan 3, 2019
1 parent 5f817b4 commit e99072b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
13 changes: 11 additions & 2 deletions python/core/auto_generated/qgsstatisticalsummary.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

};
Expand Down
46 changes: 46 additions & 0 deletions src/core/qgsstatisticalsummary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

11 changes: 9 additions & 2 deletions src/core/qgsstatisticalsummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/src/core/testqgsstatisticalsummary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TestQgsStatisticSummary: public QObject
void maxMin();
void countMissing();
void noValues();
void shortName();

private:

Expand Down Expand Up @@ -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.