|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ExtractByExpression.py |
| 6 | + --------------------- |
| 7 | + Date : September 2017 |
| 8 | + Copyright : (C) 2017 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__ = 'September 2017' |
| 22 | +__copyright__ = '(C) 2017, 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 QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsFeatureRequest |
| 29 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 30 | +from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 31 | +from processing.core.parameters import ParameterVector |
| 32 | +from processing.core.parameters import ParameterString |
| 33 | +from processing.core.outputs import OutputVector |
| 34 | +from processing.tools import dataobjects |
| 35 | + |
| 36 | + |
| 37 | +class ExtractByExpression(GeoAlgorithm): |
| 38 | + |
| 39 | + INPUT = 'INPUT' |
| 40 | + EXPRESSION = 'EXPRESSION' |
| 41 | + OUTPUT = 'OUTPUT' |
| 42 | + |
| 43 | + def defineCharacteristics(self): |
| 44 | + self.name, self.i18n_name = self.trAlgorithm('Extract by expression') |
| 45 | + self.group, self.i18n_group = self.trAlgorithm('Vector selection tools') |
| 46 | + |
| 47 | + self.addParameter(ParameterVector(self.INPUT, |
| 48 | + self.tr('Input Layer'), [ParameterVector.VECTOR_TYPE_ANY])) |
| 49 | + self.addParameter(ParameterString(self.EXPRESSION, |
| 50 | + self.tr("Expression"))) |
| 51 | + self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (expression)'))) |
| 52 | + |
| 53 | + def processAlgorithm(self, progress): |
| 54 | + layer = layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT)) |
| 55 | + |
| 56 | + expression = self.getParameterValue(self.EXPRESSION) |
| 57 | + qExp = QgsExpression(expression) |
| 58 | + if qExp.hasParserError(): |
| 59 | + raise GeoAlgorithmExecutionException(qExp.parserErrorString()) |
| 60 | + |
| 61 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( |
| 62 | + layer.fields(), layer.wkbType(), layer.crs()) |
| 63 | + |
| 64 | + context = QgsExpressionContext() |
| 65 | + context.appendScope(QgsExpressionContextUtils.globalScope()) |
| 66 | + context.appendScope(QgsExpressionContextUtils.projectScope()) |
| 67 | + context.appendScope(QgsExpressionContextUtils.layerScope(layer)) |
| 68 | + |
| 69 | + count = layer.featureCount() |
| 70 | + step = 100.0 / count if count else 1 |
| 71 | + |
| 72 | + request = QgsFeatureRequest(qExp, context) |
| 73 | + |
| 74 | + for current, f in enumerate(layer.getFeatures(request)): |
| 75 | + writer.addFeature(f) |
| 76 | + progress.setPercentage(int(current * step)) |
| 77 | + |
| 78 | + del writer |
0 commit comments