QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 : [email protected]
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 
23 QString QgsFilterAlgorithm::name() const
24 {
25  return QStringLiteral( "filter" );
26 }
27 
28 QString QgsFilterAlgorithm::displayName() const
29 {
30  return QObject::tr( "Feature filter" );
31 }
32 
33 QStringList QgsFilterAlgorithm::tags() const
34 {
35  return QObject::tr( "filter,proxy,redirect,route" ).split( ',' );
36 }
37 
38 QString QgsFilterAlgorithm::group() const
39 {
40  return QObject::tr( "Modeler tools" );
41 }
42 
43 QString QgsFilterAlgorithm::groupId() const
44 {
45  return QStringLiteral( "modelertools" );
46 }
47 
48 QgsProcessingAlgorithm::Flags QgsFilterAlgorithm::flags() const
49 {
50  return FlagHideFromToolbox;
51 }
52 
53 QString QgsFilterAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "This algorithm filters features from the input layer and redirects them to one or several outputs." );
56 }
57 
58 QgsFilterAlgorithm *QgsFilterAlgorithm::createInstance() const
59 {
60  return new QgsFilterAlgorithm();
61 }
62 
63 QgsFilterAlgorithm::~QgsFilterAlgorithm()
64 {
65  qDeleteAll( mOutputs );
66 }
67 
68 void QgsFilterAlgorithm::initAlgorithm( const QVariantMap &configuration )
69 {
70  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
71 
72  const QVariantList outputs = configuration.value( QStringLiteral( "outputs" ) ).toList();
73  for ( const QVariant &output : outputs )
74  {
75  const QVariantMap outputDef = output.toMap();
76  const QString name = QStringLiteral( "OUTPUT_%1" ).arg( outputDef.value( QStringLiteral( "name" ) ).toString() );
77  QgsProcessingParameterFeatureSink *outputParam = new QgsProcessingParameterFeatureSink( name, outputDef.value( QStringLiteral( "name" ) ).toString() );
78  QgsProcessingParameterDefinition::Flags flags = QgsProcessingParameterDefinition::Flags();
80  if ( outputDef.value( QStringLiteral( "isModelOutput" ) ).toBool() )
82  outputParam->setFlags( flags );
83  addParameter( outputParam );
84  mOutputs.append( new Output( name, outputDef.value( QStringLiteral( "expression" ) ).toString() ) );
85  }
86 }
87 
88 
89 QVariantMap QgsFilterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
90 {
91  std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
92  if ( !source )
93  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
94 
95  QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
96  for ( Output *output : std::as_const( mOutputs ) )
97  {
98  output->sink.reset( parameterAsSink( parameters, output->name, context, output->destinationIdentifier, source->fields(), source->wkbType(), source->sourceCrs() ) );
99  if ( !output->sink )
100  throw QgsProcessingException( invalidSinkError( parameters, output->name ) );
101  output->expression.prepare( &expressionContext );
102  }
103 
104  long count = source->featureCount();
105 
106  QgsFeature f;
108 
109  double step = count > 0 ? 100.0 / count : 1;
110  int current = 0;
111 
112  while ( it.nextFeature( f ) )
113  {
114  if ( feedback->isCanceled() )
115  {
116  break;
117  }
118 
119  expressionContext.setFeature( f );
120 
121  for ( Output *output : std::as_const( mOutputs ) )
122  {
123  if ( output->expression.evaluate( &expressionContext ).toBool() )
124  {
125  if ( !output->sink->addFeature( f, QgsFeatureSink::FastInsert ) )
126  throw QgsProcessingException( writeFeatureError( output->sink.get(), parameters, output->name ) );
127  }
128  }
129 
130  feedback->setProgress( current * step );
131  current++;
132  }
133 
134  QVariantMap outputs;
135  for ( const Output *output : std::as_const( mOutputs ) )
136  {
137  outputs.insert( output->name, output->destinationIdentifier );
138  }
139  qDeleteAll( mOutputs );
140  mOutputs.clear();
141  return outputs;
142 }
143 
144 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsProcessingParameterFeatureSource
An input feature source (such as vector layers) parameter for processing algorithms.
Definition: qgsprocessingparameters.h:3057
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
qgsapplication.h
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterDefinition::FlagIsModelOutput
@ FlagIsModelOutput
Destination parameter is final output. The parameter name will be used.
Definition: qgsprocessingparameters.h:454
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsProcessingParameterDefinition::FlagHidden
@ FlagHidden
Parameter is hidden and should not be shown to users.
Definition: qgsprocessingparameters.h:452
qgsalgorithmfilter.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525
QgsProcessingParameterDefinition::setFlags
void setFlags(Flags flags)
Sets the flags associated with the parameter.
Definition: qgsprocessingparameters.h:600