Skip to content

Commit

Permalink
[refFunctions] fix overlay test
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Sep 9, 2020
1 parent 6192cf9 commit 751ad05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5761,11 +5761,7 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
request.setDestinationCrs( sourceLayer->crs(), TransformContext ); //if crs are not the same, cached target will be reprojected to source crs
}

QVariant currentFeatId;
if ( sourceLayer && sourceLayer->id() == targetLayerValue )
{
currentFeatId = feat.id(); //if sourceLayer and targetLayer are the same, current feature have to be excluded from spatial check
}
bool sameLayers = ( sourceLayer && sourceLayer->id() == targetLayer->id() );

QgsRectangle intDomain = geometry.boundingBox();
if ( bboxGrow != 0 )
Expand Down Expand Up @@ -5812,8 +5808,7 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
QList<QgsFeatureId> fidsList;
if ( isNearestFunc )
{
// TODO : add 1 to limit if target and source layers are the same and ignore this feature
fidsList = spatialIndex.nearestNeighbor( geometry, limit, max_distance );
fidsList = spatialIndex.nearestNeighbor( geometry, sameLayers ? limit + 1 : limit, max_distance );
}
else
{
Expand All @@ -5823,8 +5818,10 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
QListIterator<QgsFeatureId> i( fidsList );
while ( i.hasNext() )
{
// TODO : ignore feature if same as this (and remove logic below)
features.append( cachedTarget->getFeature( i.next() ) );
QgsFeatureId fId2 = i.next();
if ( sameLayers && feat.id() == fId2 )
continue;
features.append( cachedTarget->getFeature( fId2 ) );
}

}
Expand All @@ -5834,11 +5831,12 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
// get the features from the target layer
request.setFilterRect( intDomain );
QgsFeatureIterator fit = targetLayer->getFeatures( request );
QgsFeature feat;
while ( fit.nextFeature( feat ) )
QgsFeature feat2;
while ( fit.nextFeature( feat2 ) )
{
// TODO : ignore feature if same as this (and remove logic below)
features.append( feat );
if ( sameLayers && feat.id() == feat2.id() )
continue;
features.append( feat2 );
}
}

Expand All @@ -5858,17 +5856,9 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
QListIterator<QgsFeature> i( features );
while ( i.hasNext() )
{
QgsFeature feat = i.next();

// TODO : remove this, and implement above
// currentFeatId is only set if sourceLayer and targetLayer are the same,
// in which case current feature have to be excluded from spatial check
if ( !currentFeatId.isNull() && currentFeatId.toLongLong() == feat.id() )
{
continue;
}
QgsFeature feat2 = i.next();

if ( ! relationFunction || ( feat.geometry().*relationFunction )( geometry ) ) // Calls the method provided as template argument for the function (e.g. QgsGeometry::intersects)
if ( ! relationFunction || ( feat2.geometry().*relationFunction )( geometry ) ) // Calls the method provided as template argument for the function (e.g. QgsGeometry::intersects)
{
found = true;

Expand All @@ -5879,13 +5869,13 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
if ( !invert )
{
// We want a list of attributes / geometries / other expression values, evaluate now
subContext.setFeature( feat );
subContext.setFeature( feat2 );
results.append( subExpression.evaluate( &subContext ) );
}
else
{
// If not, results is a list of found ids, which we'll inverse and evaluate below
results.append( feat.id() );
results.append( feat2.id() );
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsoverlayexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ void TestQgsOverlayExpression::testOverlaySelf()
{
QgsExpressionContext context;
context.appendScope( QgsExpressionContextUtils::projectScope( QgsProject::instance() ) );
context.appendScope( QgsExpressionContextUtils::layerScope( mPolyLayer ) );

QgsExpression exp( "geometry_overlay_intersects('polys')" );
QVERIFY2( exp.prepare( &context ), exp.parserErrorString().toUtf8().constData() );
Expand Down

0 comments on commit 751ad05

Please sign in to comment.