2014-07-06 20:17:31 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SelectByExpression.py
|
|
|
|
---------------------
|
|
|
|
Date : July 2014
|
2016-02-17 09:36:59 +02:00
|
|
|
Copyright : (C) 2014 by Michael Douchin
|
2014-07-06 20:17:31 +02:00
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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 Douchin'
|
|
|
|
__date__ = 'July 2014'
|
|
|
|
__copyright__ = '(C) 2014, Michael Douchin'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsExpression,
|
2017-05-02 14:36:23 +10:00
|
|
|
QgsVectorLayer,
|
|
|
|
QgsProcessingUtils)
|
2014-07-06 20:17:31 +02:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.qgis import QgisAlgorithm
|
2016-11-03 16:37:00 +10:00
|
|
|
from processing.core.parameters import ParameterExpression
|
2014-07-06 20:17:31 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class SelectByExpression(QgisAlgorithm):
|
2014-07-06 20:17:31 +02:00
|
|
|
|
|
|
|
LAYERNAME = 'LAYERNAME'
|
2015-08-22 14:29:41 +02:00
|
|
|
EXPRESSION = 'EXPRESSION'
|
2014-07-06 20:17:31 +02:00
|
|
|
RESULT = 'RESULT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector selection tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.methods = [self.tr('creating new selection'),
|
|
|
|
self.tr('adding to current selection'),
|
2016-05-19 10:40:18 +10:00
|
|
|
self.tr('removing from current selection'),
|
|
|
|
self.tr('selecting within current selection')]
|
2015-08-31 16:59:11 +02:00
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.LAYERNAME,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input Layer')))
|
2016-11-03 16:37:00 +10:00
|
|
|
self.addParameter(ParameterExpression(self.EXPRESSION,
|
|
|
|
self.tr("Expression"), parent_layer=self.LAYERNAME))
|
2014-07-06 20:17:31 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Modify current selection by'), self.methods, 0))
|
2015-05-22 16:09:55 +02:00
|
|
|
self.addOutput(OutputVector(self.RESULT, self.tr('Selected (expression)'), True))
|
2014-07-06 20:17:31 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'selectbyexpression'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Select by expression')
|
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2014-07-06 20:17:31 +02:00
|
|
|
filename = self.getParameterValue(self.LAYERNAME)
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(filename, context)
|
2014-07-06 20:17:31 +02:00
|
|
|
method = self.getParameterValue(self.METHOD)
|
|
|
|
|
2016-05-19 10:40:18 +10:00
|
|
|
if method == 0:
|
2016-12-19 10:52:29 +01:00
|
|
|
behavior = QgsVectorLayer.SetSelection
|
2016-05-19 10:40:18 +10:00
|
|
|
elif method == 1:
|
2016-12-19 10:52:29 +01:00
|
|
|
behavior = QgsVectorLayer.AddToSelection
|
2016-05-19 10:40:18 +10:00
|
|
|
elif method == 2:
|
|
|
|
behavior = QgsVectorLayer.RemoveFromSelection
|
|
|
|
elif method == 3:
|
2016-12-19 10:52:29 +01:00
|
|
|
behavior = QgsVectorLayer.IntersectSelection
|
2016-05-19 10:40:18 +10:00
|
|
|
|
2014-07-06 20:17:31 +02:00
|
|
|
expression = self.getParameterValue(self.EXPRESSION)
|
|
|
|
qExp = QgsExpression(expression)
|
2016-05-19 10:40:18 +10:00
|
|
|
if qExp.hasParserError():
|
2014-07-06 20:17:31 +02:00
|
|
|
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
|
|
|
|
|
2016-12-19 10:52:29 +01:00
|
|
|
layer.selectByExpression(expression, behavior)
|
2015-01-15 20:41:15 +02:00
|
|
|
self.setOutputValue(self.RESULT, filename)
|