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 QGS_MARK_ALGORITHM_SOURCE
121 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
127 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
128 const Operation op =
static_cast<Operation
>( parameterAsEnum( parameters, u
"OPERATOR"_s, context ) );
129 const QString value = parameterAsString( parameters, u
"VALUE"_s, context );
133 if ( parameters.value( u
"METHOD"_s ).isValid() )
135 method =
static_cast<Qgis::SelectBehavior>( parameterAsEnum( parameters, u
"METHOD"_s, context ) );
146 mLayerId = layer->id();
148 const int idx = layer->fields().lookupField( fieldName );
151 throw QgsProcessingException( QObject::tr(
"Field '%1' was not found in INPUT layer." ).arg( fieldName ) );
154 const QMetaType::Type fieldType = layer->fields().at( idx ).type();
155 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
161 method = QObject::tr(
"begins with" );
164 method = QObject::tr(
"contains" );
167 method = QObject::tr(
"does not contain" );
174 throw QgsProcessingException( QObject::tr(
"Operator '%1' can be used only with string fields." ).arg( method ) );
183 expr = u
"%1 = %2"_s.arg( fieldRef, quotedVal );
186 expr = u
"%1 <> %2"_s.arg( fieldRef, quotedVal );
189 expr = u
"%1 > %2"_s.arg( fieldRef, quotedVal );
191 case GreaterThanEqualTo:
192 expr = u
"%1 >= %2"_s.arg( fieldRef, quotedVal );
195 expr = u
"%1 < %2"_s.arg( fieldRef, quotedVal );
197 case LessThanEqualTo:
198 expr = u
"%1 <= %2"_s.arg( fieldRef, quotedVal );
201 expr = u
"%1 LIKE '%2%'"_s.arg( fieldRef, value );
204 expr = u
"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
207 expr = u
"%1 IS NULL"_s.arg( fieldRef );
210 expr = u
"%1 IS NOT NULL"_s.arg( fieldRef );
213 expr = u
"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
218 if ( expression.hasParserError() )
224 expressionContext.
appendScope( layer->createExpressionContextScope() );
226 layer->selectByExpression( expression, method, &expressionContext );
234 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).
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.