Skip to content

Commit c9ec4d4

Browse files
committed
[processing] Select by atribute sum algorithm
1 parent a741f82 commit c9ec4d4

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
from SetVectorStyle import SetVectorStyle
124124
from SetRasterStyle import SetRasterStyle
125125
from SelectByExpression import SelectByExpression
126+
from SelectByAttributeSum import SelectByAttributeSum
126127
from HypsometricCurves import HypsometricCurves
127128
from SplitLinesWithLines import SplitLinesWithLines
128129
from processing.algs.qgis.FieldsMapper import FieldsMapper
@@ -172,7 +173,7 @@ def __init__(self):
172173
SetVectorStyle(), SetRasterStyle(),
173174
SelectByExpression(), HypsometricCurves(),
174175
SplitLinesWithLines(), CreateConstantRaster(),
175-
FieldsMapper(),
176+
FieldsMapper(),SelectByAttributeSum()
176177
]
177178

178179
if hasMatplotlib:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SelectByAttributeSum.py
6+
---------------------
7+
Date : April 2015
8+
Copyright : (C) 2015 by Alexander Bruy
9+
Email : alexander dot bruy at gmail 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__ = 'Alexander Bruy'
21+
__date__ = 'April 2015'
22+
__copyright__ = '(C) 2015, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QgsSpatialIndex, QgsFeatureRequest, QgsGeometry
29+
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
32+
from processing.core.parameters import ParameterVector
33+
from processing.core.parameters import ParameterTableField
34+
from processing.core.parameters import ParameterNumber
35+
from processing.core.outputs import OutputVector
36+
from processing.tools import dataobjects, vector
37+
38+
39+
class SelectByAttributeSum(GeoAlgorithm):
40+
INPUT = 'INPUT'
41+
FIELD = 'FIELD'
42+
VALUE = 'VALUE'
43+
OUTPUT = 'OUTPUT'
44+
45+
def defineCharacteristics(self):
46+
self.name = 'Select by attribute sum'
47+
self.group = 'Vector selection tools'
48+
49+
self.addParameter(ParameterVector(self.INPUT,
50+
self.tr('Input Layer'), [ParameterVector.VECTOR_TYPE_ANY]))
51+
self.addParameter(ParameterTableField(self.FIELD,
52+
self.tr('Selection attribute'), self.INPUT, ParameterTableField.DATA_TYPE_NUMBER))
53+
self.addParameter(ParameterNumber(self.VALUE, self.tr('Value')))
54+
55+
self.addOutput(OutputVector(self.OUTPUT, self.tr('Output'), True))
56+
57+
def processAlgorithm(self, progress):
58+
fileName = self.getParameterValue(self.INPUT)
59+
layer = dataobjects.getObjectFromUri(fileName)
60+
fieldName = self.getParameterValue(self.FIELD)
61+
value = self.getParameterValue(self.VALUE)
62+
63+
selected = layer.selectedFeaturesIds()
64+
if len(selected) == 0:
65+
GeoAlgorithmExecutionException(
66+
self.tr('There is no selection in the input layer. '
67+
'Select one feature and try again.'))
68+
69+
ft = layer.selectedFeatures()[0]
70+
geom = QgsGeometry(ft.geometry())
71+
attrSum = ft[fieldName]
72+
73+
idx = QgsSpatialIndex(layer.getFeatures())
74+
req = QgsFeatureRequest()
75+
completed = False
76+
while not completed:
77+
intersected = idx.intersects(geom.boundingBox())
78+
if len(intersected) < 0:
79+
progress.setInfo(self.tr('No adjacent features found.'))
80+
break
81+
82+
for i in intersected:
83+
ft = layer.getFeatures(req.setFilterFid(i)).next()
84+
tmpGeom = QgsGeometry(ft.geometry())
85+
if tmpGeom.touches(geom):
86+
geom = tmpGeom.combine(geom)
87+
selected.append(i)
88+
attrSum += ft[fieldName]
89+
if attrSum >= value:
90+
completed = True
91+
break
92+
93+
layer.setSelectedFeatures(selected)
94+
self.setOutputValue(self.OUTPUT, fileName)

0 commit comments

Comments
 (0)