Skip to content

Commit f355fcd

Browse files
committed
Fix geometry aggregate when first geometry is NULL (usertype)
1 parent adb13e1 commit f355fcd

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/core/qgsexpression.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ static QVariant fcnGeometry( const QVariantList &, const QgsExpressionContext *c
22352235
if ( !geom.isNull() )
22362236
return QVariant::fromValue( geom );
22372237
else
2238-
return QVariant();
2238+
return QVariant( QVariant::UserType );
22392239
}
22402240
static QVariant fcnGeomFromWKT( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
22412241
{

tests/src/core/testqgsexpression.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class TestQgsExpression: public QObject
147147
af4.setAttribute( QStringLiteral( "col3" ), 2 );
148148
af4.setAttribute( QStringLiteral( "col4" ), "" );
149149
QgsFeature af5( mAggregatesLayer->dataProvider()->fields(), 5 );
150-
af5.setGeometry( QgsGeometry::fromPoint( QgsPoint( 4, 0 ) ) );
150+
af5.setGeometry( QgsGeometry() );
151151
af5.setAttribute( QStringLiteral( "col1" ), 5 );
152152
af5.setAttribute( QStringLiteral( "col2" ), QVariant( QVariant::String ) );
153153
af5.setAttribute( QStringLiteral( "col3" ), 3 );
@@ -1327,7 +1327,7 @@ class TestQgsExpression: public QObject
13271327
QTest::newRow( "string aggregate 2" ) << "aggregate('test','min_length',\"col2\")" << false << QVariant( 5 );
13281328
QTest::newRow( "string concatenate" ) << "aggregate('test','concatenate',\"col2\",concatenator:=' , ')" << false << QVariant( "test1 , test2 , test3 , test4" );
13291329

1330-
QTest::newRow( "geometry collect" ) << "geom_to_wkt(aggregate('aggregate_layer','collect',$geometry))" << false << QVariant( QStringLiteral( "MultiPoint ((0 0),(1 0),(2 0),(3 0),(4 0),(5 0))" ) );
1330+
QTest::newRow( "geometry collect" ) << "geom_to_wkt(aggregate('aggregate_layer','collect',$geometry))" << false << QVariant( QStringLiteral( "MultiPoint ((0 0),(1 0),(2 0),(3 0),(5 0))" ) );
13311331

13321332
QTest::newRow( "sub expression" ) << "aggregate('test','sum',\"col1\" * 2)" << false << QVariant( 65 * 2 );
13331333
QTest::newRow( "bad sub expression" ) << "aggregate('test','sum',\"xcvxcv\" * 2)" << true << QVariant();
@@ -1411,7 +1411,8 @@ class TestQgsExpression: public QObject
14111411
QTest::newRow( "max_length" ) << "max_length(\"col2\")" << false << QVariant( 7 );
14121412
QTest::newRow( "concatenate" ) << "concatenate(\"col2\",concatenator:=',')" << false << QVariant( "test,,test333,test4,,test4" );
14131413

1414-
QTest::newRow( "geometry collect" ) << "geom_to_wkt(collect($geometry))" << false << QVariant( QStringLiteral( "MultiPoint ((0 0),(1 0),(2 0),(3 0),(4 0),(5 0))" ) );
1414+
QTest::newRow( "geometry collect" ) << "geom_to_wkt(collect($geometry))" << false << QVariant( QStringLiteral( "MultiPoint ((0 0),(1 0),(2 0),(3 0),(5 0))" ) );
1415+
QTest::newRow( "geometry collect with null geometry first" ) << "geom_to_wkt(collect($geometry, filter:=\"col3\"=3))" << false << QVariant( QStringLiteral( "MultiPoint ((5 0))" ) );
14151416

14161417
QTest::newRow( "bad expression" ) << "sum(\"xcvxcvcol1\")" << true << QVariant();
14171418
QTest::newRow( "aggregate named" ) << "sum(expression:=\"col1\")" << false << QVariant( 24.0 );
@@ -1707,24 +1708,25 @@ class TestQgsExpression: public QObject
17071708
QTest::addColumn<QString>( "string" );
17081709
QTest::addColumn<QgsGeometry>( "geom" );
17091710
QTest::addColumn<bool>( "evalError" );
1710-
QTest::addColumn<double>( "result" );
1711+
QTest::addColumn<QVariant>( "result" );
17111712

17121713
QgsPoint point( 123, 456 );
17131714
QgsPolyline line;
17141715
line << QgsPoint( 1, 1 ) << QgsPoint( 4, 2 ) << QgsPoint( 3, 1 );
17151716

1716-
QTest::newRow( "geom x" ) << "$x" << QgsGeometry::fromPoint( point ) << false << 123.;
1717-
QTest::newRow( "geom y" ) << "$y" << QgsGeometry::fromPoint( point ) << false << 456.;
1718-
QTest::newRow( "geom xat" ) << "xat(-1)" << QgsGeometry::fromPolyline( line ) << false << 3.;
1719-
QTest::newRow( "geom yat" ) << "yat(1)" << QgsGeometry::fromPolyline( line ) << false << 2.;
1717+
QTest::newRow( "geom x" ) << "$x" << QgsGeometry::fromPoint( point ) << false << QVariant( 123. );
1718+
QTest::newRow( "geom y" ) << "$y" << QgsGeometry::fromPoint( point ) << false << QVariant( 456. );
1719+
QTest::newRow( "geom xat" ) << "xat(-1)" << QgsGeometry::fromPolyline( line ) << false << QVariant( 3. );
1720+
QTest::newRow( "geom yat" ) << "yat(1)" << QgsGeometry::fromPolyline( line ) << false << QVariant( 2. );
1721+
QTest::newRow( "null geometry" ) << "$geometry" << QgsGeometry() << false << QVariant( QVariant::UserType );
17201722
}
17211723

17221724
void eval_geometry()
17231725
{
17241726
QFETCH( QString, string );
17251727
QFETCH( QgsGeometry, geom );
17261728
QFETCH( bool, evalError );
1727-
QFETCH( double, result );
1729+
QFETCH( QVariant, result );
17281730

17291731
QgsFeature f;
17301732
f.setGeometry( geom );
@@ -1736,7 +1738,7 @@ class TestQgsExpression: public QObject
17361738
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
17371739
QVariant out = exp.evaluate( &context );
17381740
QCOMPARE( exp.hasEvalError(), evalError );
1739-
QCOMPARE( out.toDouble(), result );
1741+
QCOMPARE( out, result );
17401742
}
17411743

17421744
void eval_geometry_calc()

0 commit comments

Comments
 (0)