QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
21 
22 QgsProcessingAlgorithm::Flags QgsSaveSelectedFeatures::flags() const
23 {
25 }
26 
27 void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
28 {
29  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
30  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ), QgsProcessing::TypeVectorPoint ) );
31 }
32 
33 QString QgsSaveSelectedFeatures::name() const
34 {
35  return QStringLiteral( "saveselectedfeatures" );
36 }
37 
38 QString QgsSaveSelectedFeatures::displayName() const
39 {
40  return QObject::tr( "Save Selected Features" );
41 }
42 
43 QStringList QgsSaveSelectedFeatures::tags() const
44 {
45  return QObject::tr( "selection,save" ).split( ',' );
46 }
47 
48 QString QgsSaveSelectedFeatures::group() const
49 {
50  return QObject::tr( "Vector general" );
51 }
52 
53 QString QgsSaveSelectedFeatures::groupId() const
54 {
55  return QStringLiteral( "vectorgeneral" );
56 }
57 
58 QString QgsSaveSelectedFeatures::shortHelpString() const
59 {
60  return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
61  "If the selected layer has no selected features, the newly created layer will be empty." );
62 }
63 
64 QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
65 {
66  return new QgsSaveSelectedFeatures();
67 }
68 
69 QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
70 {
71  QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
72  if ( !selectLayer )
73  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
74 
75  QString dest;
76  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
77  if ( !sink )
78  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
79 
80 
81  int count = selectLayer->selectedFeatureCount();
82  int current = 0;
83  double step = count > 0 ? 100.0 / count : 1;
84 
85  QgsFeatureIterator it = selectLayer->getSelectedFeatures();
86  QgsFeature feat;
87  while ( it.nextFeature( feat ) )
88  {
89  if ( feedback->isCanceled() )
90  {
91  break;
92  }
93 
94  sink->addFeature( feat, QgsFeatureSink::FastInsert );
95 
96  feedback->setProgress( current++ * step );
97  }
98 
99  QVariantMap outputs;
100  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
101  return outputs;
102 }
103 
105 
106 
107 
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
A feature sink output for processing algorithms.
QgsFields fields() const override
Returns the list of fields of this layer.
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
QgsWkbTypes::Type wkbType() const override
Returns the WKBType or WKBUnknown in case of error.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
A vector layer (with or without geometry) parameter for processing algorithms.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
Vector point layers.
Definition: qgsprocessing.h:48
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.