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 QGS_MARK_ALGORITHM_SOURCE
105 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
109 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
110 const Operation op =
static_cast<Operation
>( parameterAsEnum( parameters, u
"OPERATOR"_s, context ) );
111 const QString value = parameterAsString( parameters, u
"VALUE"_s, context );
113 QString matchingSinkId;
114 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, u
"OUTPUT"_s, context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
118 QString nonMatchingSinkId;
119 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, u
"FAIL_OUTPUT"_s, context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
121 const int idx = source->fields().lookupField( fieldName );
123 throw QgsProcessingException( QObject::tr(
"Field '%1' was not found in INPUT source" ).arg( fieldName ) );
125 const QMetaType::Type fieldType = source->fields().at( idx ).type();
127 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
133 method = QObject::tr(
"begins with" );
136 method = QObject::tr(
"contains" );
139 method = QObject::tr(
"does not contain" );
146 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
155 expr = u
"%1 = %3"_s.arg( fieldRef, quotedVal );
158 expr = u
"%1 != %3"_s.arg( fieldRef, quotedVal );
161 expr = u
"%1 > %3"_s.arg( fieldRef, quotedVal );
163 case GreaterThanEqualTo:
164 expr = u
"%1 >= %3"_s.arg( fieldRef, quotedVal );
167 expr = u
"%1 < %3"_s.arg( fieldRef, quotedVal );
169 case LessThanEqualTo:
170 expr = u
"%1 <= %3"_s.arg( fieldRef, quotedVal );
173 expr = u
"%1 LIKE '%2%'"_s.arg( fieldRef, value );
176 expr = u
"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
179 expr = u
"%1 IS NULL"_s.arg( fieldRef );
182 expr = u
"%1 IS NOT NULL"_s.arg( fieldRef );
185 expr = u
"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
190 if ( expression.hasParserError() )
195 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
197 const long count = source->featureCount();
199 const double step = count > 0 ? 100.0 / count : 1;
202 if ( !nonMatchingSink )
230 expressionContext.
setFields( source->fields() );
231 expression.prepare( &expressionContext );
243 if ( expression.evaluate( &expressionContext ).toBool() )
265 matchingSink->finalize();
268 if ( nonMatchingSink )
270 nonMatchingSink->finalize();
275 outputs.insert( u
"OUTPUT"_s, matchingSinkId );
276 if ( nonMatchingSink )
277 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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.