QGIS API Documentation  3.6.0-Noosa (5873452)
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 
23 QgsProcessingAlgorithm::Flags QgsSaveSelectedFeatures::flags() const
24 {
26 }
27 
28 void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
29 {
30  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ),
31  QList< int >() << QgsProcessing::TypeVector ) );
32  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ), QgsProcessing::TypeVectorPoint ) );
33 }
34 
35 QString QgsSaveSelectedFeatures::name() const
36 {
37  return QStringLiteral( "saveselectedfeatures" );
38 }
39 
40 QString QgsSaveSelectedFeatures::displayName() const
41 {
42  return QObject::tr( "Extract selected features" );
43 }
44 
45 QStringList QgsSaveSelectedFeatures::tags() const
46 {
47  return QObject::tr( "selection,save,by" ).split( ',' );
48 }
49 
50 QString QgsSaveSelectedFeatures::group() const
51 {
52  return QObject::tr( "Vector general" );
53 }
54 
55 QString QgsSaveSelectedFeatures::groupId() const
56 {
57  return QStringLiteral( "vectorgeneral" );
58 }
59 
60 QString QgsSaveSelectedFeatures::shortHelpString() const
61 {
62  return QObject::tr( "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 QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
67 {
68  return new QgsSaveSelectedFeatures();
69 }
70 
71 QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72 {
73  QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
74  if ( !selectLayer )
75  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
76 
77  QString dest;
78  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
79  if ( !sink )
80  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
81 
82 
83  int count = selectLayer->selectedFeatureCount();
84  int current = 0;
85  double step = count > 0 ? 100.0 / count : 1;
86 
87  QgsFeatureIterator it = selectLayer->getSelectedFeatures();
88  QgsFeature feat;
89  while ( it.nextFeature( feat ) )
90  {
91  if ( feedback->isCanceled() )
92  {
93  break;
94  }
95 
96  sink->addFeature( feat, QgsFeatureSink::FastInsert );
97 
98  feedback->setProgress( current++ * step );
99  }
100 
101  QVariantMap outputs;
102  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
103  return outputs;
104 }
105 
107 
108 
109 
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.
QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
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:55
A feature sink output for processing algorithms.
QgsFields fields() const FINAL
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.
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
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsCoordinateReferenceSystem sourceCrs() const FINAL
Returns the coordinate reference system for features in the source.
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.