Skip to content

Commit

Permalink
Restore select by expression algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 27, 2017
1 parent f98bcb2 commit ae82985
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -65,6 +65,7 @@
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
Expand Down Expand Up @@ -128,7 +129,6 @@
# 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
Expand Down Expand Up @@ -216,7 +216,7 @@ def getAlgs(self):
# RandomPointsPolygonsVariable(),
# RandomPointsAlongLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# SelectByExpression(), HypsometricCurves(),
# HypsometricCurves(),
# SplitWithLines(), CreateConstantRaster(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
Expand Down Expand Up @@ -266,6 +266,7 @@ def getAlgs(self):
RegularPoints(),
SaveSelectedFeatures(),
SelectByAttribute(),
SelectByExpression(),
SimplifyGeometries(),
Smooth(),
SpatialiteExecuteSQL(),
Expand Down
41 changes: 20 additions & 21 deletions python/plugins/processing/algs/qgis/SelectByExpression.py
Expand Up @@ -24,23 +24,21 @@

__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):
Expand All @@ -53,13 +51,14 @@ def __init__(self):
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'
Expand All @@ -68,9 +67,9 @@ def displayName(self):
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
Expand All @@ -81,10 +80,10 @@ def processAlgorithm(self, parameters, context, feedback):
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]}

0 comments on commit ae82985

Please sign in to comment.