Skip to content
Permalink
Browse files

Restore save selected features algorithm

  • Loading branch information
nyalldawson committed Jun 27, 2017
1 parent 449d8f8 commit 96c5453ae28c00944ad9661cb8537797470b3f89
@@ -63,6 +63,7 @@
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RegularPoints import RegularPoints
from .SaveSelectedFeatures import SaveSelectedFeatures
from .SimplifyGeometries import SimplifyGeometries
from .Smooth import Smooth
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
@@ -110,7 +111,6 @@
# from .StatisticsByCategories import StatisticsByCategories
# from .EquivalentNumField import EquivalentNumField
# from .FieldsCalculator import FieldsCalculator
# from .SaveSelectedFeatures import SaveSelectedFeatures
# from .Explode import Explode
# from .FieldPyculator import FieldsPyculator
# from .JoinAttributes import JoinAttributes
@@ -206,7 +206,7 @@ def getAlgs(self):
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# JoinAttributes(),
# Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
# StatisticsByCategories(), ConcaveHull(),
@@ -265,6 +265,7 @@ def getAlgs(self):
RandomExtract(),
RandomExtractWithinSubsets(),
RegularPoints(),
SaveSelectedFeatures(),
SimplifyGeometries(),
Smooth(),
SpatialiteExecuteSQL(),
@@ -25,30 +25,29 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from qgis.core import (QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector


class SaveSelectedFeatures(QgisAlgorithm):

OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'

def group(self):
return self.tr('Vector general tools')

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))

self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Selection')))
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'
@@ -57,20 +56,20 @@ def displayName(self):
return self.tr('Save selected features')

def processAlgorithm(self, parameters, context, feedback):
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vectorLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

vectorLayer = QgsProcessingUtils.mapLayerFromString(inputFilename, context)

writer = output.getVectorWriter(vectorLayer.fields(), vectorLayer.wkbType(), vectorLayer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
vectorLayer.fields(), vectorLayer.wkbType(), vectorLayer.sourceCrs())

features = vectorLayer.getSelectedFeatures()
count = int(vectorLayer.selectedFeatureCount())
if count == 0:
raise GeoAlgorithmExecutionException(self.tr('There are no selected features in the input layer.'))

total = 100.0 / count if count else 1
for current, feat in enumerate(features):
writer.addFeature(feat, QgsFeatureSink.FastInsert)
if feedback.isCanceled():
break

sink.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
del writer

return {self.OUTPUT: dest_id}

0 comments on commit 96c5453

Please sign in to comment.
You can’t perform that action at this time.