|
| 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