Add option to get null count from QgsVectorLayer::getDoubleValues

This commit is contained in:
Nyall Dawson 2015-05-23 02:52:32 +10:00
parent 0aedbcaf7f
commit 1078daf712
4 changed files with 20 additions and 6 deletions

View File

@ -1148,11 +1148,12 @@ class QgsVectorLayer : QgsMapLayer
* @param fieldOrExpression field name or an expression string evaluating to a double value
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @param nullCount optional pointer to integer to store number of null values encountered in
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getValues
*/
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int* nullCount = 0 );
/** Set the blending mode used for rendering each feature */
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );

View File

@ -3205,10 +3205,13 @@ QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, boo
return values;
}
QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly )
QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly, int* nullCount )
{
QList<double> values;
if ( nullCount )
*nullCount = 0;
QList<QVariant> variantValues = getValues( fieldOrExpression, ok, selectedOnly );
if ( !ok )
return values;
@ -3219,6 +3222,11 @@ QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression,
double val = value.toDouble( &convertOk );
if ( convertOk )
values << val;
else if ( value.isNull() )
{
if ( nullCount )
*nullCount += 1;
}
}
ok = true;
return values;

View File

@ -1514,11 +1514,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
* @param fieldOrExpression field name or an expression string evaluating to a double value
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @param nullCount optional pointer to integer to store number of null values encountered in
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getValues
*/
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int* nullCount = 0 );
/** Set the blending mode used for rendering each feature */
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );

View File

@ -229,19 +229,22 @@ class TestQgsVectorLayer : public QObject
QCOMPARE( varList.at( 0 ), QVariant( 2 ) );
QCOMPARE( varList.at( 1 ), QVariant( 3 ) );
QList<double> doubleList = layer->getDoubleValues( "col1", ok );
int nulls = 0;
QList<double> doubleList = layer->getDoubleValues( "col1", ok, false, &nulls );
QVERIFY( ok );
QCOMPARE( doubleList.length(), 3 );
QCOMPARE( doubleList.at( 0 ), 1.0 );
QCOMPARE( doubleList.at( 1 ), 2.0 );
QCOMPARE( doubleList.at( 2 ), 3.0 );
QCOMPARE( nulls, 1 );
//check with selected features
doubleList = layer->getDoubleValues( "col1", ok, true );
doubleList = layer->getDoubleValues( "col1", ok, true, &nulls );
QVERIFY( ok );
QCOMPARE( doubleList.length(), 2 );
QCOMPARE( doubleList.at( 0 ), 2.0 );
QCOMPARE( doubleList.at( 1 ), 3.0 );
QCOMPARE( nulls, 0 );
QList<QVariant> expVarList = layer->getValues( "tostring(col1) || ' '", ok );
QVERIFY( ok );
@ -251,12 +254,13 @@ class TestQgsVectorLayer : public QObject
QCOMPARE( expVarList.at( 2 ).toString(), QString( "3 " ) );
QCOMPARE( expVarList.at( 3 ), QVariant() );
QList<double> expDoubleList = layer->getDoubleValues( "col1 * 2", ok );
QList<double> expDoubleList = layer->getDoubleValues( "col1 * 2", ok, false, &nulls );
QVERIFY( ok );
QCOMPARE( expDoubleList.length(), 3 );
QCOMPARE( expDoubleList.at( 0 ), 2.0 );
QCOMPARE( expDoubleList.at( 1 ), 4.0 );
QCOMPARE( expDoubleList.at( 2 ), 6.0 );
QCOMPARE( nulls, 1 );
delete layer;
}