Skip to content

Commit 25ad20c

Browse files
committed
[FEATURE] Add expression function get_feature_by_id
... like get_feature, just with an id instead of a field value
1 parent 10dc0d5 commit 25ad20c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/core/expression/qgsexpressionfunction.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,31 @@ static QVariant fcnTransformGeometry( const QVariantList &values, const QgsExpre
33153315
}
33163316

33173317

3318+
static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
3319+
{
3320+
QVariant result;
3321+
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
3322+
if ( !vl )
3323+
return result;
3324+
3325+
QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
3326+
3327+
QgsFeatureRequest req;
3328+
req.setFilterFid( fid );
3329+
req.setLimit( 1 );
3330+
if ( !parent->needsGeometry() )
3331+
{
3332+
req.setFlags( QgsFeatureRequest::NoGeometry );
3333+
}
3334+
QgsFeatureIterator fIt = vl->getFeatures( req );
3335+
3336+
QgsFeature fet;
3337+
if ( fIt.nextFeature( fet ) )
3338+
result = QVariant::fromValue( fet );
3339+
3340+
return result;
3341+
}
3342+
33183343
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
33193344
{
33203345
//arguments: 1. layer id / name, 2. key attribute, 3. eq value
@@ -4144,7 +4169,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
41444169
sFunctions << uuidFunc;
41454170

41464171
sFunctions
4147-
<< new QgsStaticExpressionFunction( QStringLiteral( "get_feature" ), 3, fcnGetFeature, QStringLiteral( "Record" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "QgsExpressionUtils::getFeature" ) );
4172+
<< new QgsStaticExpressionFunction( QStringLiteral( "get_feature" ), 3, fcnGetFeature, QStringLiteral( "Record" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "QgsExpressionUtils::getFeature" ) )
4173+
<< new QgsStaticExpressionFunction( QStringLiteral( "get_feature_by_id" ), 2, fcnGetFeatureById, QStringLiteral( "Record" ), QString(), false, QSet<QString>(), false );
41484174

41494175
QgsStaticExpressionFunction *isSelectedFunc = new QgsStaticExpressionFunction(
41504176
QStringLiteral( "is_selected" ),

0 commit comments

Comments
 (0)