Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][processing] New modeler algorithm for conditional branches …
…in models This algorithm allows useres to setup multiple conditions (via qgis expressions), which cause their corresponding branch of the model to be run or skipped depending on the result of the expression evaluation. Sponsored by Andreas Neumann
- Loading branch information
1 parent
05bc716
commit 6a1b106
Showing
11 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/analysis/processing/qgsalgorithmconditionalbranch.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*************************************************************************** | ||
qgsalgorithmconditionalbranch.cpp | ||
--------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmconditionalbranch.h" | ||
#include "qgsapplication.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsConditionalBranchAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "condition" ); | ||
} | ||
|
||
QString QgsConditionalBranchAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Conditional branch" ); | ||
} | ||
|
||
QStringList QgsConditionalBranchAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "if,logic,test" ).split( ',' ); | ||
} | ||
|
||
QString QgsConditionalBranchAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Modeler tools" ); | ||
} | ||
|
||
QString QgsConditionalBranchAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "modelertools" ); | ||
} | ||
|
||
QgsProcessingAlgorithm::Flags QgsConditionalBranchAlgorithm::flags() const | ||
{ | ||
return FlagHideFromToolbox | FlagSkipGenericModelLogging; | ||
} | ||
|
||
QString QgsConditionalBranchAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm adds a conditional branch into a model, allowing parts of the model to be executed based on the result of an expression evaluation." ); | ||
} | ||
|
||
QString QgsConditionalBranchAlgorithm::shortDescription() const | ||
{ | ||
return QObject::tr( "Adds a conditional branch into a model, allowing parts of the model to be selectively executed." ); | ||
} | ||
|
||
QgsConditionalBranchAlgorithm *QgsConditionalBranchAlgorithm::createInstance() const | ||
{ | ||
return new QgsConditionalBranchAlgorithm(); | ||
} | ||
|
||
QgsConditionalBranchAlgorithm::~QgsConditionalBranchAlgorithm() | ||
{ | ||
qDeleteAll( mOutputs ); | ||
} | ||
|
||
void QgsConditionalBranchAlgorithm::initAlgorithm( const QVariantMap &configuration ) | ||
{ | ||
const QVariantList branches = configuration.value( QStringLiteral( "conditions" ) ).toList(); | ||
for ( const QVariant &branch : branches ) | ||
{ | ||
const QVariantMap branchDef = branch.toMap(); | ||
const QString name = branchDef.value( QStringLiteral( "name" ) ).toString(); | ||
mOutputs.append( new Output( name, branchDef.value( QStringLiteral( "expression" ) ).toString() ) ); | ||
addOutput( new QgsProcessingOutputConditionalBranch( name, name ) ); | ||
} | ||
} | ||
|
||
QVariantMap QgsConditionalBranchAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
QgsExpressionContext expressionContext = createExpressionContext( parameters, context ); | ||
|
||
QVariantMap results; | ||
for ( Output *output : qgis::as_const( mOutputs ) ) | ||
{ | ||
output->expression.prepare( &expressionContext ); | ||
const QVariant res = output->expression.evaluate( &expressionContext ); | ||
results.insert( output->name, res ); | ||
if ( res.toBool() ) | ||
{ | ||
feedback->pushInfo( QObject::tr( "Condition %1 passed" ).arg( output->name ) ); | ||
} | ||
else | ||
{ | ||
feedback->pushInfo( QObject::tr( "Condition %1 failed" ).arg( output->name ) ); | ||
} | ||
} | ||
qDeleteAll( mOutputs ); | ||
mOutputs.clear(); | ||
return results; | ||
} | ||
|
||
|
||
///@endcond PRIVATE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/*************************************************************************** | ||
qgsalgorithmconditionalbranch.h | ||
--------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMCONDITIONALBRANCH_H | ||
#define QGSALGORITHMCONDITIONALBRANCH_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
class QgsProcessingModelAlgorithm; | ||
class QTableWidget; | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Feature filter algorithm for modeler. | ||
* Accepts a list of expressions and names and creates outputs where | ||
* matching features are sent to. | ||
* | ||
* \since QGIS 3.2 | ||
*/ | ||
class QgsConditionalBranchAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
public: | ||
QgsConditionalBranchAlgorithm() = default; | ||
~QgsConditionalBranchAlgorithm() override; | ||
|
||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
Flags flags() const override; | ||
QString shortHelpString() const override; | ||
QString shortDescription() const override; | ||
QgsConditionalBranchAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
|
||
private: | ||
struct Output | ||
{ | ||
Output( const QString &name, const QString &expression ) | ||
: name( name ) | ||
, expression( expression ) | ||
{} | ||
QString name; | ||
QgsExpression expression; | ||
}; | ||
|
||
QList<Output *> mOutputs; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMCONDITIONALBRANCH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.