QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 void 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 
30 QString QgsSaveSelectedFeatures::name() const
31 {
32  return QStringLiteral( "saveselectedfeatures" );
33 }
34 
35 QString QgsSaveSelectedFeatures::displayName() const
36 {
37  return QObject::tr( "Extract selected features" );
38 }
39 
40 QStringList QgsSaveSelectedFeatures::tags() const
41 {
42  return QObject::tr( "selection,save,by" ).split( ',' );
43 }
44 
45 QString QgsSaveSelectedFeatures::group() const
46 {
47  return QObject::tr( "Vector general" );
48 }
49 
50 QString QgsSaveSelectedFeatures::groupId() const
51 {
52  return QStringLiteral( "vectorgeneral" );
53 }
54 
55 QString 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 
61 QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
62 {
63  return new QgsSaveSelectedFeatures();
64 }
65 
66 bool 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 
76 QVariantMap 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  int count = mSelection.count();
89  int current = 0;
90  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  sink->addFeature( feat, QgsFeatureSink::FastInsert );
102 
103  feedback->setProgress( current++ * step );
104  }
105 
106  QVariantMap outputs;
107  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
108  return outputs;
109 }
110 
112 
113 
114 
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
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.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
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
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
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.
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.