RandomSelection.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. ***************************************************************************
  3. RandomSelection.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. import random
  22. from qgis.PyQt.QtGui import QIcon
  23. from qgis.core import (QgsApplication,
  24. QgsFeatureSink,
  25. QgsProcessingException,
  26. QgsProcessingUtils,
  27. QgsProcessingAlgorithm,
  28. QgsProcessingParameterVectorLayer,
  29. QgsProcessingParameterEnum,
  30. QgsProcessingParameterNumber,
  31. QgsProcessingParameterFeatureSink,
  32. QgsProcessingOutputVectorLayer)
  33. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  34. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  35. class RandomSelection(QgisAlgorithm):
  36. INPUT = 'INPUT'
  37. OUTPUT = 'OUTPUT'
  38. METHOD = 'METHOD'
  39. NUMBER = 'NUMBER'
  40. def icon(self):
  41. return QgsApplication.getThemeIcon("/algorithms/mAlgorithmSelectRandom.svg")
  42. def svgIconPath(self):
  43. return QgsApplication.iconPath("/algorithms/mAlgorithmSelectRandom.svg")
  44. def group(self):
  45. return self.tr('Vector selection')
  46. def groupId(self):
  47. return 'vectorselection'
  48. def flags(self):
  49. return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagNotAvailableInStandaloneTool
  50. def __init__(self):
  51. super().__init__()
  52. def initAlgorithm(self, config=None):
  53. self.methods = [self.tr('Number of selected features'),
  54. self.tr('Percentage of selected features')]
  55. self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
  56. self.tr('Input layer')))
  57. self.addParameter(QgsProcessingParameterEnum(self.METHOD,
  58. self.tr('Method'), self.methods, False, 0))
  59. self.addParameter(QgsProcessingParameterNumber(self.NUMBER,
  60. self.tr('Number/percentage of selected features'), QgsProcessingParameterNumber.Integer,
  61. 10, False, 0.0))
  62. self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (random)')))
  63. def name(self):
  64. return 'randomselection'
  65. def displayName(self):
  66. return self.tr('Random selection')
  67. def processAlgorithm(self, parameters, context, feedback):
  68. layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
  69. method = self.parameterAsEnum(parameters, self.METHOD, context)
  70. ids = layer.allFeatureIds()
  71. value = self.parameterAsInt(parameters, self.NUMBER, context)
  72. if method == 0:
  73. if value > len(ids):
  74. raise QgsProcessingException(
  75. self.tr('Selected number is greater than feature count. '
  76. 'Choose a lower value and try again.'))
  77. else:
  78. if value > 100:
  79. raise QgsProcessingException(
  80. self.tr("Percentage can't be greater than 100. Set a "
  81. "different value and try again."))
  82. value = int(round(value / 100.0, 4) * len(ids))
  83. selran = random.sample(ids, value)
  84. layer.selectByIds(selran)
  85. return {self.OUTPUT: parameters[self.INPUT]}