diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 0921ace03d1..18c5b105aa8 100755 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -65,6 +65,7 @@ from .RandomExtractWithinSubsets import RandomExtractWithinSubsets from .RegularPoints import RegularPoints from .SaveSelectedFeatures import SaveSelectedFeatures from .SelectByAttribute import SelectByAttribute +from .SelectByExpression import SelectByExpression from .SimplifyGeometries import SimplifyGeometries from .Smooth import Smooth from .SpatialiteExecuteSQL import SpatialiteExecuteSQL @@ -128,7 +129,6 @@ from .VectorSplit import VectorSplit # from .PointsToPaths import PointsToPaths # from .SetVectorStyle import SetVectorStyle # from .SetRasterStyle import SetRasterStyle -# from .SelectByExpression import SelectByExpression # from .SelectByAttributeSum import SelectByAttributeSum # from .HypsometricCurves import HypsometricCurves # from .SplitWithLines import SplitWithLines @@ -216,7 +216,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider): # RandomPointsPolygonsVariable(), # RandomPointsAlongLines(), PointsToPaths(), # SetVectorStyle(), SetRasterStyle(), - # SelectByExpression(), HypsometricCurves(), + # HypsometricCurves(), # SplitWithLines(), CreateConstantRaster(), # FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(), # OrientedMinimumBoundingBox(), @@ -266,6 +266,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider): RegularPoints(), SaveSelectedFeatures(), SelectByAttribute(), + SelectByExpression(), SimplifyGeometries(), Smooth(), SpatialiteExecuteSQL(), diff --git a/python/plugins/processing/algs/qgis/SelectByExpression.py b/python/plugins/processing/algs/qgis/SelectByExpression.py index 421753eb80e..ac1f833ab2a 100644 --- a/python/plugins/processing/algs/qgis/SelectByExpression.py +++ b/python/plugins/processing/algs/qgis/SelectByExpression.py @@ -24,23 +24,21 @@ __copyright__ = '(C) 2014, Michael Douchin' __revision__ = '$Format:%H$' -from qgis.core import (QgsApplication, - QgsExpression, +from qgis.core import (QgsExpression, QgsVectorLayer, - QgsProcessingUtils) + QgsProcessingParameterVectorLayer, + QgsProcessingParameterExpression, + QgsProcessingParameterEnum, + QgsProcessingOutputVectorLayer) from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException -from processing.core.parameters import ParameterVector -from processing.core.parameters import ParameterSelection -from processing.core.outputs import OutputVector from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm -from processing.core.parameters import ParameterExpression class SelectByExpression(QgisAlgorithm): - LAYERNAME = 'LAYERNAME' + INPUT = 'INPUT' EXPRESSION = 'EXPRESSION' - RESULT = 'RESULT' + OUTPUT = 'OUTPUT' METHOD = 'METHOD' def group(self): @@ -53,13 +51,14 @@ class SelectByExpression(QgisAlgorithm): self.tr('removing from current selection'), self.tr('selecting within current selection')] - self.addParameter(ParameterVector(self.LAYERNAME, - self.tr('Input Layer'))) - self.addParameter(ParameterExpression(self.EXPRESSION, - self.tr("Expression"), parent_layer=self.LAYERNAME)) - self.addParameter(ParameterSelection(self.METHOD, - self.tr('Modify current selection by'), self.methods, 0)) - self.addOutput(OutputVector(self.RESULT, self.tr('Selected (expression)'), True)) + self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer'))) + + self.addParameter(QgsProcessingParameterExpression(self.EXPRESSION, + self.tr('Expression'), parentLayerParameterName=self.INPUT)) + self.addParameter(QgsProcessingParameterEnum(self.METHOD, + self.tr('Modify current selection by'), self.methods, 0)) + + self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)'))) def name(self): return 'selectbyexpression' @@ -68,9 +67,9 @@ class SelectByExpression(QgisAlgorithm): return self.tr('Select by expression') def processAlgorithm(self, parameters, context, feedback): - filename = self.getParameterValue(self.LAYERNAME) - layer = QgsProcessingUtils.mapLayerFromString(filename, context) - method = self.getParameterValue(self.METHOD) + layer = self.parameterAsVectorLayer(parameters, self.INPUT, context) + + method = self.parameterAsEnum(parameters, self.METHOD, context) if method == 0: behavior = QgsVectorLayer.SetSelection @@ -81,10 +80,10 @@ class SelectByExpression(QgisAlgorithm): elif method == 3: behavior = QgsVectorLayer.IntersectSelection - expression = self.getParameterValue(self.EXPRESSION) + expression = self.parameterAsString(parameters, self.EXPRESSION, context) qExp = QgsExpression(expression) if qExp.hasParserError(): raise GeoAlgorithmExecutionException(qExp.parserErrorString()) layer.selectByExpression(expression, behavior) - self.setOutputValue(self.RESULT, filename) + return {self.OUTPUT: parameters[self.INPUT]}