mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-16 00:03:12 -04:00
[processing][needs-docs] add selection methods to Select by attribute
algorithm (fix #18682)
This commit is contained in:
parent
c27ce39205
commit
580509fd5e
@ -27,6 +27,7 @@ __revision__ = '$Format:%H$'
|
|||||||
|
|
||||||
from qgis.PyQt.QtCore import QVariant
|
from qgis.PyQt.QtCore import QVariant
|
||||||
from qgis.core import (QgsExpression,
|
from qgis.core import (QgsExpression,
|
||||||
|
QgsVectorLayer,
|
||||||
QgsProcessing,
|
QgsProcessing,
|
||||||
QgsProcessingException,
|
QgsProcessingException,
|
||||||
QgsProcessingAlgorithm,
|
QgsProcessingAlgorithm,
|
||||||
@ -43,6 +44,7 @@ class SelectByAttribute(QgisAlgorithm):
|
|||||||
FIELD = 'FIELD'
|
FIELD = 'FIELD'
|
||||||
OPERATOR = 'OPERATOR'
|
OPERATOR = 'OPERATOR'
|
||||||
VALUE = 'VALUE'
|
VALUE = 'VALUE'
|
||||||
|
METHOD = 'METHOD'
|
||||||
OUTPUT = 'OUTPUT'
|
OUTPUT = 'OUTPUT'
|
||||||
|
|
||||||
OPERATORS = ['=',
|
OPERATORS = ['=',
|
||||||
@ -77,26 +79,38 @@ class SelectByAttribute(QgisAlgorithm):
|
|||||||
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
||||||
|
|
||||||
def initAlgorithm(self, config=None):
|
def initAlgorithm(self, config=None):
|
||||||
self.i18n_operators = ['=',
|
self.operators = ['=',
|
||||||
'!=',
|
'!=',
|
||||||
'>',
|
'>',
|
||||||
'>=',
|
'>=',
|
||||||
'<',
|
'<',
|
||||||
'<=',
|
'<=',
|
||||||
self.tr('begins with'),
|
self.tr('begins with'),
|
||||||
self.tr('contains'),
|
self.tr('contains'),
|
||||||
self.tr('is null'),
|
self.tr('is null'),
|
||||||
self.tr('is not null'),
|
self.tr('is not null'),
|
||||||
self.tr('does not contain')
|
self.tr('does not contain')
|
||||||
]
|
]
|
||||||
|
|
||||||
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer'), types=[QgsProcessing.TypeVector]))
|
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')]
|
||||||
|
|
||||||
|
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
|
||||||
|
self.tr('Input layer'),
|
||||||
|
types=[QgsProcessing.TypeVector]))
|
||||||
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
||||||
self.tr('Selection attribute'), parentLayerParameterName=self.INPUT))
|
self.tr('Selection attribute'),
|
||||||
|
parentLayerParameterName=self.INPUT))
|
||||||
self.addParameter(QgsProcessingParameterEnum(self.OPERATOR,
|
self.addParameter(QgsProcessingParameterEnum(self.OPERATOR,
|
||||||
self.tr('Operator'), self.i18n_operators))
|
self.tr('Operator'), self.operators))
|
||||||
self.addParameter(QgsProcessingParameterString(self.VALUE, self.tr('Value')))
|
self.addParameter(QgsProcessingParameterString(self.VALUE,
|
||||||
|
self.tr('Value')))
|
||||||
|
self.addParameter(QgsProcessingParameterEnum(self.METHOD,
|
||||||
|
self.tr('Modify current selection by'),
|
||||||
|
self.methods,
|
||||||
|
0))
|
||||||
|
|
||||||
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)')))
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)')))
|
||||||
|
|
||||||
@ -130,17 +144,28 @@ class SelectByAttribute(QgisAlgorithm):
|
|||||||
elif operator == 'is not null':
|
elif operator == 'is not null':
|
||||||
expression_string = '{} IS NOT NULL'.format(field_ref)
|
expression_string = '{} IS NOT NULL'.format(field_ref)
|
||||||
elif operator == 'begins with':
|
elif operator == 'begins with':
|
||||||
expression_string = """%s LIKE '%s%%'""" % (field_ref, value)
|
expression_string = "{} LIKE '{}%'".format(field_ref, value)
|
||||||
elif operator == 'contains':
|
elif operator == 'contains':
|
||||||
expression_string = """%s LIKE '%%%s%%'""" % (field_ref, value)
|
expression_string = "{} LIKE '%{}%'".format(field_ref, value)
|
||||||
elif operator == 'does not contain':
|
elif operator == 'does not contain':
|
||||||
expression_string = """%s NOT LIKE '%%%s%%'""" % (field_ref, value)
|
expression_string = "{} NOT LIKE '%{}%'".format(field_ref, value)
|
||||||
else:
|
else:
|
||||||
expression_string = '{} {} {}'.format(field_ref, operator, quoted_val)
|
expression_string = '{} {} {}'.format(field_ref, operator, quoted_val)
|
||||||
|
|
||||||
|
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
||||||
|
if method == 0:
|
||||||
|
behavior = QgsVectorLayer.SetSelection
|
||||||
|
elif method == 1:
|
||||||
|
behavior = QgsVectorLayer.AddToSelection
|
||||||
|
elif method == 2:
|
||||||
|
behavior = QgsVectorLayer.RemoveFromSelection
|
||||||
|
elif method == 3:
|
||||||
|
behavior = QgsVectorLayer.IntersectSelection
|
||||||
|
|
||||||
expression = QgsExpression(expression_string)
|
expression = QgsExpression(expression_string)
|
||||||
if expression.hasParserError():
|
if expression.hasParserError():
|
||||||
raise QgsProcessingException(expression.parserErrorString())
|
raise QgsProcessingException(expression.parserErrorString())
|
||||||
|
|
||||||
layer.selectByExpression(expression_string)
|
layer.selectByExpression(expression_string, behavior)
|
||||||
|
|
||||||
return {self.OUTPUT: parameters[self.INPUT]}
|
return {self.OUTPUT: parameters[self.INPUT]}
|
||||||
|
@ -80,7 +80,6 @@ class SelectByExpression(QgisAlgorithm):
|
|||||||
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
|
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
|
||||||
|
|
||||||
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
||||||
|
|
||||||
if method == 0:
|
if method == 0:
|
||||||
behavior = QgsVectorLayer.SetSelection
|
behavior = QgsVectorLayer.SetSelection
|
||||||
elif method == 1:
|
elif method == 1:
|
||||||
@ -96,4 +95,5 @@ class SelectByExpression(QgisAlgorithm):
|
|||||||
raise QgsProcessingException(qExp.parserErrorString())
|
raise QgsProcessingException(qExp.parserErrorString())
|
||||||
|
|
||||||
layer.selectByExpression(expression, behavior)
|
layer.selectByExpression(expression, behavior)
|
||||||
|
|
||||||
return {self.OUTPUT: parameters[self.INPUT]}
|
return {self.OUTPUT: parameters[self.INPUT]}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user