Skip to content

Commit d724b88

Browse files
enricoferm-kuhn
authored andcommittedJul 3, 2020
exclude current feature for self overlay layer
1 parent 33e0710 commit d724b88

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,16 +5650,20 @@ static QVariant fcnFromBase64( const QVariantList &values, const QgsExpressionCo
56505650
return QVariant( decoded );
56515651
}
56525652

5653-
typedef std::function < QVariant( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, int neighbors, double max_distance, double bboxGrow ) > overlayFunc;
5653+
typedef std::function < QVariant( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, QVariant currentFeatId, int neighbors, double max_distance, double bboxGrow ) > overlayFunc;
56545654

56555655
static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, bool testOnly, const overlayFunc &overlayFunction, bool invert = false, double bboxGrow = 0 )
56565656
{
5657+
5658+
const QVariant sourceLayerRef = context->variable( QStringLiteral( "layer" ) ); //used to detect if sorceLayer and targetLayer are the same
5659+
QgsVectorLayer *sourceLayer = QgsExpressionUtils::getVectorLayer( sourceLayerRef, parent );
5660+
56575661
// First parameter is the overlay layer
56585662
QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
56595663
ENSURE_NO_EVAL_ERROR
56605664

56615665
const bool layerCanBeCached = node->isStatic( parent, context );
5662-
QVariant layerValue = node->eval( parent, context );
5666+
QVariant targetLayerValue = node->eval( parent, context );
56635667
ENSURE_NO_EVAL_ERROR
56645668

56655669
QString subExpString;
@@ -5675,10 +5679,10 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
56755679
QgsSpatialIndex spatialIndex;
56765680
std::shared_ptr<QgsVectorLayer> cachedTarget;
56775681

5678-
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( layerValue, parent );
5682+
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( targetLayerValue, parent );
56795683
if ( !layer ) // No layer, no joy
56805684
{
5681-
parent->setEvalErrorString( QObject::tr( "Layer '%1' could not be loaded." ).arg( layerValue.toString() ) );
5685+
parent->setEvalErrorString( QObject::tr( "Layer '%1' could not be loaded." ).arg( targetLayerValue.toString() ) );
56825686
return QVariant();
56835687
}
56845688

@@ -5752,15 +5756,21 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
57525756
FEAT_FROM_CONTEXT( context, feat )
57535757
const QgsGeometry geometry = feat.geometry();
57545758

5755-
return overlayFunction( subExpression, subContext, spatialIndex, cachedTarget, geometry, testOnly, invert, neighbors, max_distance, bboxGrow );
5759+
QVariant currentFeatId;
5760+
if (sourceLayer->id() == targetLayerValue)
5761+
{
5762+
currentFeatId = feat.id(); //if sourceLayer and targetLayer are the same, current feature have to be excluded from spatial check
5763+
}
5764+
5765+
return overlayFunction( subExpression, subContext, spatialIndex, cachedTarget, geometry, testOnly, invert, currentFeatId, neighbors, max_distance, bboxGrow );
57565766
}
57575767

57585768
// Intersect functions:
57595769

57605770
typedef bool ( QgsGeometry::*t_relationFunction )( const QgsGeometry &geometry ) const;
57615771

57625772
template <t_relationFunction T>
5763-
static QVariant indexedFilteredOverlay( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, int neighbors, double max_distance, double bboxGrow = 0 )
5773+
static QVariant indexedFilteredOverlay( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, QVariant currentFeatId, int neighbors, double max_distance, double bboxGrow = 0 )
57645774
{
57655775
QgsRectangle intDomain = geometry.boundingBox();
57665776
if ( bboxGrow != 0 )
@@ -5774,6 +5784,10 @@ static QVariant indexedFilteredOverlay( QgsExpression &subExp, QgsExpressionCont
57745784
QVariantList results;
57755785
for ( QgsFeatureId id : targetFeatureIds )
57765786
{
5787+
if (!currentFeatId.isNull() && currentFeatId.toLongLong() == id)
5788+
{
5789+
continue; //if sourceLayer and targetLayer are the same, current feature have to be excluded from spatial check
5790+
}
57775791

57785792
QgsFeature feat = cachedTarget->getFeature( id );
57795793

@@ -5826,7 +5840,7 @@ static QVariant indexedFilteredOverlay( QgsExpression &subExp, QgsExpressionCont
58265840
}
58275841
}
58285842

5829-
static QVariantList indexedFilteredNearest( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, int neighbors, double max_distance, double bboxGrow = 0 )
5843+
static QVariantList indexedFilteredNearest( QgsExpression &subExp, QgsExpressionContext &subContext, const QgsSpatialIndex &spatialIndex, std::shared_ptr<QgsVectorLayer> cachedTarget, const QgsGeometry &geometry, bool testOnly, bool invert, QVariant currentFeatId, int neighbors, double max_distance, double bboxGrow = 0 )
58305844
{
58315845

58325846
const QList<QgsFeatureId> targetFeatureIds = spatialIndex.nearestNeighbor( geometry, neighbors, max_distance );

0 commit comments

Comments
 (0)
Please sign in to comment.