|
30 | 30 | #include "qgsfeature.h"
|
31 | 31 | #include "qgsgeometry.h"
|
32 | 32 | #include "qgslogger.h"
|
| 33 | +#include "qgsmaplayerregistry.h" |
33 | 34 | #include "qgsogcutils.h"
|
34 | 35 | #include "qgsvectorlayer.h"
|
35 | 36 | #include "qgssymbollayerv2utils.h"
|
@@ -1505,6 +1506,52 @@ static QVariant fcnSpecialColumn( const QVariantList& values, const QgsFeature*
|
1505 | 1506 | return QgsExpression::specialColumn( varName );
|
1506 | 1507 | }
|
1507 | 1508 |
|
| 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 | + |
1508 | 1555 | bool QgsExpression::registerFunction( QgsExpression::Function* function )
|
1509 | 1556 | {
|
1510 | 1557 | int fnIdx = functionIndex( function->name() );
|
@@ -1683,6 +1730,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
|
1683 | 1730 | << new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
|
1684 | 1731 | << new StaticFunction( "$scale", 0, fcnScale, "Record" )
|
1685 | 1732 | << new StaticFunction( "$uuid", 0, fcnUuid, "Record" )
|
| 1733 | + << new StaticFunction( "getFeature", 3, fcnGetFeature, "Record" ) |
1686 | 1734 |
|
1687 | 1735 | //return all attributes string for referencedColumns - this is caught by
|
1688 | 1736 | // QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the
|
|
0 commit comments