22using namespace Qt::StringLiterals;
26QString QgsExtractByExpressionAlgorithm::name()
const
28 return u
"extractbyexpression"_s;
31QString QgsExtractByExpressionAlgorithm::displayName()
const
33 return QObject::tr(
"Extract by expression" );
36QStringList QgsExtractByExpressionAlgorithm::tags()
const
38 return QObject::tr(
"extract,filter,expression,field" ).split(
',' );
41QString QgsExtractByExpressionAlgorithm::group()
const
43 return QObject::tr(
"Vector selection" );
46QString QgsExtractByExpressionAlgorithm::groupId()
const
48 return u
"vectorselection"_s;
51void QgsExtractByExpressionAlgorithm::initAlgorithm(
const QVariantMap & )
59 addParameter( failOutput );
62QString QgsExtractByExpressionAlgorithm::shortHelpString()
const
64 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. "
65 "The criteria for adding features to the resulting layer is based on a QGIS expression.\n\n"
66 "For help with QGIS expression functions, see the inbuilt help for specific functions "
67 "which is available in the expression builder." );
70QString QgsExtractByExpressionAlgorithm::shortDescription()
const
72 return QObject::tr(
"Creates a vector layer that only contains features matching a QGIS expression from an input layer." );
75QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::createInstance()
const
77 return new QgsExtractByExpressionAlgorithm();
82 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
86 const QString expressionString = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
88 QString matchingSinkId;
89 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, u
"OUTPUT"_s, context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
93 QString nonMatchingSinkId;
94 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, u
"FAIL_OUTPUT"_s, context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
97 if ( expression.hasParserError() )
102 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
104 const long count = source->featureCount();
106 const double step = count > 0 ? 100.0 / count : 1;
109 if ( !nonMatchingSink )
135 expressionContext.
setFields( source->fields() );
136 expression.prepare( &expressionContext );
148 if ( expression.evaluate( &expressionContext ).toBool() )
165 matchingSink->finalize();
166 if ( nonMatchingSink )
167 nonMatchingSink->finalize();
170 outputs.insert( u
"OUTPUT"_s, matchingSinkId );
171 if ( nonMatchingSink )
172 outputs.insert( u
"FAIL_OUTPUT"_s, 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.
Handles 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.
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.