QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmextractbyattribute.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractbyattribute.cpp
3 ----------------------------------
4 begin : April 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson 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 <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsExtractByAttributeAlgorithm::name() const
27{
28 return u"extractbyattribute"_s;
29}
30
31QString QgsExtractByAttributeAlgorithm::displayName() const
32{
33 return QObject::tr( "Extract by attribute" );
34}
35
36QStringList QgsExtractByAttributeAlgorithm::tags() const
37{
38 return QObject::tr( "extract,filter,attribute,value,contains,null,field" ).split( ',' );
39}
40
41QString QgsExtractByAttributeAlgorithm::group() const
42{
43 return QObject::tr( "Vector selection" );
44}
45
46QString QgsExtractByAttributeAlgorithm::groupId() const
47{
48 return u"vectorselection"_s;
49}
50
51void QgsExtractByAttributeAlgorithm::initAlgorithm( const QVariantMap & )
52{
53 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
54 addParameter( new QgsProcessingParameterField( u"FIELD"_s, QObject::tr( "Selection attribute" ), QVariant(), u"INPUT"_s ) );
55 addParameter( new QgsProcessingParameterEnum(
56 u"OPERATOR"_s,
57 QObject::tr( "Operator" ),
58 QStringList()
59 << QObject::tr( "=" )
60 << QObject::tr( "≠" )
61 << QObject::tr( ">" )
62 << QObject::tr( "≥" )
63 << QObject::tr( "<" )
64 << QObject::tr( "≤" )
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" ),
70 false,
71 0
72 ) );
73 addParameter( new QgsProcessingParameterString( u"VALUE"_s, QObject::tr( "Value" ), QVariant(), false, true ) );
74
75 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Extracted (attribute)" ) ) );
77 = new QgsProcessingParameterFeatureSink( u"FAIL_OUTPUT"_s, QObject::tr( "Extracted (non-matching)" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true );
78 failOutput->setCreateByDefault( false );
79 addParameter( failOutput );
80}
81
82QString QgsExtractByAttributeAlgorithm::shortHelpString() const
83{
84 return QObject::tr(
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."
88 );
89}
90
91QString QgsExtractByAttributeAlgorithm::shortDescription() const
92{
93 return QObject::tr( "Creates a vector layer that only contains features matching an attribute value from an input layer." );
94}
95
96QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance() const
97{
98 return new QgsExtractByAttributeAlgorithm();
99}
100
101QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
104 if ( !source )
105 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
106
107 const QString fieldName = parameterAsString( parameters, u"FIELD"_s, context );
108 const Operation op = static_cast<Operation>( parameterAsEnum( parameters, u"OPERATOR"_s, context ) );
109 const QString value = parameterAsString( parameters, u"VALUE"_s, context );
110
111 QString matchingSinkId;
112 std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, u"OUTPUT"_s, context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
113 if ( !matchingSink )
114 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
115
116 QString nonMatchingSinkId;
117 std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, u"FAIL_OUTPUT"_s, context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
118
119 const int idx = source->fields().lookupField( fieldName );
120 if ( idx < 0 )
121 throw QgsProcessingException( QObject::tr( "Field '%1' was not found in INPUT source" ).arg( fieldName ) );
122
123 const QMetaType::Type fieldType = source->fields().at( idx ).type();
124
125 if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
126 {
127 QString method;
128 switch ( op )
129 {
130 case BeginsWith:
131 method = QObject::tr( "begins with" );
132 break;
133 case Contains:
134 method = QObject::tr( "contains" );
135 break;
136 case DoesNotContain:
137 method = QObject::tr( "does not contain" );
138 break;
139
140 default:
141 break;
142 }
143
144 throw QgsProcessingException( QObject::tr( "Operator '%1' can be used only with string fields." ).arg( method ) );
145 }
146
147 const QString fieldRef = QgsExpression::quotedColumnRef( fieldName );
148 const QString quotedVal = QgsExpression::quotedValue( value );
149 QString expr;
150 switch ( op )
151 {
152 case Equals:
153 expr = u"%1 = %3"_s.arg( fieldRef, quotedVal );
154 break;
155 case NotEquals:
156 expr = u"%1 != %3"_s.arg( fieldRef, quotedVal );
157 break;
158 case GreaterThan:
159 expr = u"%1 > %3"_s.arg( fieldRef, quotedVal );
160 break;
161 case GreaterThanEqualTo:
162 expr = u"%1 >= %3"_s.arg( fieldRef, quotedVal );
163 break;
164 case LessThan:
165 expr = u"%1 < %3"_s.arg( fieldRef, quotedVal );
166 break;
167 case LessThanEqualTo:
168 expr = u"%1 <= %3"_s.arg( fieldRef, quotedVal );
169 break;
170 case BeginsWith:
171 expr = u"%1 LIKE '%2%'"_s.arg( fieldRef, value );
172 break;
173 case Contains:
174 expr = u"%1 LIKE '%%2%'"_s.arg( fieldRef, value );
175 break;
176 case IsNull:
177 expr = u"%1 IS NULL"_s.arg( fieldRef );
178 break;
179 case IsNotNull:
180 expr = u"%1 IS NOT NULL"_s.arg( fieldRef );
181 break;
182 case DoesNotContain:
183 expr = u"%1 NOT LIKE '%%2%'"_s.arg( fieldRef, value );
184 break;
185 }
186
187 QgsExpression expression( expr );
188 if ( expression.hasParserError() )
189 {
190 throw QgsProcessingException( expression.parserErrorString() );
191 }
192
193 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
194
195 const long count = source->featureCount();
196
197 const double step = count > 0 ? 100.0 / count : 1;
198 int current = 0;
199
200 if ( !nonMatchingSink )
201 {
202 // not saving failing features - so only fetch good features
204 req.setFilterExpression( expr );
205 req.setExpressionContext( expressionContext );
206
208 QgsFeature f;
209 while ( it.nextFeature( f ) )
210 {
211 if ( feedback->isCanceled() )
212 {
213 break;
214 }
215
216 if ( !matchingSink->addFeature( f, QgsFeatureSink::FastInsert ) )
217 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, u"OUTPUT"_s ) );
218
219 feedback->setProgress( current * step );
220 current++;
221 }
222 }
223 else
224 {
225 // saving non-matching features, so we need EVERYTHING
226 expressionContext.setFields( source->fields() );
227 expression.prepare( &expressionContext );
228
230 QgsFeature f;
231 while ( it.nextFeature( f ) )
232 {
233 if ( feedback->isCanceled() )
234 {
235 break;
236 }
237
238 expressionContext.setFeature( f );
239 if ( expression.evaluate( &expressionContext ).toBool() )
240 {
241 if ( !matchingSink->addFeature( f, QgsFeatureSink::FastInsert ) )
242 throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, u"OUTPUT"_s ) );
243 }
244 else
245 {
246 if ( !nonMatchingSink->addFeature( f, QgsFeatureSink::FastInsert ) )
247 throw QgsProcessingException( writeFeatureError( nonMatchingSink.get(), parameters, u"FAIL_OUTPUT"_s ) );
248 }
249
250 feedback->setProgress( current * step );
251 current++;
252 }
253 }
254
255 if ( matchingSink )
256 matchingSink->finalize();
257 if ( nonMatchingSink )
258 nonMatchingSink->finalize();
259
260 QVariantMap outputs;
261 outputs.insert( u"OUTPUT"_s, matchingSinkId );
262 if ( nonMatchingSink )
263 outputs.insert( u"FAIL_OUTPUT"_s, nonMatchingSinkId );
264 return outputs;
265}
266
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3647
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
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...
Definition qgsfeature.h:60
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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.
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.