22QString QgsExtractByExpressionAlgorithm::name()
const
24 return QStringLiteral(
"extractbyexpression" );
27QString QgsExtractByExpressionAlgorithm::displayName()
const
29 return QObject::tr(
"Extract by expression" );
32QStringList QgsExtractByExpressionAlgorithm::tags()
const
34 return QObject::tr(
"extract,filter,expression,field" ).split(
',' );
37QString QgsExtractByExpressionAlgorithm::group()
const
39 return QObject::tr(
"Vector selection" );
42QString QgsExtractByExpressionAlgorithm::groupId()
const
44 return QStringLiteral(
"vectorselection" );
47void QgsExtractByExpressionAlgorithm::initAlgorithm(
const QVariantMap & )
55 addParameter( failOutput );
58QString QgsExtractByExpressionAlgorithm::shortHelpString()
const
60 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. "
61 "The criteria for adding features to the resulting layer is based on a QGIS expression.\n\n"
62 "For help with QGIS expression functions, see the inbuilt help for specific functions "
63 "which is available in the expression builder." );
66QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::createInstance()
const
68 return new QgsExtractByExpressionAlgorithm();
73 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
77 const QString expressionString = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
79 QString matchingSinkId;
80 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
84 QString nonMatchingSinkId;
85 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, QStringLiteral(
"FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
88 if ( expression.hasParserError() )
93 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
95 const long count = source->featureCount();
97 const double step = count > 0 ? 100.0 / count : 1;
100 if ( !nonMatchingSink )
117 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
126 expressionContext.
setFields( source->fields() );
127 expression.prepare( &expressionContext );
139 if ( expression.evaluate( &expressionContext ).toBool() )
142 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
147 throw QgsProcessingException( writeFeatureError( nonMatchingSink.get(), parameters, QStringLiteral(
"FAIL_OUTPUT" ) ) );
156 matchingSink->finalize();
157 if ( nonMatchingSink )
158 nonMatchingSink->finalize();
161 outputs.insert( QStringLiteral(
"OUTPUT" ), matchingSinkId );
162 if ( nonMatchingSink )
163 outputs.insert( QStringLiteral(
"FAIL_OUTPUT" ), nonMatchingSinkId );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ VectorAnyGeometry
Any vector layer with geometry.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Contains information about the context in which a processing algorithm is executed.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An expression parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.