@@ -51,21 +51,32 @@ class SelectByAttribute(GeoAlgorithm):
51
51
'<' ,
52
52
'<=' ,
53
53
'begins with' ,
54
- 'contains'
54
+ 'contains' ,
55
+ 'is null' ,
56
+ 'is not null' ,
57
+ 'does not contain'
55
58
]
59
+ STRING_OPERATORS = ['begins with' ,
60
+ 'contains' ,
61
+ 'does not contain' ]
56
62
57
63
def defineCharacteristics (self ):
58
64
self .name , self .i18n_name = self .trAlgorithm ('Select by attribute' )
59
65
self .group , self .i18n_group = self .trAlgorithm ('Vector selection tools' )
66
+ self .tags = self .tr ('select,attribute,value,contains,null,field' )
60
67
61
68
self .i18n_operators = ['=' ,
62
69
'!=' ,
63
70
'>' ,
64
71
'>=' ,
65
72
'<' ,
66
73
'<=' ,
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
+ ]
69
80
70
81
self .addParameter (ParameterVector (self .INPUT ,
71
82
self .tr ('Input Layer' )))
@@ -89,32 +100,29 @@ def processAlgorithm(self, feedback):
89
100
idx = layer .fields ().lookupField (fieldName )
90
101
fieldType = fields [idx ].type ()
91
102
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 ])
94
105
raise GeoAlgorithmExecutionException (
95
106
self .tr ('Operators {0} can be used only with string fields.' ).format (op ))
96
107
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 )
108
120
else :
109
- raise GeoAlgorithmExecutionException (
110
- self .tr ('Unsupported field type "{0}"' ).format (fields [idx ].typeName ()))
121
+ expression_string = '{} {} {}' .format (field_ref , operator , quoted_val )
111
122
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 ())
118
126
119
- layer .selectByIds ( selected )
127
+ layer .selectByExpression ( expression_string )
120
128
self .setOutputValue (self .OUTPUT , fileName )
0 commit comments