QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmsaveselectedfeatures.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsaveselectedfeatures.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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
19#include "qgsvectorlayer.h"
20
22
23void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
24{
25 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
26 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ) ) );
27}
28
29QString QgsSaveSelectedFeatures::name() const
30{
31 return QStringLiteral( "saveselectedfeatures" );
32}
33
34QString QgsSaveSelectedFeatures::displayName() const
35{
36 return QObject::tr( "Extract selected features" );
37}
38
39QStringList QgsSaveSelectedFeatures::tags() const
40{
41 return QObject::tr( "selection,save,by" ).split( ',' );
42}
43
44QString QgsSaveSelectedFeatures::group() const
45{
46 return QObject::tr( "Vector general" );
47}
48
49QString QgsSaveSelectedFeatures::groupId() const
50{
51 return QStringLiteral( "vectorgeneral" );
52}
53
54QString QgsSaveSelectedFeatures::shortHelpString() const
55{
56 return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
57 "If the selected layer has no selected features, the newly created layer will be empty." );
58}
59
60QString QgsSaveSelectedFeatures::shortDescription() const
61{
62 return QObject::tr( "Creates a layer with all the selected features in a given vector layer." );
63}
64
65QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
66{
67 return new QgsSaveSelectedFeatures();
68}
69
70bool QgsSaveSelectedFeatures::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
71{
72 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
73 if ( !selectLayer )
74 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
75
76 mSelection = selectLayer->selectedFeatureIds();
77 return true;
78}
79
80QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
81{
82 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
83 if ( !selectLayer )
84 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
85
86 QString dest;
87 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
88 if ( !sink )
89 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
90
91
92 const int count = mSelection.count();
93 int current = 0;
94 const double step = count > 0 ? 100.0 / count : 1;
95
96 QgsFeatureIterator it = selectLayer->getFeatures( QgsFeatureRequest().setFilterFids( mSelection ) );
97 QgsFeature feat;
98 while ( it.nextFeature( feat ) )
99 {
100 if ( feedback->isCanceled() )
101 {
102 break;
103 }
104
105 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
106 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
107
108 feedback->setProgress( current++ * step );
109 }
110
111 sink->finalize();
112
113 QVariantMap outputs;
114 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
115 return outputs;
116}
117
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
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.
A feature sink output for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsCoordinateReferenceSystem sourceCrs() const FINAL
Returns the coordinate reference system for features in the source.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.