Skip to content

Commit 04ee85f

Browse files
committedSep 15, 2017
[processing] native save selected features algorithm
1 parent 625c205 commit 04ee85f

File tree

5 files changed

+84
-79
lines changed

5 files changed

+84
-79
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,6 @@ qgis:reprojectlayer: >
499499

500500
Attributes are not modified by this algorithm.
501501

502-
qgis:saveselectedfeatures: >
503-
This algorithm creates a new layer with all the selected features in a given vector layer.
504-
505-
If the selected layer has no selected features, all features will be added to the resulting feature.
506-
507502
qgis:selectbyattribute: >
508503
This algorithm creates a selection in a vector layer. The criteria for selected features is defined based on the values of an attribute from the input layer.
509504

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
from .Relief import Relief
130130
from .ReverseLineDirection import ReverseLineDirection
131131
from .Ruggedness import Ruggedness
132-
from .SaveSelectedFeatures import SaveSelectedFeatures
133132
from .SelectByAttribute import SelectByAttribute
134133
from .SelectByExpression import SelectByExpression
135134
from .ServiceAreaFromLayer import ServiceAreaFromLayer
@@ -266,7 +265,6 @@ def getAlgs(self):
266265
Relief(),
267266
ReverseLineDirection(),
268267
Ruggedness(),
269-
SaveSelectedFeatures(),
270268
SelectByAttribute(),
271269
SelectByExpression(),
272270
ServiceAreaFromLayer(),

‎python/plugins/processing/algs/qgis/SaveSelectedFeatures.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,71 @@ void QgsNativeAlgorithms::loadAlgorithms()
8080
addAlgorithm( new QgsExtractByLocationAlgorithm() );
8181
addAlgorithm( new QgsFixGeometriesAlgorithm() );
8282
addAlgorithm( new QgsMergeLinesAlgorithm() );
83+
addAlgorithm( new QgsSaveSelectedFeatures() );
8384
addAlgorithm( new QgsSmoothAlgorithm() );
8485
addAlgorithm( new QgsSimplifyAlgorithm() );
8586
}
8687

88+
void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
89+
{
90+
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
91+
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ), QgsProcessing::TypeVectorPoint ) );
92+
}
93+
94+
QString QgsSaveSelectedFeatures::shortHelpString() const
95+
{
96+
return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
97+
"If the selected layer has no selected features, all features will be added to the resulting feature." );
98+
}
99+
100+
QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
101+
{
102+
return new QgsSaveSelectedFeatures();
103+
}
104+
105+
QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
106+
{
107+
QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
108+
109+
QString dest;
110+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
111+
if ( !sink )
112+
return QVariantMap();
113+
114+
QgsFeatureIterator it;
115+
QgsFeature feat;
116+
117+
int count = selectLayer->selectedFeatureCount();
118+
if ( count > 0 )
119+
{
120+
it = selectLayer->getSelectedFeatures();
121+
}
122+
else
123+
{
124+
count = selectLayer->featureCount();
125+
it = selectLayer->getFeatures();
126+
}
127+
128+
int current = 0;
129+
double step = count > 0 ? 100.0 / count : 1;
130+
131+
while ( it.nextFeature( feat ) )
132+
{
133+
if ( feedback->isCanceled() )
134+
{
135+
break;
136+
}
137+
138+
sink->addFeature( feat, QgsFeatureSink::FastInsert );
139+
140+
feedback->setProgress( current++ * step );
141+
}
142+
143+
QVariantMap outputs;
144+
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
145+
return outputs;
146+
}
147+
87148
void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
88149
{
89150
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ class QgsNativeAlgorithms: public QgsProcessingProvider
4848

4949
};
5050

51+
/**
52+
* Native save selected features algorithm.
53+
*/
54+
class QgsSaveSelectedFeatures : public QgsProcessingAlgorithm
55+
{
56+
57+
public:
58+
59+
QgsSaveSelectedFeatures() = default;
60+
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
61+
QString name() const override { return QStringLiteral( "saveselectedfeatures" ); }
62+
QString displayName() const override { return QObject::tr( "Save Selected Features" ); }
63+
QStringList tags() const override { return QObject::tr( "selection,save" ).split( ',' ); }
64+
QString group() const override { return QObject::tr( "Vector general" ); }
65+
QString shortHelpString() const override;
66+
QgsSaveSelectedFeatures *createInstance() const override SIP_FACTORY;
67+
68+
protected:
69+
70+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
71+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
72+
};
73+
5174
/**
5275
* Native centroid algorithm.
5376
*/

0 commit comments

Comments
 (0)
Please sign in to comment.