Skip to content
Permalink
Browse files

Added array_mode expression function

  • Loading branch information
uclaros authored and nyalldawson committed Dec 28, 2020
1 parent d24f9a1 commit f2ed1c493fcc5dcf4439dbfe8907e7f9902642ed
Showing with 25 additions and 0 deletions.
  1. +11 −0 resources/function_help/json/array_mode
  2. +14 −0 src/core/expression/qgsexpressionfunction.cpp
@@ -0,0 +1,11 @@
{
"name": "array_mode",
"type": "function",
"groups": ["Arrays"],
"description": "Returns an array containing the mode value (most common value) of an array. The returned array may contain more than one value if more values occur equally often.",
"arguments": [ {"arg":"array","description":"an array"} ],
"examples": [
{ "expression":"array_mode(array(0,1,42,42,43))", "returns":"[ 42 ]"},
{ "expression":"array_mode(array(0,0,1,2,2,42))", "returns":"[ 0, 2 ]"}
]
}
@@ -5354,6 +5354,19 @@ static QVariant fcnArrayMaximum( const QVariantList &values, const QgsExpression
return list.isEmpty() ? QVariant() : *std::max_element( list.constBegin(), list.constEnd(), []( QVariant a, QVariant b ) -> bool { return ( qgsVariantLessThan( a, b ) ); } );
}

static QVariant fcnArrayMode( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
QHash< QVariant, int > hash;
for ( const auto &item : list )
{
++hash[item];
}
const QList< int > occurrences = hash.values();
const int maxValue = *std::max_element( occurrences.constBegin(), occurrences.constEnd() );
return list.isEmpty() ? QVariant() : hash.keys( maxValue );
}

static QVariant convertToSameType( const QVariant &value, QVariant::Type type )
{
QVariant result = value;
@@ -6961,6 +6974,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "array_last" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayLast, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_min" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMinimum, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_max" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMaximum, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_mode" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMode, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_append" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayAppend, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_prepend" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayPrepend, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_insert" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "pos" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayInsert, QStringLiteral( "Arrays" ) )

0 comments on commit f2ed1c4

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