Skip to content
Permalink
Browse files

[processing] Use QgsVectorLayer::selectByExpression for select by exp…

…ression alg
  • Loading branch information
nyalldawson committed May 19, 2016
1 parent b951d5a commit 1767d3b0736b671421b02c8dbd0c9cfb6d3cb8eb
Showing with 14 additions and 12 deletions.
  1. +14 −12 python/plugins/processing/algs/qgis/SelectByExpression.py
@@ -25,7 +25,7 @@
__revision__ = '$Format:%H$'

import processing
from qgis.core import QgsExpression, QgsFeatureRequest
from qgis.core import QgsExpression, QgsVectorLayer
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterSelection
@@ -47,7 +47,8 @@ def defineCharacteristics(self):

self.methods = [self.tr('creating new selection'),
self.tr('adding to current selection'),
self.tr('removing from current selection')]
self.tr('removing from current selection'),
self.tr('selecting within current selection')]

self.addParameter(ParameterVector(self.LAYERNAME,
self.tr('Input Layer'), [ParameterVector.VECTOR_TYPE_ANY]))
@@ -63,18 +64,19 @@ def processAlgorithm(self, progress):
oldSelection = set(layer.selectedFeaturesIds())
method = self.getParameterValue(self.METHOD)

if method == 0:
behaviour = QgsVectorLayer.SetSelection
elif method == 1:
behaviour = QgsVectorLayer.AddToSelection
elif method == 2:
behavior = QgsVectorLayer.RemoveFromSelection
elif method == 3:
behaviour = QgsVectorLayer.IntersectSelection

expression = self.getParameterValue(self.EXPRESSION)
qExp = QgsExpression(expression)
if not qExp.hasParserError():
qReq = QgsFeatureRequest(qExp)
else:
if qExp.hasParserError():
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
selected = [f.id() for f in layer.getFeatures(qReq)]

if method == 1:
selected = list(oldSelection.union(selected))
elif method == 2:
selected = list(oldSelection.difference(selected))

layer.setSelectedFeatures(selected)
layer.selectByExpression(expression, behaviour)
self.setOutputValue(self.RESULT, filename)

0 comments on commit 1767d3b

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