QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmfilter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmfilter.cpp
3 ---------------------
4 begin : April 2018
5 copyright : (C) 2018 by Matthias Kuhn
6 email : matthias@opengis.ch
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
18#include "qgsalgorithmfilter.h"
19#include "qgsapplication.h"
20
22
23QString QgsFilterAlgorithm::name() const
24{
25 return QStringLiteral( "filter" );
26}
27
28QString QgsFilterAlgorithm::displayName() const
29{
30 return QObject::tr( "Feature filter" );
31}
32
33QStringList QgsFilterAlgorithm::tags() const
34{
35 return QObject::tr( "filter,proxy,redirect,route" ).split( ',' );
36}
37
38QString QgsFilterAlgorithm::group() const
39{
40 return QObject::tr( "Modeler tools" );
41}
42
43QString QgsFilterAlgorithm::groupId() const
44{
45 return QStringLiteral( "modelertools" );
46}
47
48Qgis::ProcessingAlgorithmFlags QgsFilterAlgorithm::flags() const
49{
51}
52
53QString QgsFilterAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm filters features from the input layer and redirects them to one or more outputs." );
56}
57
58QString QgsFilterAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Filters features from the input layer and redirects them to one or more outputs." );
61}
62
63QgsFilterAlgorithm *QgsFilterAlgorithm::createInstance() const
64{
65 return new QgsFilterAlgorithm();
66}
67
68QgsFilterAlgorithm::~QgsFilterAlgorithm()
69{
70 qDeleteAll( mOutputs );
71}
72
73void QgsFilterAlgorithm::initAlgorithm( const QVariantMap &configuration )
74{
75 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
76
77 const QVariantList outputs = configuration.value( QStringLiteral( "outputs" ) ).toList();
78 for ( const QVariant &output : outputs )
79 {
80 const QVariantMap outputDef = output.toMap();
81 const QString name = QStringLiteral( "OUTPUT_%1" ).arg( outputDef.value( QStringLiteral( "name" ) ).toString() );
82 QgsProcessingParameterFeatureSink *outputParam = new QgsProcessingParameterFeatureSink( name, outputDef.value( QStringLiteral( "name" ) ).toString() );
85 if ( outputDef.value( QStringLiteral( "isModelOutput" ) ).toBool() )
87 outputParam->setFlags( flags );
88 addParameter( outputParam );
89 mOutputs.append( new Output( name, outputDef.value( QStringLiteral( "expression" ) ).toString() ) );
90 }
91}
92
93
94QVariantMap QgsFilterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
95{
96 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
97 if ( !source )
98 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
99
100 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
101 for ( Output *output : std::as_const( mOutputs ) )
102 {
103 output->sink.reset( parameterAsSink( parameters, output->name, context, output->destinationIdentifier, source->fields(), source->wkbType(), source->sourceCrs() ) );
104 if ( !output->sink )
105 throw QgsProcessingException( invalidSinkError( parameters, output->name ) );
106 output->expression.prepare( &expressionContext );
107 }
108
109 long count = source->featureCount();
110
111 QgsFeature f;
113
114 double step = count > 0 ? 100.0 / count : 1;
115 int current = 0;
116
117 while ( it.nextFeature( f ) )
118 {
119 if ( feedback->isCanceled() )
120 {
121 break;
122 }
123
124 expressionContext.setFeature( f );
125
126 for ( Output *output : std::as_const( mOutputs ) )
127 {
128 if ( output->expression.evaluate( &expressionContext ).toBool() )
129 {
130 if ( !output->sink->addFeature( f, QgsFeatureSink::FastInsert ) )
131 throw QgsProcessingException( writeFeatureError( output->sink.get(), parameters, output->name ) );
132 }
133 }
134
135 feedback->setProgress( current * step );
136 current++;
137 }
138
139 QVariantMap outputs;
140 for ( const Output *output : std::as_const( mOutputs ) )
141 {
142 outputs.insert( output->name, output->destinationIdentifier );
143 }
144 qDeleteAll( mOutputs );
145 mOutputs.clear();
146 return outputs;
147}
148
149
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3476
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3643
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
@ Hidden
Parameter is hidden and should not be shown to users.
@ IsModelOutput
Destination parameter is final output. The parameter name will be used.
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.
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).
@ 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:58
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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.
void setFlags(Qgis::ProcessingParameterFlags flags)
Sets the flags associated with the parameter.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.