Skip to content
Permalink
Browse files

Add max and min functions

  • Loading branch information
nyalldawson committed May 12, 2013
1 parent a85b0bc commit 2076031424d543db5d6becd4234ed7cd5b150fdd
@@ -0,0 +1,13 @@
<h3>max() function</h3>
Returns the largest value in a set of values.

<h4>Syntax</h4>
max(<i>value<i>[,<i>value</i>...])

<h4>Arguments</h4>
<!-- List args for functions here-->
<i> value</i> &rarr; a number.<br>

<h4>Example</h4>
<!-- Show example of function.-->
max(2,10.2,5.5) &rarr; 10.2
@@ -0,0 +1,13 @@
<h3>min() function</h3>
Returns the smallest value in a set of values.

<h4>Syntax</h4>
min(<i>value<i>[,<i>value</i>...])

<h4>Arguments</h4>
<!-- List args for functions here-->
<i> value</i> &rarr; a number.<br>

<h4>Example</h4>
<!-- Show example of function.-->
min(20.5,10,6.2) &rarr; 6.2
@@ -431,6 +431,43 @@ static QVariant fcnRnd( const QVariantList& values, QgsFeature* , QgsExpression*
// Return a random integer in the range [min, max] (inclusive)
return QVariant( min + ( rand() % ( int )( max - min + 1 ) ) );
}

static QVariant fcnMax( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
//initially set max as first value
double maxVal = getDoubleValue( values.at( 0 ), parent );

//check against all other values
for ( int i = 1; i < values.length(); ++i )
{
double testVal = getDoubleValue( values[i], parent );
if ( testVal > maxVal )
{
maxVal = testVal;
}
}

return QVariant( maxVal );
}

static QVariant fcnMin( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
//initially set min as first value
double minVal = getDoubleValue( values.at( 0 ), parent );

//check against all other values
for ( int i = 1; i < values.length(); ++i )
{
double testVal = getDoubleValue( values[i], parent );
if ( testVal < minVal )
{
minVal = testVal;
}
}

return QVariant( minVal );
}

static QVariant fcnToInt( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
return QVariant( getIntValue( values.at( 0 ), parent ) );
@@ -1253,7 +1290,8 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "sqrt" << "cos" << "sin" << "tan"
<< "asin" << "acos" << "atan" << "atan2"
<< "exp" << "ln" << "log10" << "log"
<< "round" << "rand" << "randf" << "toint" << "toreal" << "tostring"
<< "round" << "rand" << "randf" << "max" << "min"
<< "toint" << "toreal" << "tostring"
<< "todatetime" << "todate" << "totime" << "tointerval"
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
<< "month" << "week" << "day" << "hour"
@@ -1294,6 +1332,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "round", -1, fcnRound, QObject::tr( "Math" ) )
<< new StaticFunction( "rand", 2, fcnRnd, QObject::tr( "Math" ) )
<< new StaticFunction( "randf", 2, fcnRndF, QObject::tr( "Math" ) )
<< new StaticFunction( "max", -1, fcnMax, QObject::tr( "Math" ) )
<< new StaticFunction( "min", -1, fcnMin, QObject::tr( "Math" ) )
<< new StaticFunction( "$pi", 0, fcnPi, QObject::tr( "Math" ) )
<< new StaticFunction( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
<< new StaticFunction( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
@@ -253,6 +253,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "round(1234.554,2) - round down" ) << "round(1234.554,2)" << false << QVariant( 1234.55 );
QTest::newRow( "round(1234.6) - round up to int" ) << "round(1234.6)" << false << QVariant( 1235 );
QTest::newRow( "round(1234.6) - round down to int" ) << "round(1234.4)" << false << QVariant( 1234 );
QTest::newRow( "max(1)" ) << "max(1)" << false << QVariant( 1. );
QTest::newRow( "max(1,3.5,-2.1)" ) << "max(1,3.5,-2.1)" << false << QVariant( 3.5 );
QTest::newRow( "min(-1.5)" ) << "min(-1.5)" << false << QVariant( -1.5 );
QTest::newRow( "min(-16.6,3.5,-2.1)" ) << "min(-16.6,3.5,-2.1)" << false << QVariant( -16.6 );

// cast functions
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );

0 comments on commit 2076031

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