24using namespace Qt::StringLiterals;
28QString QgsSelectByAttributeAlgorithm::name()
const
30 return u
"selectbyattribute"_s;
33QString QgsSelectByAttributeAlgorithm::displayName()
const
35 return QObject::tr(
"Select by attribute" );
38QStringList QgsSelectByAttributeAlgorithm::tags()
const
40 return QObject::tr(
"extract,filter,attribute,value,contains,null,field" ).split(
',' );
43QString QgsSelectByAttributeAlgorithm::group()
const
45 return QObject::tr(
"Vector selection" );
48QString QgsSelectByAttributeAlgorithm::groupId()
const
50 return u
"vectorselection"_s;
53QString QgsSelectByAttributeAlgorithm::shortHelpString()
const
56 "This algorithm creates a selection in a vector layer.\n\n"
57 "The criteria for selected features is defined based on the values of an attribute from the input layer."
61QString QgsSelectByAttributeAlgorithm::shortDescription()
const
63 return QObject::tr(
"Selects features from a vector layer based on an attribute from the layer." );
66QgsSelectByAttributeAlgorithm *QgsSelectByAttributeAlgorithm::createInstance()
const
68 return new QgsSelectByAttributeAlgorithm();
71void QgsSelectByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
77 QObject::tr(
"Operator" ),
85 << QObject::tr(
"begins with" )
86 << QObject::tr(
"contains" )
87 << QObject::tr(
"is null" )
88 << QObject::tr(
"is not null" )
89 << QObject::tr(
"does not contain" ),
95 u
"SELECTION_METHOD"_s,
96 QObject::tr(
"Modify current selection by" ),
97 QStringList() << QObject::tr(
"creating new selection" ) << QObject::tr(
"adding to current selection" ) << QObject::tr(
"selecting within current selection" ) << QObject::tr(
"removing from current selection" ),
104 auto methodParam = std::make_unique<QgsProcessingParameterEnum>(
106 QObject::tr(
"Modify current selection by" ),
107 QStringList() << QObject::tr(
"creating new selection" ) << QObject::tr(
"adding to current selection" ) << QObject::tr(
"removing from current selection" ) << QObject::tr(
"selecting within current selection" ),
112 addParameter( std::move( methodParam ) );
119 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
125 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
126 const Operation op =
static_cast<Operation
>( parameterAsEnum( parameters, u
"OPERATOR"_s, context ) );
127 const QString value = parameterAsString( parameters, u
"VALUE"_s, context );
131 if ( parameters.value( u
"METHOD"_s ).isValid() )
133 method =
static_cast<Qgis::SelectBehavior>( parameterAsEnum( parameters, u
"METHOD"_s, context ) );
144 mLayerId = layer->
id();
149 throw QgsProcessingException( QObject::tr(
"Field '%1' was not found in INPUT layer." ).arg( fieldName ) );
152 const QMetaType::Type fieldType = layer->
fields().
at( idx ).
type();
153 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
159 method = QObject::tr(
"begins with" );
162 method = QObject::tr(
"contains" );
165 method = QObject::tr(
"does not contain" );
172 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
181 expr = u
"%1 = %2"_s.arg( fieldRef, quotedVal );
184 expr = u
"%1 <> %2"_s.arg( fieldRef, quotedVal );
187 expr = u
"%1 > %2"_s.arg( fieldRef, quotedVal );
189 case GreaterThanEqualTo:
190 expr = u
"%1 >= %2"_s.arg( fieldRef, quotedVal );
193 expr = u
"%1 < %2"_s.arg( fieldRef, quotedVal );
195 case LessThanEqualTo:
196 expr = u
"%1 <= %2"_s.arg( fieldRef, quotedVal );
199 expr = u
"%1 LIKE '%2%'"_s.arg( fieldRef, value );
202 expr = u
"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
205 expr = u
"%1 IS NULL"_s.arg( fieldRef );
208 expr = u
"%1 IS NOT NULL"_s.arg( fieldRef );
211 expr = u
"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
216 if ( expression.hasParserError() )
232 results.insert( u
"OUTPUT"_s, mLayerId );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ Hidden
Parameter is hidden and should not be shown to users.
SelectBehavior
Specifies how a selection should be applied.
@ IntersectSelection
Modify current selection to include only select features which match.
@ RemoveFromSelection
Remove from current selection.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of 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).
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A vector layer output for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE void selectByExpression(const QString &expression, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection, QgsExpressionContext *context=nullptr)
Selects matching features using an expression.
QgsExpressionContextScope * createExpressionContextScope() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...