22using namespace Qt::StringLiterals;
26QString QgsExtractByAttributeAlgorithm::name()
const
28 return u
"extractbyattribute"_s;
31QString QgsExtractByAttributeAlgorithm::displayName()
const
33 return QObject::tr(
"Extract by attribute" );
36QStringList QgsExtractByAttributeAlgorithm::tags()
const
38 return QObject::tr(
"extract,filter,attribute,value,contains,null,field" ).split(
',' );
41QString QgsExtractByAttributeAlgorithm::group()
const
43 return QObject::tr(
"Vector selection" );
46QString QgsExtractByAttributeAlgorithm::groupId()
const
48 return u
"vectorselection"_s;
51void QgsExtractByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
55 addParameter(
new QgsProcessingParameterEnum( u
"OPERATOR"_s, QObject::tr(
"Operator" ), QStringList() << QObject::tr(
"=" ) << QObject::tr(
"≠" ) << QObject::tr(
">" ) << QObject::tr(
"≥" ) << QObject::tr(
"<" ) << QObject::tr(
"≤" ) << QObject::tr(
"begins with" ) << QObject::tr(
"contains" ) << QObject::tr(
"is null" ) << QObject::tr(
"is not null" ) << QObject::tr(
"does not contain" ),
false, 0 ) );
61 addParameter( failOutput );
64QString QgsExtractByAttributeAlgorithm::shortHelpString()
const
66 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. "
67 "The criteria for adding features to the resulting layer is defined based on the values "
68 "of an attribute from the input layer." );
71QString QgsExtractByAttributeAlgorithm::shortDescription()
const
73 return QObject::tr(
"Creates a vector layer that only contains features matching an attribute value from an input layer." );
76QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance()
const
78 return new QgsExtractByAttributeAlgorithm();
83 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
87 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
88 const Operation op =
static_cast<Operation
>( parameterAsEnum( parameters, u
"OPERATOR"_s, context ) );
89 const QString value = parameterAsString( parameters, u
"VALUE"_s, context );
91 QString matchingSinkId;
92 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, u
"OUTPUT"_s, context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
96 QString nonMatchingSinkId;
97 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, u
"FAIL_OUTPUT"_s, context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
99 const int idx = source->fields().lookupField( fieldName );
101 throw QgsProcessingException( QObject::tr(
"Field '%1' was not found in INPUT source" ).arg( fieldName ) );
103 const QMetaType::Type fieldType = source->fields().at( idx ).type();
105 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
111 method = QObject::tr(
"begins with" );
114 method = QObject::tr(
"contains" );
117 method = QObject::tr(
"does not contain" );
124 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
133 expr = u
"%1 = %3"_s.arg( fieldRef, quotedVal );
136 expr = u
"%1 != %3"_s.arg( fieldRef, quotedVal );
139 expr = u
"%1 > %3"_s.arg( fieldRef, quotedVal );
141 case GreaterThanEqualTo:
142 expr = u
"%1 >= %3"_s.arg( fieldRef, quotedVal );
145 expr = u
"%1 < %3"_s.arg( fieldRef, quotedVal );
147 case LessThanEqualTo:
148 expr = u
"%1 <= %3"_s.arg( fieldRef, quotedVal );
151 expr = u
"%1 LIKE '%2%'"_s.arg( fieldRef, value );
154 expr = u
"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
157 expr = u
"%1 IS NULL"_s.arg( fieldRef );
160 expr = u
"%1 IS NOT NULL"_s.arg( fieldRef );
163 expr = u
"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
168 if ( expression.hasParserError() )
173 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
175 const long count = source->featureCount();
177 const double step = count > 0 ? 100.0 / count : 1;
180 if ( !nonMatchingSink )
206 expressionContext.
setFields( source->fields() );
207 expression.prepare( &expressionContext );
219 if ( expression.evaluate( &expressionContext ).toBool() )
236 matchingSink->finalize();
237 if ( nonMatchingSink )
238 nonMatchingSink->finalize();
241 outputs.insert( u
"OUTPUT"_s, matchingSinkId );
242 if ( nonMatchingSink )
243 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").
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
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 enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.