QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const int count = mSelection.count();
89  int current = 0;
90  const 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  if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
102  throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
103 
104  feedback->setProgress( current++ * step );
105  }
106 
107  QVariantMap outputs;
108  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
109  return outputs;
110 }
111 
113 
114 
115 
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:1052
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
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:54
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3621
QgsProcessingParameterVectorLayer
A vector layer (with or without geometry) parameter for processing algorithms. Consider using the mor...
Definition: qgsprocessingparameters.h:2827
qgsvectorlayer.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
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:407
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsProcessingException
Custom exception class for processing related exceptions.
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