|
| 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 ExtractByAttribute(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 = 'Extract 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 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, |
| 81 | + layer.wkbType(), layer.crs()) |
| 82 | + |
| 83 | + idx = layer.fieldNameIndex(fieldName) |
| 84 | + fieldType = fields[idx].type() |
| 85 | + |
| 86 | + if fieldType != QVariant.String and operator in self.OPERATORS[-2:]: |
| 87 | + op = ''.join(['"%s", ' % o for o in self.OPERATORS[-2:]]) |
| 88 | + raise GeoAlgorithmExecutionException( |
| 89 | + 'Operators %s can be used only with string fields.' % op) |
| 90 | + |
| 91 | + if fieldType in [QVariant.Int, QVariant.Double]: |
| 92 | + progress.setInfo('Numeric field') |
| 93 | + expr = '"%s" %s %s' % (fieldName, operator, value) |
| 94 | + progress.setInfo(expr) |
| 95 | + elif fieldType == QVariant.String: |
| 96 | + progress.setInfo('String field') |
| 97 | + if operator not in self.OPERATORS[-2:]: |
| 98 | + expr = """"%s" %s '%s'""" % (fieldName, operator, value) |
| 99 | + elif operator == 'begins with': |
| 100 | + expr = """"%s" LIKE '%s%%'""" % (fieldName, value) |
| 101 | + elif operator == 'contains': |
| 102 | + expr = """"%s" LIKE '%%%s%%'""" % (fieldName, value) |
| 103 | + progress.setInfo(expr) |
| 104 | + elif fieldType in [QVariant.Date, QVariant.DateTime]: |
| 105 | + progress.setInfo('Date field') |
| 106 | + expr = """"%s" %s '%s'""" % (fieldX, operator, value) |
| 107 | + progress.setInfo(expr) |
| 108 | + else: |
| 109 | + raise GeoAlgorithmExecutionException( |
| 110 | + 'Unsupported field type "%s"' % fields[idx].typeName()) |
| 111 | + |
| 112 | + expression = QgsExpression(expr) |
| 113 | + expression.prepare(fields) |
| 114 | + |
| 115 | + features = vector.features(layer) |
| 116 | + |
| 117 | + count = len(features) |
| 118 | + total = 100.0 / float(count) |
| 119 | + for count, f in enumerate(features): |
| 120 | + if expression.evaluate(f, fields): |
| 121 | + writer.addFeature(f) |
| 122 | + progress.setPercentage(int(count * total)) |
| 123 | + |
| 124 | + del writer |
0 commit comments