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