Skip to content
Permalink
Browse files

fix NULL field in size expression beeing drawn

the expression generated by the assistant was causing symbol with size
expr evaluated to NULL to be drawn with default size, wich is not what
the default should be.

The generated size expression is now composed with coalesce(...,0)
so the symbol is not drawn when the size expression is NULL

The expression is still recognized without the coalesce to allow the
legend to be drawn even in the absence of coalesce: this can be used
to have the default size for NULL expression and use a conditional expr
fro color to highlight symbols where value is NULL.
  • Loading branch information
vmora authored and nyalldawson committed Jun 24, 2015
1 parent 4fb22b4 commit 61fbe1c602bf1442ba9f2ab25ffc7b74c8ddae51
Showing with 38 additions and 8 deletions.
  1. +15 −5 src/core/qgsscaleexpression.cpp
  2. +23 −3 tests/src/core/testqgsscaleexpression.cpp
@@ -44,6 +44,8 @@ QgsScaleExpression::QgsScaleExpression( Type type, const QString& baseExpression
void QgsScaleExpression::init()
{
bool ok;
mType = Unknown;

if ( !rootNode() )
return;

@@ -53,6 +55,16 @@ void QgsScaleExpression::init()

QList<Node*> args = f->args()->list();

// the scale function may be enclosed in a coalesce(expr, 0) to avoid NULL value
// to be drawn with the default size
if ( "coalesce" == Functions()[f->fnIndex()]->name() )
{
f = dynamic_cast<const NodeFunction*>( args[0] );
if ( !f )
return;
args = f->args()->list();
}

if ( "scale_linear" == Functions()[f->fnIndex()]->name() )
{
mType = Linear;
@@ -68,13 +80,11 @@ void QgsScaleExpression::init()
mType = Area;
else
{
mType = Unknown;
return;
}
}
else
{
mType = Unknown;
return;
}

@@ -106,13 +116,13 @@ QString QgsScaleExpression::createExpression( Type type, const QString & baseExp
switch ( type )
{
case Linear:
return QString( "scale_linear(%1,%2,%3,%4,%5)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_linear(%1, %2, %3, %4, %5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Area:
return QString( "scale_exp(%1,%2,%3,%4,%5, 0.5)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Flannery:
return QString( "scale_exp(%1,%2,%3,%4,%5, 0.57)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.57), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Unknown:
break;
@@ -29,6 +29,26 @@ class TestQgsScaleExpression: public QObject

void parsing()
{
{
QgsScaleExpression exp( "coalesce(scale_linear(column, 1, 7, 2, 10), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Linear );
QCOMPARE( exp.baseExpression(), QString( "column" ) );
QCOMPARE( exp.minValue(), 1. );
QCOMPARE( exp.maxValue(), 7. );
QCOMPARE( exp.minSize(), 2. );
QCOMPARE( exp.maxSize(), 10. );
}
{
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.5), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Area );
}
{
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.57), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
}
{
QgsScaleExpression exp( "scale_linear(column, 1, 7, 2, 10)" );
QCOMPARE( bool( exp ), true );
@@ -50,17 +70,17 @@ class TestQgsScaleExpression: public QObject
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.51)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.51), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7, a, 10, 0.5)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, a, 10, 0.5), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}

0 comments on commit 61fbe1c

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