QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgsalgorithmselectbyexpression.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmselectbyexpression.h
3 ---------------------
4 begin : MArch 2026
5 copyright : (C) 2026 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsvectorlayer.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsSelectByExpressionAlgorithm::name() const
29{
30 return u"selectbyexpression"_s;
31}
32
33QString QgsSelectByExpressionAlgorithm::displayName() const
34{
35 return QObject::tr( "Select by expression" );
36}
37
38QStringList QgsSelectByExpressionAlgorithm::tags() const
39{
40 return QObject::tr( "select,filter,expression,field" ).split( ',' );
41}
42
43QString QgsSelectByExpressionAlgorithm::group() const
44{
45 return QObject::tr( "Vector selection" );
46}
47
48QString QgsSelectByExpressionAlgorithm::groupId() const
49{
50 return u"vectorselection"_s;
51}
52
53QString QgsSelectByExpressionAlgorithm::shortDescription() const
54{
55 return QObject::tr( "Selects features from a vector layer based on a QGIS expression." );
56}
57
58QString QgsSelectByExpressionAlgorithm::shortHelpString() const
59{
60 return QObject::tr(
61 "This algorithm creates a selection in a vector layer. The criteria for selecting "
62 "features is based on a QGIS expression.\n\n"
63 "For help with QGIS expression functions, see the inbuilt help for specific "
64 "functions which is available in the expression builder."
65 );
66}
67
68Qgis::ProcessingAlgorithmFlags QgsSelectByExpressionAlgorithm::flags() const
69{
71}
72
73QgsSelectByExpressionAlgorithm *QgsSelectByExpressionAlgorithm::createInstance() const
74{
75 return new QgsSelectByExpressionAlgorithm();
76}
77
78void QgsSelectByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
79{
80 QStringList methods = QStringList()
81 << QObject::tr( "creating new selection" )
82 << QObject::tr( "adding to current selection" )
83 << QObject::tr( "selecting within current selection" )
84 << QObject::tr( "removing from current selection" );
85
86 addParameter( new QgsProcessingParameterVectorLayer( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
87 addParameter( new QgsProcessingParameterExpression( u"EXPRESSION"_s, QObject::tr( "Expression" ), QVariant(), u"INPUT"_s ) );
88 addParameter( new QgsProcessingParameterEnum( u"SELECTION_METHOD"_s, QObject::tr( "Modify current selection by" ), methods, false, 0 ) );
89
90 // backwards compatibility parameters
91 // TODO QGIS 5: remove compatibility parameters and their logic
92 auto methodParam = std::make_unique<QgsProcessingParameterEnum>(
93 u"METHOD"_s,
94 QObject::tr( "Modify current selection by" ),
95 QStringList() << QObject::tr( "creating new selection" ) << QObject::tr( "adding to current selection" ) << QObject::tr( "removing from current selection" ) << QObject::tr( "selecting within current selection" ),
96 false,
97 0
98 );
99 methodParam->setFlags( methodParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
100 addParameter( std::move( methodParam ) );
101
102 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Selected (attribute)" ) ) );
103}
104
105bool QgsSelectByExpressionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
106{
107 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
108 if ( !layer )
109 {
110 throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );
111 }
112
113 const QString expressionString = parameterAsExpression( parameters, u"EXPRESSION"_s, context );
114 QgsExpression expression( expressionString );
115 if ( expression.hasParserError() )
116 {
117 throw QgsProcessingException( expression.parserErrorString() );
118 }
119
120 Qgis::SelectBehavior method = static_cast<Qgis::SelectBehavior>( parameterAsEnum( parameters, u"SELECTION_METHOD"_s, context ) );
121 // handle backwards compatibility parameter
122 if ( parameters.value( u"METHOD"_s ).isValid() )
123 {
124 method = static_cast<Qgis::SelectBehavior>( parameterAsEnum( parameters, u"METHOD"_s, context ) );
126 {
128 }
130 {
132 }
133 }
134
135 mLayerId = layer->id();
136
137 QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
138 expressionContext.appendScope( layer->createExpressionContextScope() );
139
140 layer->selectByExpression( expression, method, &expressionContext );
141
142 return true;
143}
144
145QVariantMap QgsSelectByExpressionAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
146{
147 QVariantMap results;
148 results.insert( u"OUTPUT"_s, mLayerId );
149 return results;
150}
151
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3720
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3791
@ NotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
Definition qgis.h:3777
@ Hidden
Parameter is hidden and should not be shown to users.
Definition qgis.h:3948
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1891
@ IntersectSelection
Modify current selection to include only select features which match.
Definition qgis.h:1894
@ RemoveFromSelection
Remove from current selection.
Definition qgis.h:1895
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").
QString id
Definition qgsmaplayer.h:86
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
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.
An expression 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...