QGIS/python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
Nyall Dawson 8a84e134cc Algorithms don't have to be split to prepare/process/postProcess
Since it's safe to evaluate parameters in background threads
now, it's usually going to be ok to evaluate everything in
the processAlgorithm step.

This keeps the algorithm code as simple as possible, and will
make porting faster.

Note that the prepare/postProcess virtual methods still exist
and can be used when an algorithm MUST do setup/cleanup work
in the main thread.
2017-07-07 09:28:19 +10:00

74 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
SaveSelectedFeatures.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from qgis.core import (QgsFeatureSink,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
class SaveSelectedFeatures(QgisAlgorithm):
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
def group(self):
return self.tr('Vector general tools')
def __init__(self):
super().__init__()
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Selection')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Selection")))
def name(self):
return 'saveselectedfeatures'
def displayName(self):
return self.tr('Save selected features')
def processAlgorithm(self, parameters, context, feedback):
vectorLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
vectorLayer.fields(), vectorLayer.wkbType(), vectorLayer.sourceCrs())
features = vectorLayer.getSelectedFeatures()
count = int(vectorLayer.selectedFeatureCount())
total = 100.0 / count if count else 1
for current, feat in enumerate(features):
if feedback.isCanceled():
break
sink.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
return {self.OUTPUT: dest_id}