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 & )
56 addParameter( failOutput );
59 QString QgsExtractByExpressionAlgorithm::shortHelpString()
const 61 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. " 62 "The criteria for adding features to the resulting layer is based on a QGIS expression.\n\n" 63 "For more information about expressions see the <a href =\"{qgisdocs}/user_manual/working_with_vector/expression.html\">user manual</a>" );
66 QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::createInstance()
const 68 return new QgsExtractByExpressionAlgorithm();
73 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
77 QString expressionString = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
79 QString matchingSinkId;
80 std::unique_ptr< QgsFeatureSink > matchingSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, matchingSinkId, source->fields(),
81 source->wkbType(), source->sourceCrs() ) );
85 QString nonMatchingSinkId;
86 std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral(
"FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
87 source->wkbType(), source->sourceCrs() ) );
89 QgsExpression expression( expressionString );
90 if ( expression.hasParserError() )
95 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );
97 long count = source->featureCount();
99 double step = count > 0 ? 100.0 / count : 1;
102 if ( !nonMatchingSink )
127 expressionContext.setFields( source->fields() );
128 expression.prepare( &expressionContext );
139 expressionContext.setFeature( f );
140 if ( expression.evaluate( &expressionContext ).toBool() )
156 outputs.insert( QStringLiteral(
"OUTPUT" ), matchingSinkId );
157 if ( nonMatchingSink )
158 outputs.insert( QStringLiteral(
"FAIL_OUTPUT" ), nonMatchingSinkId );
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
An expression parameter for processing algorithms.
void setProgress(double progress)
Sets the current progress for the feedback object.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A feature sink output for processing algorithms.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
Any vector layer with geometry.