QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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" ),
26 QList< int >() << QgsProcessing::TypeVector ) );
27 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ) ) );
28}
29
30QString QgsSaveSelectedFeatures::name() const
31{
32 return QStringLiteral( "saveselectedfeatures" );
33}
34
35QString QgsSaveSelectedFeatures::displayName() const
36{
37 return QObject::tr( "Extract selected features" );
38}
39
40QStringList QgsSaveSelectedFeatures::tags() const
41{
42 return QObject::tr( "selection,save,by" ).split( ',' );
43}
44
45QString QgsSaveSelectedFeatures::group() const
46{
47 return QObject::tr( "Vector general" );
48}
49
50QString QgsSaveSelectedFeatures::groupId() const
51{
52 return QStringLiteral( "vectorgeneral" );
53}
54
55QString QgsSaveSelectedFeatures::shortHelpString() const
56{
57 return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
58 "If the selected layer has no selected features, the newly created layer will be empty." );
59}
60
61QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
62{
63 return new QgsSaveSelectedFeatures();
64}
65
66bool QgsSaveSelectedFeatures::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
67{
68 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
69 if ( !selectLayer )
70 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
71
72 mSelection = selectLayer->selectedFeatureIds();
73 return true;
74}
75
76QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
79 if ( !selectLayer )
80 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
81
82 QString dest;
83 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
84 if ( !sink )
85 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
86
87
88 const int count = mSelection.count();
89 int current = 0;
90 const double step = count > 0 ? 100.0 / count : 1;
91
92 QgsFeatureIterator it = selectLayer->getFeatures( QgsFeatureRequest().setFilterFids( mSelection ) );
93 QgsFeature feat;
94 while ( it.nextFeature( feat ) )
95 {
96 if ( feedback->isCanceled() )
97 {
98 break;
99 }
100
101 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
102 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
103
104 feedback->setProgress( current++ * step );
105 }
106
107 QVariantMap outputs;
108 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
109 return outputs;
110}
111
113
114
115
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
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:56
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
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.