Skip to content

Commit c5bcbaf

Browse files
committedSep 2, 2014
Merge pull request #1574 from mhugent/getFeatureExpression
Get feature expression
2 parents f97d5e3 + dab5ff9 commit c5bcbaf

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎resources/function_help/getFeature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h3>getFeature function</h3>
2+
Returns the first feature of a layer matching a given attribute value
3+
4+
<h4>Syntax</h4>
5+
<pre>getFeature( layer, attributeField, value )</pre>
6+

‎src/core/qgsexpression.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsfeature.h"
3131
#include "qgsgeometry.h"
3232
#include "qgslogger.h"
33+
#include "qgsmaplayerregistry.h"
3334
#include "qgsogcutils.h"
3435
#include "qgsvectorlayer.h"
3536
#include "qgssymbollayerv2utils.h"
@@ -1505,6 +1506,52 @@ static QVariant fcnSpecialColumn( const QVariantList& values, const QgsFeature*
15051506
return QgsExpression::specialColumn( varName );
15061507
}
15071508

1509+
static QVariant fcnGetFeature( const QVariantList& values, const QgsFeature* f, QgsExpression* parent )
1510+
{
1511+
//arguments: 1. layer id / name, 2. key attribute, 3. eq value
1512+
QString layerString = getStringValue( values.at( 0 ), parent );
1513+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerString ) ); //search by id first
1514+
if ( !vl )
1515+
{
1516+
QList<QgsMapLayer *> layersByName = QgsMapLayerRegistry::instance()->mapLayersByName( layerString );
1517+
if ( layersByName.size() > 0 )
1518+
{
1519+
vl = qobject_cast<QgsVectorLayer*>( layersByName.at( 0 ) );
1520+
}
1521+
}
1522+
1523+
//no layer found
1524+
if ( !vl )
1525+
{
1526+
return QVariant();
1527+
}
1528+
1529+
QString attribute = getStringValue( values.at( 1 ), parent );
1530+
int attributeId = vl->fieldNameIndex( attribute );
1531+
if ( attributeId == -1 )
1532+
{
1533+
return QVariant();
1534+
}
1535+
1536+
const QVariant& attVal = values.at( 2 );
1537+
QgsFeatureRequest req;
1538+
if ( !parent->needsGeometry() )
1539+
{
1540+
req.setFlags( QgsFeatureRequest::NoGeometry );
1541+
}
1542+
QgsFeatureIterator fIt = vl->getFeatures( req );
1543+
1544+
QgsFeature fet;
1545+
while ( fIt.nextFeature( fet ) )
1546+
{
1547+
if ( fet.attribute( attributeId ) == attVal )
1548+
{
1549+
return QVariant::fromValue( fet );
1550+
}
1551+
}
1552+
return QVariant();
1553+
}
1554+
15081555
bool QgsExpression::registerFunction( QgsExpression::Function* function )
15091556
{
15101557
int fnIdx = functionIndex( function->name() );
@@ -1683,6 +1730,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
16831730
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
16841731
<< new StaticFunction( "$scale", 0, fcnScale, "Record" )
16851732
<< new StaticFunction( "$uuid", 0, fcnUuid, "Record" )
1733+
<< new StaticFunction( "getFeature", 3, fcnGetFeature, "Record" )
16861734

16871735
//return all attributes string for referencedColumns - this is caught by
16881736
// QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the

0 commit comments

Comments
 (0)
Please sign in to comment.