Skip to content

Commit

Permalink
Add option to get null count from QgsVectorLayer::getDoubleValues
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 22, 2015
1 parent 0aedbca commit 1078daf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -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 );
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -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 );
Expand Down
10 changes: 7 additions & 3 deletions tests/src/core/testqgsvectorlayer.cpp
Expand Up @@ -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 );
Expand All @@ -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;
}
Expand Down

0 comments on commit 1078daf

Please sign in to comment.