sieve.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. ***************************************************************************
  3. sieve.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. from qgis.PyQt.QtGui import QIcon
  22. from qgis.core import (QgsRasterFileWriter,
  23. QgsProcessingException,
  24. QgsProcessingParameterDefinition,
  25. QgsProcessingParameterRasterLayer,
  26. QgsProcessingParameterNumber,
  27. QgsProcessingParameterBoolean,
  28. QgsProcessingParameterString,
  29. QgsProcessingParameterRasterDestination)
  30. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  31. from processing.tools.system import isWindows
  32. from processing.algs.gdal.GdalUtils import GdalUtils
  33. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  34. class sieve(GdalAlgorithm):
  35. INPUT = 'INPUT'
  36. THRESHOLD = 'THRESHOLD'
  37. EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
  38. NO_MASK = 'NO_MASK'
  39. MASK_LAYER = 'MASK_LAYER'
  40. EXTRA = 'EXTRA'
  41. OUTPUT = 'OUTPUT'
  42. def __init__(self):
  43. super().__init__()
  44. def initAlgorithm(self, config=None):
  45. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  46. self.addParameter(QgsProcessingParameterNumber(self.THRESHOLD,
  47. self.tr('Threshold'),
  48. type=QgsProcessingParameterNumber.Integer,
  49. minValue=0,
  50. defaultValue=10))
  51. self.addParameter(QgsProcessingParameterBoolean(self.EIGHT_CONNECTEDNESS,
  52. self.tr('Use 8-connectedness'),
  53. defaultValue=False))
  54. self.addParameter(QgsProcessingParameterBoolean(self.NO_MASK,
  55. self.tr('Do not use the default validity mask for the input band'),
  56. defaultValue=False))
  57. self.addParameter(QgsProcessingParameterRasterLayer(self.MASK_LAYER,
  58. self.tr('Validity mask'),
  59. optional=True))
  60. extra_param = QgsProcessingParameterString(self.EXTRA,
  61. self.tr('Additional command-line parameters'),
  62. defaultValue=None,
  63. optional=True)
  64. extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  65. self.addParameter(extra_param)
  66. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Sieved')))
  67. def name(self):
  68. return 'sieve'
  69. def displayName(self):
  70. return self.tr('Sieve')
  71. def group(self):
  72. return self.tr('Raster analysis')
  73. def groupId(self):
  74. return 'rasteranalysis'
  75. def icon(self):
  76. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'sieve.png'))
  77. def commandName(self):
  78. return 'gdal_sieve'
  79. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  80. arguments = [
  81. '-st',
  82. str(self.parameterAsInt(parameters, self.THRESHOLD, context)),
  83. ]
  84. if self.parameterAsBoolean(parameters, self.EIGHT_CONNECTEDNESS, context):
  85. arguments.append('-8')
  86. else:
  87. arguments.append('-4')
  88. if self.parameterAsBoolean(parameters, self.NO_MASK, context):
  89. arguments.append('-nomask')
  90. mask = self.parameterAsRasterLayer(parameters, self.MASK_LAYER, context)
  91. if mask:
  92. arguments.append('-mask')
  93. arguments.append(mask.source())
  94. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  95. self.setOutputValue(self.OUTPUT, out)
  96. output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
  97. if not output_format:
  98. raise QgsProcessingException(self.tr('Output format is invalid'))
  99. arguments.append('-of')
  100. arguments.append(output_format)
  101. if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
  102. extra = self.parameterAsString(parameters, self.EXTRA, context)
  103. arguments.append(extra)
  104. raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  105. if raster is None:
  106. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  107. arguments.append(raster.source())
  108. arguments.append(out)
  109. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]