Skip to content

Commit

Permalink
Ensure using @geometry/@feature variables trigger geometry fetching
Browse files Browse the repository at this point in the history
in expression calculation

Fixes #50791
  • Loading branch information
nyalldawson committed Feb 1, 2023
1 parent 903ce1d commit 9fb0da4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -201,6 +201,11 @@ bool QgsStaticExpressionFunction::usesGeometry( const QgsExpressionNodeFunction
return mUsesGeometry;
}

void QgsStaticExpressionFunction::setUsesGeometryFunction( const std::function<bool ( const QgsExpressionNodeFunction * )> &usesGeometry )
{
mUsesGeometryFunc = usesGeometry;
}

QSet<QString> QgsStaticExpressionFunction::referencedColumns( const QgsExpressionNodeFunction *node ) const
{
if ( mReferencedColumnsFunc )
Expand Down Expand Up @@ -8415,6 +8420,21 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
return false;
}
);
varFunction->setUsesGeometryFunction(
[]( const QgsExpressionNodeFunction * node ) -> bool
{
if ( node && node->args()->count() > 0 )
{
QgsExpressionNode *argNode = node->args()->at( 0 );
if ( QgsExpressionNodeLiteral *literal = dynamic_cast<QgsExpressionNodeLiteral *>( argNode ) )
{
if ( literal->value() == QLatin1String( "geometry" ) || literal->value() == QLatin1String( "feature" ) )
return true;
}
}
return false;
}
);

functions
<< varFunction;
Expand Down
8 changes: 8 additions & 0 deletions src/core/expression/qgsexpressionfunction.h
Expand Up @@ -473,6 +473,13 @@ class QgsStaticExpressionFunction : public QgsExpressionFunction

bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;

/**
* Set a function that will be called when determining if the function requires feature geometry or not.
*
* \since QGIS 3.30
*/
void setUsesGeometryFunction( const std::function< bool( const QgsExpressionNodeFunction *node )> &usesGeometry );

QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;

bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
Expand All @@ -487,6 +494,7 @@ class QgsStaticExpressionFunction : public QgsExpressionFunction
*/
void setIsStaticFunction( const std::function< bool ( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) > &isStatic );


/**
* Tag this function as either static or not static.
* This will indicate that the function is always expected to return the same value for
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -2322,6 +2322,7 @@ class TestQgsExpression: public QObject

// newer form
QgsExpression exp2( QStringLiteral( "@feature" ) );
QVERIFY( exp2.needsGeometry() );
v = exp2.evaluate( &context );
evalFeature = v.value<QgsFeature>();
QCOMPARE( evalFeature.id(), f.id() );
Expand All @@ -2344,6 +2345,7 @@ class TestQgsExpression: public QObject

// newer form
QgsExpression exp2( QStringLiteral( "geom_to_wkt(@geometry)" ) );
QVERIFY( exp2.needsGeometry() );
v = exp2.evaluate( &contextWithGeometry );
QCOMPARE( v.toString(), QStringLiteral( "Point (1 2)" ) );
v = exp2.evaluate( &contextWithNoGeometry );
Expand Down

0 comments on commit 9fb0da4

Please sign in to comment.