Skip to content

Commit 61fbe1c

Browse files
vmoranyalldawson
authored andcommitted
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.
1 parent 4fb22b4 commit 61fbe1c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/core/qgsscaleexpression.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ QgsScaleExpression::QgsScaleExpression( Type type, const QString& baseExpression
4444
void QgsScaleExpression::init()
4545
{
4646
bool ok;
47+
mType = Unknown;
48+
4749
if ( !rootNode() )
4850
return;
4951

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

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

58+
// the scale function may be enclosed in a coalesce(expr, 0) to avoid NULL value
59+
// to be drawn with the default size
60+
if ( "coalesce" == Functions()[f->fnIndex()]->name() )
61+
{
62+
f = dynamic_cast<const NodeFunction*>( args[0] );
63+
if ( !f )
64+
return;
65+
args = f->args()->list();
66+
}
67+
5668
if ( "scale_linear" == Functions()[f->fnIndex()]->name() )
5769
{
5870
mType = Linear;
@@ -68,13 +80,11 @@ void QgsScaleExpression::init()
6880
mType = Area;
6981
else
7082
{
71-
mType = Unknown;
7283
return;
7384
}
7485
}
7586
else
7687
{
77-
mType = Unknown;
7888
return;
7989
}
8090

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

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

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

117127
case Unknown:
118128
break;

tests/src/core/testqgsscaleexpression.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ class TestQgsScaleExpression: public QObject
2929

3030
void parsing()
3131
{
32+
{
33+
QgsScaleExpression exp( "coalesce(scale_linear(column, 1, 7, 2, 10), 0)" );
34+
QCOMPARE( bool( exp ), true );
35+
QCOMPARE( exp.type(), QgsScaleExpression::Linear );
36+
QCOMPARE( exp.baseExpression(), QString( "column" ) );
37+
QCOMPARE( exp.minValue(), 1. );
38+
QCOMPARE( exp.maxValue(), 7. );
39+
QCOMPARE( exp.minSize(), 2. );
40+
QCOMPARE( exp.maxSize(), 10. );
41+
}
42+
{
43+
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.5), 0)" );
44+
QCOMPARE( bool( exp ), true );
45+
QCOMPARE( exp.type(), QgsScaleExpression::Area );
46+
}
47+
{
48+
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.57), 0)" );
49+
QCOMPARE( bool( exp ), true );
50+
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
51+
}
3252
{
3353
QgsScaleExpression exp( "scale_linear(column, 1, 7, 2, 10)" );
3454
QCOMPARE( bool( exp ), true );
@@ -50,17 +70,17 @@ class TestQgsScaleExpression: public QObject
5070
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
5171
}
5272
{
53-
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.51)" );
73+
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.51), 0)" );
5474
QCOMPARE( bool( exp ), false );
5575
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
5676
}
5777
{
58-
QgsScaleExpression exp( "scale_exp(column, 1, 7, a, 10, 0.5)" );
78+
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, a, 10, 0.5), 0)" );
5979
QCOMPARE( bool( exp ), false );
6080
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
6181
}
6282
{
63-
QgsScaleExpression exp( "scale_exp(column, 1, 7)" );
83+
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7), 0)" );
6484
QCOMPARE( bool( exp ), false );
6585
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
6686
}

0 commit comments

Comments
 (0)