SelectByExpression.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. ***************************************************************************
  3. SelectByExpression.py
  4. ---------------------
  5. Date : July 2014
  6. Copyright : (C) 2014 by Michael Douchin
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************
  15. """
  16. __author__ = 'Michael Douchin'
  17. __date__ = 'July 2014'
  18. __copyright__ = '(C) 2014, Michael Douchin'
  19. from qgis.core import (QgsExpression,
  20. QgsProcessing,
  21. QgsVectorLayer,
  22. QgsProcessingAlgorithm,
  23. QgsProcessingException,
  24. QgsProcessingParameterVectorLayer,
  25. QgsProcessingParameterExpression,
  26. QgsProcessingParameterEnum,
  27. QgsProcessingOutputVectorLayer)
  28. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  29. class SelectByExpression(QgisAlgorithm):
  30. INPUT = 'INPUT'
  31. EXPRESSION = 'EXPRESSION'
  32. OUTPUT = 'OUTPUT'
  33. METHOD = 'METHOD'
  34. def group(self):
  35. return self.tr('Vector selection')
  36. def groupId(self):
  37. return 'vectorselection'
  38. def __init__(self):
  39. super().__init__()
  40. def flags(self):
  41. return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagNotAvailableInStandaloneTool
  42. def initAlgorithm(self, config=None):
  43. self.methods = [self.tr('creating new selection'),
  44. self.tr('adding to current selection'),
  45. self.tr('removing from current selection'),
  46. self.tr('selecting within current selection')]
  47. self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer'), types=[QgsProcessing.TypeVector]))
  48. self.addParameter(QgsProcessingParameterExpression(self.EXPRESSION,
  49. self.tr('Expression'), parentLayerParameterName=self.INPUT))
  50. self.addParameter(QgsProcessingParameterEnum(self.METHOD,
  51. self.tr('Modify current selection by'), self.methods, defaultValue=0))
  52. self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)')))
  53. def name(self):
  54. return 'selectbyexpression'
  55. def displayName(self):
  56. return self.tr('Select by expression')
  57. def processAlgorithm(self, parameters, context, feedback):
  58. layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
  59. method = self.parameterAsEnum(parameters, self.METHOD, context)
  60. if method == 0:
  61. behavior = QgsVectorLayer.SetSelection
  62. elif method == 1:
  63. behavior = QgsVectorLayer.AddToSelection
  64. elif method == 2:
  65. behavior = QgsVectorLayer.RemoveFromSelection
  66. elif method == 3:
  67. behavior = QgsVectorLayer.IntersectSelection
  68. expression = self.parameterAsString(parameters, self.EXPRESSION, context)
  69. qExp = QgsExpression(expression)
  70. if qExp.hasParserError():
  71. raise QgsProcessingException(qExp.parserErrorString())
  72. layer.selectByExpression(expression, behavior)
  73. return {self.OUTPUT: parameters[self.INPUT]}