QGIS/python/plugins/processing/algs/qgis/SelectByExpression.py

102 lines
3.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
***************************************************************************
SelectByExpression.py
---------------------
Date : July 2014
Copyright : (C) 2014 by Michael Douchin
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Michael Douchin'
__date__ = 'July 2014'
__copyright__ = '(C) 2014, Michael Douchin'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2017-06-27 13:02:20 +10:00
from qgis.core import (QgsExpression,
QgsVectorLayer,
2017-06-27 13:02:20 +10:00
QgsProcessingParameterVectorLayer,
QgsProcessingParameterExpression,
QgsProcessingParameterEnum,
QgsProcessingOutputVectorLayer)
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
class SelectByExpression(QgisAlgorithm):
2017-06-27 13:02:20 +10:00
INPUT = 'INPUT'
EXPRESSION = 'EXPRESSION'
2017-06-27 13:02:20 +10:00
OUTPUT = 'OUTPUT'
METHOD = 'METHOD'
def group(self):
return self.tr('Vector selection tools')
def __init__(self):
super().__init__()
self.methods = [self.tr('creating new selection'),
self.tr('adding to current selection'),
self.tr('removing from current selection'),
self.tr('selecting within current selection')]
2017-06-27 13:02:20 +10:00
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)')))
self.layer = None
self.behavior = None
self.input = None
self.expression = None
def name(self):
return 'selectbyexpression'
def displayName(self):
return self.tr('Select by expression')
def prepareAlgorithm(self, parameters, context, feedback):
self.layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
2017-06-27 13:02:20 +10:00
method = self.parameterAsEnum(parameters, self.METHOD, context)
if method == 0:
self.behavior = QgsVectorLayer.SetSelection
elif method == 1:
self.behavior = QgsVectorLayer.AddToSelection
elif method == 2:
self.behavior = QgsVectorLayer.RemoveFromSelection
elif method == 3:
self.behavior = QgsVectorLayer.IntersectSelection
self.expression = self.parameterAsString(parameters, self.EXPRESSION, context)
qExp = QgsExpression(self.expression)
if qExp.hasParserError():
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
self.input = parameters[self.INPUT]
return True
def processAlgorithm(self, context, feedback):
self.layer.selectByExpression(self.expression, self.behavior)
return True
def postProcessAlgorithm(self, context, feedback):
return {self.OUTPUT: self.input}