-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSelect_vector_features_by_expression.py
More file actions
35 lines (28 loc) · 950 Bytes
/
Select_vector_features_by_expression.py
File metadata and controls
35 lines (28 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
##[3liz - Vector]=group
##Select vector features by expression=name
##Input_vector_layer=vector
##Expression=longstring
##selected_features_count=output number
from qgis.core import *
# Get vector layer
layer = processing.getObject(Input_vector_layer)
# Script state
ok = True
nb = 0
# Build QGIS request with expression
qExp = QgsExpression(Expression)
if not qExp.hasParserError():
qReq = QgsFeatureRequest(qExp)
else:
progress.setText('An error occured while parsing the given expression: %s' % qExp.parserErrorString() )
raise Expection(exp.parserErrorString())
ok = False
# Set selection
if ok:
# Get features corresponding to the expression
features = layer.getFeatures( qReq )
# Set the selection
layer.setSelectedFeatures( [ f.id() for f in features ] )
nb = layer.selectedFeatureCount()
selected_feature_count = nb
progress.setText('<b>%s features have been selected in the vector layer</b>' % nb)