22 QString QgsExtractByAttributeAlgorithm::name()
const 24 return QStringLiteral(
"extractbyattribute" );
27 QString QgsExtractByAttributeAlgorithm::displayName()
const 29 return QObject::tr(
"Extract by attribute" );
32 QStringList QgsExtractByAttributeAlgorithm::tags()
const 34 return QObject::tr(
"extract,filter,attribute,value,contains,null,field" ).split(
',' );
37 QString QgsExtractByAttributeAlgorithm::group()
const 39 return QObject::tr(
"Vector selection" );
42 QString QgsExtractByAttributeAlgorithm::groupId()
const 44 return QStringLiteral(
"vectorselection" );
47 void QgsExtractByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
50 addParameter(
new QgsProcessingParameterField( QStringLiteral(
"FIELD" ), QObject::tr(
"Selection attribute" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
55 << QObject::tr(
">=" )
57 << QObject::tr(
"<=" )
58 << QObject::tr(
"begins with" )
59 << QObject::tr(
"contains" )
60 << QObject::tr(
"is null" )
61 << QObject::tr(
"is not null" )
62 << QObject::tr(
"does not contain" ) ) );
69 addParameter( failOutput );
72 QString QgsExtractByAttributeAlgorithm::shortHelpString()
const 74 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an input layer. " 75 "The criteria for adding features to the resulting layer is defined based on the values " 76 "of an attribute from the input layer." );
79 QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance()
const 81 return new QgsExtractByAttributeAlgorithm();
86 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
90 QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
91 Operation op =
static_cast< Operation
>( parameterAsEnum( parameters, QStringLiteral(
"OPERATOR" ), context ) );
92 QString value = parameterAsString( parameters, QStringLiteral(
"VALUE" ), context );
94 QString matchingSinkId;
95 std::unique_ptr< QgsFeatureSink > matchingSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, matchingSinkId, source->fields(),
96 source->wkbType(), source->sourceCrs() ) );
100 QString nonMatchingSinkId;
101 std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral(
"FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
102 source->wkbType(), source->sourceCrs() ) );
105 int idx = source->fields().lookupField( fieldName );
106 QVariant::Type fieldType = source->fields().at( idx ).type();
108 if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
114 method = QObject::tr(
"begins with" );
117 method = QObject::tr(
"contains" );
120 method = QObject::tr(
"does not contain" );
127 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
130 QString fieldRef = QgsExpression::quotedColumnRef( fieldName );
131 QString quotedVal = QgsExpression::quotedValue( value );
136 expr = QStringLiteral(
"%1 = %3" ).arg( fieldRef, quotedVal );
139 expr = QStringLiteral(
"%1 != %3" ).arg( fieldRef, quotedVal );
142 expr = QStringLiteral(
"%1 > %3" ).arg( fieldRef, quotedVal );
144 case GreaterThanEqualTo:
145 expr = QStringLiteral(
"%1 >= %3" ).arg( fieldRef, quotedVal );
148 expr = QStringLiteral(
"%1 < %3" ).arg( fieldRef, quotedVal );
150 case LessThanEqualTo:
151 expr = QStringLiteral(
"%1 <= %3" ).arg( fieldRef, quotedVal );
154 expr = QStringLiteral(
"%1 LIKE '%2%'" ).arg( fieldRef, value );
157 expr = QStringLiteral(
"%1 LIKE '%%2%'" ).arg( fieldRef, value );
160 expr = QStringLiteral(
"%1 IS NULL" ).arg( fieldRef );
163 expr = QStringLiteral(
"%1 IS NOT NULL" ).arg( fieldRef );
166 expr = QStringLiteral(
"%1 NOT LIKE '%%2%'" ).arg( fieldRef, value );
170 QgsExpression expression( expr );
171 if ( expression.hasParserError() )
176 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );
178 long count = source->featureCount();
180 double step = count > 0 ? 100.0 / count : 1;
183 if ( !nonMatchingSink )
208 expressionContext.setFields( source->fields() );
209 expression.prepare( &expressionContext );
220 expressionContext.setFeature( f );
221 if ( expression.evaluate( &expressionContext ).toBool() )
237 outputs.insert( QStringLiteral(
"OUTPUT" ), matchingSinkId );
238 if ( nonMatchingSink )
239 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.
A vector layer or feature source field 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.
An enum based parameter for processing algorithms, allowing for selection from predefined values...
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.
A string parameter for processing algorithms.
Any vector layer with geometry.