-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsalgorithmfilter.cpp
134 lines (108 loc) · 4.31 KB
/
qgsalgorithmfilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/***************************************************************************
qgsalgorithmfilter.cpp
---------------------
begin : April 2018
copyright : (C) 2018 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmfilter.h"
#include "qgsapplication.h"
///@cond PRIVATE
QString QgsFilterAlgorithm::name() const
{
return QStringLiteral( "filter" );
}
QString QgsFilterAlgorithm::displayName() const
{
return QObject::tr( "Filter" );
}
QStringList QgsFilterAlgorithm::tags() const
{
return QObject::tr( "filter,proxy,redirect,route" ).split( ',' );
}
QString QgsFilterAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsFilterAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}
QgsProcessingAlgorithm::Flags QgsFilterAlgorithm::flags() const
{
return FlagHideFromToolbox;
}
QString QgsFilterAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm filters features from the input layer and redirects them to one or several outputs." );
}
QgsFilterAlgorithm *QgsFilterAlgorithm::createInstance() const
{
return new QgsFilterAlgorithm();
}
QgsFilterAlgorithm::~QgsFilterAlgorithm()
{
qDeleteAll( mOutputs );
}
void QgsFilterAlgorithm::initAlgorithm( const QVariantMap &configuration )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
const QVariantList outputs = configuration.value( QStringLiteral( "outputs" ) ).toList();
for ( const QVariant &output : outputs )
{
const QVariantMap outputDef = output.toMap();
const QString name = QStringLiteral( "OUTPUT_%1" ).arg( outputDef.value( QStringLiteral( "name" ) ).toString() );
QgsProcessingParameterFeatureSink *outputParam = new QgsProcessingParameterFeatureSink( name, outputDef.value( QStringLiteral( "name" ) ).toString() );
outputParam->setFlags( QgsProcessingParameterDefinition::FlagHidden );
addParameter( outputParam );
mOutputs.append( new Output( name, outputDef.value( QStringLiteral( "expression" ) ).toString() ) );
}
}
QVariantMap QgsFilterAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
for ( Output *output : qgis::as_const( mOutputs ) )
{
output->sink.reset( parameterAsSink( parameters, output->name, context, output->destinationIdentifier, source->fields(), source->wkbType(), source->sourceCrs() ) );
output->expression.prepare( &expressionContext );
}
long count = source->featureCount();
QgsFeature f;
QgsFeatureIterator it = source->getFeatures();
double step = count > 0 ? 100.0 / count : 1;
int current = 0;
while ( it.nextFeature( f ) )
{
if ( feedback->isCanceled() )
{
break;
}
expressionContext.setFeature( f );
for ( Output *output : qgis::as_const( mOutputs ) )
{
if ( output->expression.evaluate( &expressionContext ).toBool() )
output->sink->addFeature( f, QgsFeatureSink::FastInsert );
}
feedback->setProgress( current * step );
current++;
}
QVariantMap outputs;
for ( const Output *output : qgis::as_const( mOutputs ) )
{
outputs.insert( output->name, output->destinationIdentifier );
}
qDeleteAll( mOutputs );
mOutputs.clear();
return outputs;
}