Skip to content

Commit 2a80d28

Browse files
committedMar 20, 2017
[processing] Sync features in Select by Expression with Select by Attribute
Adds new stuff like "is null"/"is not null"/"does not contain" to the Select By Attribute algorithm.
1 parent 954ee20 commit 2a80d28

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed
 

‎python/plugins/processing/algs/qgis/SelectByAttribute.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,32 @@ class SelectByAttribute(GeoAlgorithm):
5151
'<',
5252
'<=',
5353
'begins with',
54-
'contains'
54+
'contains',
55+
'is null',
56+
'is not null',
57+
'does not contain'
5558
]
59+
STRING_OPERATORS = ['begins with',
60+
'contains',
61+
'does not contain']
5662

5763
def defineCharacteristics(self):
5864
self.name, self.i18n_name = self.trAlgorithm('Select by attribute')
5965
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
66+
self.tags = self.tr('select,attribute,value,contains,null,field')
6067

6168
self.i18n_operators = ['=',
6269
'!=',
6370
'>',
6471
'>=',
6572
'<',
6673
'<=',
67-
self.tr('begins with '),
68-
self.tr('contains')]
74+
self.tr('begins with'),
75+
self.tr('contains'),
76+
self.tr('is null'),
77+
self.tr('is not null'),
78+
self.tr('does not contain')
79+
]
6980

7081
self.addParameter(ParameterVector(self.INPUT,
7182
self.tr('Input Layer')))
@@ -89,32 +100,29 @@ def processAlgorithm(self, feedback):
89100
idx = layer.fields().lookupField(fieldName)
90101
fieldType = fields[idx].type()
91102

92-
if fieldType != QVariant.String and operator in self.OPERATORS[-2:]:
93-
op = ''.join(['"%s", ' % o for o in self.OPERATORS[-2:]])
103+
if fieldType != QVariant.String and operator in self.STRING_OPERATORS:
104+
op = ''.join(['"%s", ' % o for o in self.STRING_OPERATORS])
94105
raise GeoAlgorithmExecutionException(
95106
self.tr('Operators {0} can be used only with string fields.').format(op))
96107

97-
if fieldType in [QVariant.Int, QVariant.Double, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong]:
98-
expr = '"%s" %s %s' % (fieldName, operator, value)
99-
elif fieldType == QVariant.String:
100-
if operator not in self.OPERATORS[-2:]:
101-
expr = """"%s" %s '%s'""" % (fieldName, operator, value)
102-
elif operator == 'begins with':
103-
expr = """"%s" LIKE '%s%%'""" % (fieldName, value)
104-
elif operator == 'contains':
105-
expr = """"%s" LIKE '%%%s%%'""" % (fieldName, value)
106-
elif fieldType in [QVariant.Date, QVariant.DateTime]:
107-
expr = """"%s" %s '%s'""" % (fieldName, operator, value)
108+
field_ref = QgsExpression.quotedColumnRef(fieldName)
109+
quoted_val = QgsExpression.quotedValue(value)
110+
if operator == 'is null':
111+
expression_string = '{} IS NULL'.format(field_ref)
112+
elif operator == 'is not null':
113+
expression_string = '{} IS NOT NULL'.format(field_ref)
114+
elif operator == 'begins with':
115+
expression_string = """%s LIKE '%s%%'""" % (field_ref, value)
116+
elif operator == 'contains':
117+
expression_string = """%s LIKE '%%%s%%'""" % (field_ref, value)
118+
elif operator == 'does not contain':
119+
expression_string = """%s NOT LIKE '%%%s%%'""" % (field_ref, value)
108120
else:
109-
raise GeoAlgorithmExecutionException(
110-
self.tr('Unsupported field type "{0}"').format(fields[idx].typeName()))
121+
expression_string = '{} {} {}'.format(field_ref, operator, quoted_val)
111122

112-
qExp = QgsExpression(expr)
113-
if not qExp.hasParserError():
114-
qReq = QgsFeatureRequest(qExp).setSubsetOfAttributes([])
115-
else:
116-
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
117-
selected = [f.id() for f in layer.getFeatures(qReq)]
123+
expression = QgsExpression(expression_string)
124+
if expression.hasParserError():
125+
raise GeoAlgorithmExecutionException(expression.parserErrorString())
118126

119-
layer.selectByIds(selected)
127+
layer.selectByExpression(expression_string)
120128
self.setOutputValue(self.OUTPUT, fileName)

0 commit comments

Comments
 (0)
Please sign in to comment.