QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
60QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
61{
62 return new QgsSaveSelectedFeatures();
63}
64
65bool QgsSaveSelectedFeatures::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
66{
67 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
68 if ( !selectLayer )
69 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
70
71 mSelection = selectLayer->selectedFeatureIds();
72 return true;
73}
74
75QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76{
77 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
78 if ( !selectLayer )
79 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
80
81 QString dest;
82 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
83 if ( !sink )
84 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
85
86
87 const int count = mSelection.count();
88 int current = 0;
89 const double step = count > 0 ? 100.0 / count : 1;
90
91 QgsFeatureIterator it = selectLayer->getFeatures( QgsFeatureRequest().setFilterFids( mSelection ) );
92 QgsFeature feat;
93 while ( it.nextFeature( feat ) )
94 {
95 if ( feedback->isCanceled() )
96 {
97 break;
98 }
99
100 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
101 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
102
103 feedback->setProgress( current++ * step );
104 }
105
106 sink->finalize();
107
108 QVariantMap outputs;
109 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
110 return outputs;
111}
112
@ 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.
This class 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 data sets.
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.