22 QString QgsExtractByExpressionAlgorithm::name()
const
24 return QStringLiteral(
"extractbyexpression" );
27 QString QgsExtractByExpressionAlgorithm::displayName()
const
29 return QObject::tr(
"Extract by expression" );
32 QStringList QgsExtractByExpressionAlgorithm::tags()
const
34 return QObject::tr(
"extract,filter,expression,field" ).split(
',' );
37 QString QgsExtractByExpressionAlgorithm::group()
const
39 return QObject::tr(
"Vector selection" );
42 QString QgsExtractByExpressionAlgorithm::groupId()
const
44 return QStringLiteral(
"vectorselection" );
47 void QgsExtractByExpressionAlgorithm::initAlgorithm(
const QVariantMap & )
57 addParameter( failOutput );
60 QString QgsExtractByExpressionAlgorithm::shortHelpString()
const
62 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. "
63 "The criteria for adding features to the resulting layer is based on a QGIS expression.\n\n"
64 "For help with QGIS expression functions, see the inbuilt help for specific functions "
65 "which is available in the expression builder." );
68 QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::createInstance()
const
70 return new QgsExtractByExpressionAlgorithm();
75 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
79 const QString expressionString = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
81 QString matchingSinkId;
82 std::unique_ptr< QgsFeatureSink > matchingSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, matchingSinkId, source->fields(),
83 source->wkbType(), source->sourceCrs() ) );
87 QString nonMatchingSinkId;
88 std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral(
"FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
89 source->wkbType(), source->sourceCrs() ) );
92 if ( expression.hasParserError() )
97 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
99 const long count = source->featureCount();
101 const double step = count > 0 ? 100.0 / count : 1;
104 if ( !nonMatchingSink )
121 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
130 expressionContext.
setFields( source->fields() );
131 expression.prepare( &expressionContext );
143 if ( expression.evaluate( &expressionContext ).toBool() )
146 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
151 throw QgsProcessingException( writeFeatureError( nonMatchingSink.get(), parameters, QStringLiteral(
"FAIL_OUTPUT" ) ) );
161 outputs.insert( QStringLiteral(
"OUTPUT" ), matchingSinkId );
162 if ( nonMatchingSink )
163 outputs.insert( QStringLiteral(
"FAIL_OUTPUT" ), nonMatchingSinkId );