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 & )
57 QObject::tr(
"Operator" ),
65 << QObject::tr(
"begins with" )
66 << QObject::tr(
"contains" )
67 << QObject::tr(
"is null" )
68 << QObject::tr(
"is not null" )
69 << QObject::tr(
"does not contain" ),
79 addParameter( failOutput );
82QString QgsExtractByAttributeAlgorithm::shortHelpString()
const
85 "This algorithm creates a new vector layer that only contains matching features from an input layer. "
86 "The criteria for adding features to the resulting layer is defined based on the values "
87 "of an attribute from the input layer."
91QString QgsExtractByAttributeAlgorithm::shortDescription()
const
93 return QObject::tr(
"Creates a vector layer that only contains features matching an attribute value from an input layer." );
96QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance()
const
98 return new QgsExtractByAttributeAlgorithm();
103 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
107 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
108 const Operation op =
static_cast<Operation
>( parameterAsEnum( parameters, u
"OPERATOR"_s, context ) );
109 const QString value = parameterAsString( parameters, u
"VALUE"_s, context );
111 QString matchingSinkId;
112 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, u
"OUTPUT"_s, context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
116 QString nonMatchingSinkId;
117 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, u
"FAIL_OUTPUT"_s, context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
119 const int idx = source->fields().lookupField( fieldName );
121 throw QgsProcessingException( QObject::tr(
"Field '%1' was not found in INPUT source" ).arg( fieldName ) );
123 const QMetaType::Type fieldType = source->fields().at( idx ).type();
125 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
131 method = QObject::tr(
"begins with" );
134 method = QObject::tr(
"contains" );
137 method = QObject::tr(
"does not contain" );
144 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
153 expr = u
"%1 = %3"_s.arg( fieldRef, quotedVal );
156 expr = u
"%1 != %3"_s.arg( fieldRef, quotedVal );
159 expr = u
"%1 > %3"_s.arg( fieldRef, quotedVal );
161 case GreaterThanEqualTo:
162 expr = u
"%1 >= %3"_s.arg( fieldRef, quotedVal );
165 expr = u
"%1 < %3"_s.arg( fieldRef, quotedVal );
167 case LessThanEqualTo:
168 expr = u
"%1 <= %3"_s.arg( fieldRef, quotedVal );
171 expr = u
"%1 LIKE '%2%'"_s.arg( fieldRef, value );
174 expr = u
"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
177 expr = u
"%1 IS NULL"_s.arg( fieldRef );
180 expr = u
"%1 IS NOT NULL"_s.arg( fieldRef );
183 expr = u
"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
188 if ( expression.hasParserError() )
193 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
195 const long count = source->featureCount();
197 const double step = count > 0 ? 100.0 / count : 1;
200 if ( !nonMatchingSink )
226 expressionContext.
setFields( source->fields() );
227 expression.prepare( &expressionContext );
239 if ( expression.evaluate( &expressionContext ).toBool() )
256 matchingSink->finalize();
257 if ( nonMatchingSink )
258 nonMatchingSink->finalize();
261 outputs.insert( u
"OUTPUT"_s, matchingSinkId );
262 if ( nonMatchingSink )
263 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.