Skip to content

Commit

Permalink
Restore save selected features algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 27, 2017
1 parent 449d8f8 commit 96c5453
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -206,7 +206,7 @@ def getAlgs(self):
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# JoinAttributes(),
# Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
# StatisticsByCategories(), ConcaveHull(),
Expand Down Expand Up @@ -265,6 +265,7 @@ def getAlgs(self):
RandomExtract(),
RandomExtractWithinSubsets(),
RegularPoints(),
SaveSelectedFeatures(),
SimplifyGeometries(),
Smooth(),
SpatialiteExecuteSQL(),
Expand Down
39 changes: 19 additions & 20 deletions python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.