QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
62 "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
63 "If the selected layer has no selected features, the newly created layer will be empty."
64 );
65}
66
67QString QgsSaveSelectedFeatures::shortDescription() const
68{
69 return QObject::tr( "Creates a layer with all the selected features in a given vector layer." );
70}
71
72QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
73{
74 return new QgsSaveSelectedFeatures();
75}
76
77bool QgsSaveSelectedFeatures::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
78{
79 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
80 if ( !selectLayer )
81 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
82
83 mSelection = selectLayer->selectedFeatureIds();
84 return true;
85}
86
87QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
88{
89 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
90 if ( !selectLayer )
91 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
92
93 QString dest;
94 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
95 if ( !sink )
96 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
97
98
99 const int count = mSelection.count();
100 int current = 0;
101 const double step = count > 0 ? 100.0 / count : 1;
102
103 QgsFeatureIterator it = selectLayer->getFeatures( QgsFeatureRequest().setFilterFids( mSelection ) );
104 QgsFeature feat;
105 while ( it.nextFeature( feat ) )
106 {
107 if ( feedback->isCanceled() )
108 {
109 break;
110 }
111
112 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
113 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
114
115 feedback->setProgress( current++ * step );
116 }
117
118 sink->finalize();
119
120 QVariantMap outputs;
121 outputs.insert( u"OUTPUT"_s, dest );
122 return outputs;
123}
124
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
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:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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.