Skip to content

Commit 3b60bd6

Browse files
committedOct 3, 2014
refactor Select by attribute alg
1 parent 5d114a8 commit 3b60bd6

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
 

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
from mmqgisx.DeleteDuplicateGeometries import DeleteDuplicateGeometries
7878
from mmqgisx.TextToFloat import TextToFloat
7979
from mmqgisx.ExtractByAttribute import ExtractByAttribute
80+
from mmqgisx.SelectByAttribute import SelectByAttribute
8081

8182
from ConcaveHull import ConcaveHull
8283
from Polygonize import Polygonize
@@ -143,6 +144,7 @@ def __init__(self):
143144
# ------ mmqgisx ------
144145
DeleteColumn(), DeleteDuplicateGeometries(),
145146
TextToFloat(), ExtractByAttribute(),
147+
SelectByAttribute(),
146148
#mmqgisx_delete_duplicate_geometries_algorithm(),
147149
#mmqgisx_geometry_convert_algorithm(),
148150
#mmqgisx_grid_algorithm(),
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
DeleteColumn.py
6+
---------------------
7+
Date : May 2010
8+
Copyright : (C) 2010 by Michael Minn
9+
Email : pyqgis at michaelminn dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Michael Minn'
21+
__date__ = 'May 2010'
22+
__copyright__ = '(C) 2010, Michael Minn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.GeoAlgorithmExecutionException import \
32+
GeoAlgorithmExecutionException
33+
from processing.core.parameters import ParameterVector
34+
from processing.core.parameters import ParameterTableField
35+
from processing.core.parameters import ParameterSelection
36+
from processing.core.parameters import ParameterString
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
41+
class SelectByAttribute(GeoAlgorithm):
42+
INPUT = 'INPUT'
43+
FIELD = 'FIELD'
44+
OPERATOR = 'OPERATOR'
45+
VALUE = 'VALUE'
46+
OUTPUT = 'OUTPUT'
47+
48+
OPERATORS = ['=',
49+
'!=',
50+
'>',
51+
'>=',
52+
'<',
53+
'<=',
54+
'begins with',
55+
'contains'
56+
]
57+
58+
def defineCharacteristics(self):
59+
self.name = 'Select by attribute'
60+
self.group = 'Vector selection tools'
61+
62+
self.addParameter(ParameterVector(
63+
self.INPUT, 'Input Layer', [ParameterVector.VECTOR_TYPE_ANY]))
64+
self.addParameter(ParameterTableField(
65+
self.FIELD, 'Selection attribute', self.INPUT))
66+
self.addParameter(ParameterSelection(
67+
self.OPERATOR, 'Operator', self.OPERATORS))
68+
self.addParameter(ParameterString(self.VALUE, 'Value'))
69+
70+
self.addOutput(OutputVector(self.OUTPUT, 'Output'))
71+
72+
def processAlgorithm(self, progress):
73+
layer = dataobjects.getObjectFromUri(
74+
self.getParameterValue(self.INPUT))
75+
fieldName = self.getParameterValue(self.FIELD)
76+
operator = self.OPERATORS[self.getParameterValue(self.OPERATOR)]
77+
value = self.getParameterValue(self.VALUE)
78+
79+
fields = layer.pendingFields()
80+
81+
idx = layer.fieldNameIndex(fieldName)
82+
fieldType = fields[idx].type()
83+
84+
if fieldType != QVariant.String and operator in self.OPERATORS[-2:]:
85+
op = ''.join(['"%s", ' % o for o in self.OPERATORS[-2:]])
86+
raise GeoAlgorithmExecutionException(
87+
'Operators %s can be used only with string fields.' % op)
88+
89+
if fieldType in [QVariant.Int, QVariant.Double]:
90+
progress.setInfo('Numeric field')
91+
expr = '"%s" %s %s' % (fieldName, operator, value)
92+
progress.setInfo(expr)
93+
elif fieldType == QVariant.String:
94+
progress.setInfo('String field')
95+
if operator not in self.OPERATORS[-2:]:
96+
expr = """"%s" %s '%s'""" % (fieldName, operator, value)
97+
elif operator == 'begins with':
98+
expr = """"%s" LIKE '%s%%'""" % (fieldName, value)
99+
elif operator == 'contains':
100+
expr = """"%s" LIKE '%%%s%%'""" % (fieldName, value)
101+
progress.setInfo(expr)
102+
elif fieldType in [QVariant.Date, QVariant.DateTime]:
103+
progress.setInfo('Date field')
104+
expr = """"%s" %s '%s'""" % (fieldX, operator, value)
105+
progress.setInfo(expr)
106+
else:
107+
raise GeoAlgorithmExecutionException(
108+
'Unsupported field type "%s"' % fields[idx].typeName())
109+
110+
expression = QgsExpression(expr)
111+
expression.prepare(fields)
112+
113+
features = vector.features(layer)
114+
115+
selected = []
116+
count = len(features)
117+
total = 100.0 / float(count)
118+
for count, f in enumerate(features):
119+
if expression.evaluate(f, fields):
120+
selected.append(f.id())
121+
progress.setPercentage(int(count * total))
122+
123+
layer.setSelectedFeatures(selected)
124+
self.setOutputValue(self.OUTPUT, self.getParameterValue(self.INPUT))

0 commit comments

Comments
 (0)
Please sign in to comment.