QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:664
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3433
QgsProcessingParameterVectorLayer
Definition: qgsprocessingparameters.h:2382
qgsvectorlayer.h
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFeature
Definition: qgsfeature.h:55
qgsalgorithmsaveselectedfeatures.h
QgsVectorLayer::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const FINAL
Returns the coordinate reference system for features in the source.
Definition: qgsvectorlayer.cpp:368
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70