1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ ***************************************************************************
5
+ SelectByExpression.py
6
+ ---------------------
7
+ Date : July 2014
8
+ Copyright : (C) 2014 by Michaël Douchin
9
+ ***************************************************************************
10
+ * *
11
+ * This program is free software; you can redistribute it and/or modify *
12
+ * it under the terms of the GNU General Public License as published by *
13
+ * the Free Software Foundation; either version 2 of the License, or *
14
+ * (at your option) any later version. *
15
+ * *
16
+ ***************************************************************************
17
+ """
18
+
19
+ __author__ = 'Michael Douchin'
20
+ __date__ = 'July 2014'
21
+ __copyright__ = '(C) 2014, Michael Douchin'
22
+
23
+ # This will get replaced with a git SHA1 when you do a git archive
24
+
25
+ __revision__ = '$Format:%H$'
26
+
27
+ import processing
28
+ from qgis .core import *
29
+ from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
30
+ from processing .parameters .ParameterVector import ParameterVector
31
+ from processing .parameters .ParameterSelection import ParameterSelection
32
+ from processing .outputs .OutputVector import OutputVector
33
+ from processing .core .GeoAlgorithm import GeoAlgorithm
34
+ from processing .parameters .ParameterString import ParameterString
35
+
36
+ class SelectByExpression (GeoAlgorithm ):
37
+
38
+ LAYERNAME = 'LAYERNAME'
39
+ EXPRESSION = 'EXPRESSION'
40
+ RESULT = 'RESULT'
41
+ METHOD = 'METHOD'
42
+ METHODS = ['creating new selection' , 'adding to current selection' ,
43
+ 'removing from current selection' ]
44
+
45
+ def defineCharacteristics (self ):
46
+ self .name = 'Select by expression'
47
+ self .group = 'Vector selection tools'
48
+
49
+ self .addParameter (ParameterVector (self .LAYERNAME , 'Input Layer' ,
50
+ [ParameterVector .VECTOR_TYPE_ANY ]))
51
+ self .addParameter (ParameterString (self .EXPRESSION , "Expression" ))
52
+ self .addParameter (ParameterSelection (self .METHOD ,
53
+ 'Modify current selection by' , self .METHODS , 0 ))
54
+ self .addOutput (OutputVector (self .RESULT , 'Output' , True ))
55
+
56
+ def processAlgorithm (self , progress ):
57
+
58
+ filename = self .getParameterValue (self .LAYERNAME )
59
+ layer = processing .getObject (filename )
60
+ oldSelection = set (layer .selectedFeaturesIds ())
61
+ method = self .getParameterValue (self .METHOD )
62
+
63
+ # Build QGIS request with expression
64
+ expression = self .getParameterValue (self .EXPRESSION )
65
+ qExp = QgsExpression (expression )
66
+ if not qExp .hasParserError ():
67
+ qReq = QgsFeatureRequest (qExp )
68
+ else :
69
+ raise GeoAlgorithmExecutionException (qExp .parserErrorString ())
70
+ selected = [f .id () for f in layer .getFeatures (qReq )]
71
+
72
+ if method == 1 :
73
+ selected = list (oldSelection .union (selected ))
74
+ elif method == 2 :
75
+ selected = list (oldSelection .difference (selected ))
76
+
77
+ # Set the selection
78
+ layer .setSelectedFeatures (selected )
79
+
80
+ self .setOutputValue (self .RESULT , filename )
0 commit comments