QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
20#include "qgsvectorlayer.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
29{
30 addParameter( new QgsProcessingParameterVectorLayer( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
31 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Selected features" ) ) );
32}
33
34QString QgsSaveSelectedFeatures::name() const
35{
36 return u"saveselectedfeatures"_s;
37}
38
39QString QgsSaveSelectedFeatures::displayName() const
40{
41 return QObject::tr( "Extract selected features" );
42}
43
44QStringList QgsSaveSelectedFeatures::tags() const
45{
46 return QObject::tr( "selection,save,by" ).split( ',' );
47}
48
49QString QgsSaveSelectedFeatures::group() const
50{
51 return QObject::tr( "Vector general" );
52}
53
54QString QgsSaveSelectedFeatures::groupId() const
55{
56 return u"vectorgeneral"_s;
57}
58
59QString QgsSaveSelectedFeatures::shortHelpString() const
60{
61 return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
62 "If the selected layer has no selected features, the newly created layer will be empty." );
63}
64
65QString QgsSaveSelectedFeatures::shortDescription() const
66{
67 return QObject::tr( "Creates a layer with all the selected features in a given vector layer." );
68}
69
70QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
71{
72 return new QgsSaveSelectedFeatures();
73}
74
75bool QgsSaveSelectedFeatures::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
76{
77 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
78 if ( !selectLayer )
79 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
80
81 mSelection = selectLayer->selectedFeatureIds();
82 return true;
83}
84
85QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
86{
87 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
88 if ( !selectLayer )
89 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
90
91 QString dest;
92 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
93 if ( !sink )
94 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
95
96
97 const int count = mSelection.count();
98 int current = 0;
99 const double step = count > 0 ? 100.0 / count : 1;
100
101 QgsFeatureIterator it = selectLayer->getFeatures( QgsFeatureRequest().setFilterFids( mSelection ) );
102 QgsFeature feat;
103 while ( it.nextFeature( feat ) )
104 {
105 if ( feedback->isCanceled() )
106 {
107 break;
108 }
109
110 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
111 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
112
113 feedback->setProgress( current++ * step );
114 }
115
116 sink->finalize();
117
118 QVariantMap outputs;
119 outputs.insert( u"OUTPUT"_s, dest );
120 return outputs;
121}
122
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
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:60
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
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.
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.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
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.