mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-28 00:05:04 -04:00
Algorithms and other processing code should use this method (instead of dataobjects.getLayerFromString) to retrieve layers from a string, as it considers the processing context and allows resolving strings to temporarily stored layers. This permits processing models to function correctly when intermediate results are stored as memory layers. Subsequent model algorithms can then access these temporary layers as inputs. All temporary layers will be removed when the context object is destroyed after the model algorithm is run.
144 lines
5.6 KiB
Python
144 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
SelectByAttribute.py
|
|
---------------------
|
|
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$'
|
|
|
|
from qgis.core import (QgsApplication)
|
|
from qgis.PyQt.QtCore import QVariant
|
|
from qgis.core import QgsExpression, QgsFeatureRequest
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
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
|
|
|
|
|
|
class SelectByAttribute(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']
|
|
|
|
def icon(self):
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
def svgIconPath(self):
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
def tags(self):
|
|
return self.tr('select,attribute,value,contains,null,field').split(',')
|
|
|
|
def group(self):
|
|
return self.tr('Vector selection tools')
|
|
|
|
def name(self):
|
|
return 'selectbyattribute'
|
|
|
|
def displayName(self):
|
|
return self.tr('Select by attribute')
|
|
|
|
def defineCharacteristics(self):
|
|
self.i18n_operators = ['=',
|
|
'!=',
|
|
'>',
|
|
'>=',
|
|
'<',
|
|
'<=',
|
|
self.tr('begins with'),
|
|
self.tr('contains'),
|
|
self.tr('is null'),
|
|
self.tr('is not null'),
|
|
self.tr('does not contain')
|
|
]
|
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
self.tr('Input Layer')))
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
|
self.tr('Selection attribute'), self.INPUT))
|
|
self.addParameter(ParameterSelection(self.OPERATOR,
|
|
self.tr('Operator'), self.i18n_operators))
|
|
self.addParameter(ParameterString(self.VALUE, self.tr('Value')))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Selected (attribute)'), True))
|
|
|
|
def processAlgorithm(self, context, feedback):
|
|
fileName = self.getParameterValue(self.INPUT)
|
|
layer = dataobjects.QgsProcessingUtils.mapLayerFromString(fileName, context)
|
|
fieldName = self.getParameterValue(self.FIELD)
|
|
operator = self.OPERATORS[self.getParameterValue(self.OPERATOR)]
|
|
value = self.getParameterValue(self.VALUE)
|
|
|
|
fields = layer.fields()
|
|
|
|
idx = layer.fields().lookupField(fieldName)
|
|
fieldType = fields[idx].type()
|
|
|
|
if fieldType != QVariant.String and operator in self.STRING_OPERATORS:
|
|
op = ''.join(['"%s", ' % o for o in self.STRING_OPERATORS])
|
|
raise GeoAlgorithmExecutionException(
|
|
self.tr('Operators {0} can be used only with string fields.').format(op))
|
|
|
|
field_ref = QgsExpression.quotedColumnRef(fieldName)
|
|
quoted_val = QgsExpression.quotedValue(value)
|
|
if operator == 'is null':
|
|
expression_string = '{} IS NULL'.format(field_ref)
|
|
elif operator == 'is not null':
|
|
expression_string = '{} IS NOT NULL'.format(field_ref)
|
|
elif operator == 'begins with':
|
|
expression_string = """%s LIKE '%s%%'""" % (field_ref, value)
|
|
elif operator == 'contains':
|
|
expression_string = """%s LIKE '%%%s%%'""" % (field_ref, value)
|
|
elif operator == 'does not contain':
|
|
expression_string = """%s NOT LIKE '%%%s%%'""" % (field_ref, value)
|
|
else:
|
|
expression_string = '{} {} {}'.format(field_ref, operator, quoted_val)
|
|
|
|
expression = QgsExpression(expression_string)
|
|
if expression.hasParserError():
|
|
raise GeoAlgorithmExecutionException(expression.parserErrorString())
|
|
|
|
layer.selectByExpression(expression_string)
|
|
self.setOutputValue(self.OUTPUT, fileName)
|