mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
Add max and min functions
This commit is contained in:
parent
a85b0bc79e
commit
2076031424
13
resources/function_help/max-en_US
Normal file
13
resources/function_help/max-en_US
Normal file
@ -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> → a number.<br>
|
||||||
|
|
||||||
|
<h4>Example</h4>
|
||||||
|
<!-- Show example of function.-->
|
||||||
|
max(2,10.2,5.5) → 10.2
|
13
resources/function_help/min-en_US
Normal file
13
resources/function_help/min-en_US
Normal file
@ -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> → a number.<br>
|
||||||
|
|
||||||
|
<h4>Example</h4>
|
||||||
|
<!-- Show example of function.-->
|
||||||
|
min(20.5,10,6.2) → 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 a random integer in the range [min, max] (inclusive)
|
||||||
return QVariant( min + ( rand() % ( int )( max - min + 1 ) ) );
|
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 )
|
static QVariant fcnToInt( const QVariantList& values, QgsFeature* , QgsExpression* parent )
|
||||||
{
|
{
|
||||||
return QVariant( getIntValue( values.at( 0 ), parent ) );
|
return QVariant( getIntValue( values.at( 0 ), parent ) );
|
||||||
@ -1253,7 +1290,8 @@ const QStringList &QgsExpression::BuiltinFunctions()
|
|||||||
<< "sqrt" << "cos" << "sin" << "tan"
|
<< "sqrt" << "cos" << "sin" << "tan"
|
||||||
<< "asin" << "acos" << "atan" << "atan2"
|
<< "asin" << "acos" << "atan" << "atan2"
|
||||||
<< "exp" << "ln" << "log10" << "log"
|
<< "exp" << "ln" << "log10" << "log"
|
||||||
<< "round" << "rand" << "randf" << "toint" << "toreal" << "tostring"
|
<< "round" << "rand" << "randf" << "max" << "min"
|
||||||
|
<< "toint" << "toreal" << "tostring"
|
||||||
<< "todatetime" << "todate" << "totime" << "tointerval"
|
<< "todatetime" << "todate" << "totime" << "tointerval"
|
||||||
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
|
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
|
||||||
<< "month" << "week" << "day" << "hour"
|
<< "month" << "week" << "day" << "hour"
|
||||||
@ -1294,6 +1332,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
|
|||||||
<< new StaticFunction( "round", -1, fcnRound, QObject::tr( "Math" ) )
|
<< new StaticFunction( "round", -1, fcnRound, QObject::tr( "Math" ) )
|
||||||
<< new StaticFunction( "rand", 2, fcnRnd, QObject::tr( "Math" ) )
|
<< new StaticFunction( "rand", 2, fcnRnd, QObject::tr( "Math" ) )
|
||||||
<< new StaticFunction( "randf", 2, fcnRndF, 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( "$pi", 0, fcnPi, QObject::tr( "Math" ) )
|
||||||
<< new StaticFunction( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
|
<< new StaticFunction( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
|
||||||
<< new StaticFunction( "toreal", 1, fcnToReal, 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.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 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( "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
|
// cast functions
|
||||||
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );
|
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user