Skip to content

Commit c6577db

Browse files
elpasonyalldawson
authored andcommittedNov 29, 2021
Intersection min area
1 parent 7163749 commit c6577db

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,7 +6545,7 @@ static QVariant fcnFromBase64( const QVariantList &values, const QgsExpressionCo
65456545

65466546
typedef bool ( QgsGeometry::*RelationFunction )( const QgsGeometry &geometry ) const;
65476547

6548-
static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const RelationFunction &relationFunction, bool invert = false, double bboxGrow = 0, bool isNearestFunc = false )
6548+
static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const RelationFunction &relationFunction, bool invert = false, double bboxGrow = 0, bool isNearestFunc = false, bool isIntersectsFunc = false )
65496549
{
65506550

65516551
const QVariant sourceLayerRef = context->variable( QStringLiteral( "layer" ) ); //used to detect if sourceLayer and targetLayer are the same
@@ -6611,6 +6611,18 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
66116611
ENSURE_NO_EVAL_ERROR
66126612
bool cacheEnabled = cacheValue.toBool();
66136613

6614+
// Sixth parameter (for intersects only) is the min area
6615+
double minArea { -1 };
6616+
if ( isIntersectsFunc )
6617+
{
6618+
// Fourth parameter is the limit
6619+
node = QgsExpressionUtils::getNode( values.at( 5 ), parent ); //in expressions overlay functions throw the exception: Eval Error: Cannot convert '' to int
6620+
ENSURE_NO_EVAL_ERROR
6621+
const QVariant minAreaValue = node->eval( parent, context );
6622+
ENSURE_NO_EVAL_ERROR
6623+
minArea = QgsExpressionUtils::getDoubleValue( minAreaValue, parent );
6624+
}
6625+
66146626

66156627
FEAT_FROM_CONTEXT( context, feat )
66166628
const QgsGeometry geometry = feat.geometry();
@@ -6732,6 +6744,13 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
67326744

67336745
if ( ! relationFunction || ( geometry.*relationFunction )( feat2.geometry() ) ) // Calls the method provided as template argument for the function (e.g. QgsGeometry::intersects)
67346746
{
6747+
6748+
// Check min area for intersection (if set)
6749+
if ( isIntersectsFunc && minArea != -1 && geometry.intersection( feat2.geometry() ).area() <= minArea )
6750+
{
6751+
continue;
6752+
}
6753+
67356754
found = true;
67366755
foundCount++;
67376756

@@ -6787,7 +6806,7 @@ static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpress
67876806

67886807
static QVariant fcnGeomOverlayIntersects( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
67896808
{
6790-
return executeGeomOverlay( values, context, parent, &QgsGeometry::intersects );
6809+
return executeGeomOverlay( values, context, parent, &QgsGeometry::intersects, false, 0, false, true );
67916810
}
67926811

67936812
static QVariant fcnGeomOverlayContains( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
@@ -6817,7 +6836,7 @@ static QVariant fcnGeomOverlayWithin( const QVariantList &values, const QgsExpre
68176836

68186837
static QVariant fcnGeomOverlayDisjoint( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
68196838
{
6820-
return executeGeomOverlay( values, context, parent, &QgsGeometry::intersects, true );
6839+
return executeGeomOverlay( values, context, parent, &QgsGeometry::intersects, true, 0, false, true );
68216840
}
68226841

68236842
static QVariant fcnGeomOverlayNearest( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
@@ -7230,7 +7249,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
72307249
<< QgsExpressionFunction::Parameter( QStringLiteral( "expression" ), true, QVariant(), true )
72317250
<< QgsExpressionFunction::Parameter( QStringLiteral( "filter" ), true, QVariant(), true )
72327251
<< QgsExpressionFunction::Parameter( QStringLiteral( "limit" ), true, QVariant( -1 ), true )
7233-
<< QgsExpressionFunction::Parameter( QStringLiteral( "cache" ), true, QVariant( false ), false ),
7252+
<< QgsExpressionFunction::Parameter( QStringLiteral( "cache" ), true, QVariant( false ), false )
7253+
<< QgsExpressionFunction::Parameter( QStringLiteral( "min_intersection_area" ), true, QVariant( -1 ), false ),
72347254
i.value(), QStringLiteral( "GeometryGroup" ), QString(), true, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES, true );
72357255

72367256
// The current feature is accessed for the geometry, so this should not be cached

0 commit comments

Comments
 (0)
Please sign in to comment.