QGIS/python/plugins/processing/algs/qgis/ExtractByAttribute.py

134 lines
5.4 KiB
Python
Raw Normal View History

2014-10-02 19:08:01 +03:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
ExtractByAttribute.py
2014-10-02 19:08:01 +03:00
---------------------
Date : May 2010
Copyright : (C) 2010 by Michael Minn
Email : pyqgis at michaelminn dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Michael Minn'
__date__ = 'May 2010'
__copyright__ = '(C) 2010, Michael Minn'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsExpression, QgsFeatureRequest
2014-10-02 19:08:01 +03:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
2014-10-02 19:08:01 +03:00
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterString
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
2014-10-02 19:08:01 +03:00
class ExtractByAttribute(GeoAlgorithm):
INPUT = 'INPUT'
FIELD = 'FIELD'
OPERATOR = 'OPERATOR'
VALUE = 'VALUE'
OUTPUT = 'OUTPUT'
OPERATORS = ['=',
'!=',
'>',
'>=',
'<',
'<=',
'begins with',
'contains',
'is null',
'is not null',
'does not contain'
]
STRING_OPERATORS = ['begins with',
'contains',
'does not contain']
2014-10-02 19:08:01 +03:00
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Extract by attribute')
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
self.tags = self.tr('extract,filter,attribute,value,contains,null,field')
2014-10-02 19:08:01 +03:00
self.i18n_operators = ['=',
'!=',
'>',
'>=',
'<',
'<=',
self.tr('begins with'),
self.tr('contains'),
self.tr('is null'),
self.tr('is not null'),
self.tr('does not contain')
]
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input Layer')))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Selection attribute'), self.INPUT))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterSelection(self.OPERATOR,
self.tr('Operator'), self.i18n_operators))
self.addParameter(ParameterString(self.VALUE, self.tr('Value'), optional=True))
2014-10-02 19:08:01 +03:00
self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (attribute)')))
2014-10-02 19:08:01 +03:00
def processAlgorithm(self, feedback):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
2014-10-02 19:08:01 +03:00
fieldName = self.getParameterValue(self.FIELD)
operator = self.OPERATORS[self.getParameterValue(self.OPERATOR)]
value = self.getParameterValue(self.VALUE)
fields = layer.fields()
2014-10-02 19:08:01 +03:00
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
layer.wkbType(), layer.crs())
2014-10-02 19:08:01 +03:00
2016-09-22 12:09:13 +02:00
idx = layer.fields().lookupField(fieldName)
2014-10-02 19:08:01 +03:00
fieldType = fields[idx].type()
if fieldType != QVariant.String and operator in self.STRING_OPERATORS:
op = ''.join(['"%s", ' % o for o in self.STRING_OPERATORS])
2014-10-02 19:08:01 +03:00
raise GeoAlgorithmExecutionException(
2017-03-04 16:23:36 +01:00
self.tr('Operators {0} can be used only with string fields.').format(op))
2014-10-02 19:08:01 +03:00
field_ref = QgsExpression.quotedColumnRef(fieldName)
quoted_val = QgsExpression.quotedValue(value)
if operator == 'is null':
expr = '{} IS NULL'.format(field_ref)
elif operator == 'is not null':
expr = '{} IS NOT NULL'.format(field_ref)
elif operator == 'begins with':
expr = """%s LIKE '%s%%'""" % (field_ref, value)
elif operator == 'contains':
expr = """%s LIKE '%%%s%%'""" % (field_ref, value)
elif operator == 'does not contain':
expr = """%s NOT LIKE '%%%s%%'""" % (field_ref, value)
2014-10-02 19:08:01 +03:00
else:
expr = '{} {} {}'.format(field_ref, operator, quoted_val)
2014-10-02 19:08:01 +03:00
expression = QgsExpression(expr)
if not expression.hasParserError():
req = QgsFeatureRequest(expression)
else:
raise GeoAlgorithmExecutionException(expression.parserErrorString())
2014-10-02 19:08:01 +03:00
for f in layer.getFeatures(req):
writer.addFeature(f)
2014-10-02 19:08:01 +03:00
del writer