nearblack.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. ***************************************************************************
  3. nearblack.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. QgsProcessingParameterBoolean,
  27. QgsProcessingParameterNumber,
  28. QgsProcessingParameterString,
  29. QgsProcessingParameterRasterDestination)
  30. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  31. from processing.algs.gdal.GdalUtils import GdalUtils
  32. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  33. class nearblack(GdalAlgorithm):
  34. INPUT = 'INPUT'
  35. NEAR = 'NEAR'
  36. WHITE = 'WHITE'
  37. OPTIONS = 'OPTIONS'
  38. EXTRA = 'EXTRA'
  39. OUTPUT = 'OUTPUT'
  40. def __init__(self):
  41. super().__init__()
  42. def initAlgorithm(self, config=None):
  43. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  44. self.addParameter(QgsProcessingParameterNumber(self.NEAR,
  45. self.tr('How far from black (white)'),
  46. type=QgsProcessingParameterNumber.Integer,
  47. minValue=0,
  48. defaultValue=15))
  49. self.addParameter(QgsProcessingParameterBoolean(self.WHITE,
  50. self.tr('Search for nearly white pixels instead of nearly black'),
  51. defaultValue=False))
  52. options_param = QgsProcessingParameterString(self.OPTIONS,
  53. self.tr('Additional creation options'),
  54. defaultValue='',
  55. optional=True)
  56. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  57. options_param.setMetadata({
  58. 'widget_wrapper': {
  59. 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
  60. self.addParameter(options_param)
  61. extra_param = QgsProcessingParameterString(self.EXTRA,
  62. self.tr('Additional command-line parameters'),
  63. defaultValue=None,
  64. optional=True)
  65. extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  66. self.addParameter(extra_param)
  67. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Nearblack')))
  68. def name(self):
  69. return 'nearblack'
  70. def displayName(self):
  71. return self.tr('Near black')
  72. def group(self):
  73. return self.tr('Raster analysis')
  74. def groupId(self):
  75. return 'rasteranalysis'
  76. def icon(self):
  77. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'nearblack.png'))
  78. def commandName(self):
  79. return 'nearblack'
  80. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  81. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  82. if inLayer is None:
  83. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  84. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  85. self.setOutputValue(self.OUTPUT, out)
  86. output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
  87. if not output_format:
  88. raise QgsProcessingException(self.tr('Output format is invalid'))
  89. arguments = [
  90. inLayer.source(),
  91. '-of',
  92. output_format,
  93. '-o',
  94. out,
  95. '-near',
  96. str(self.parameterAsInt(parameters, self.NEAR, context))
  97. ]
  98. if self.parameterAsBoolean(parameters, self.WHITE, context):
  99. arguments.append('-white')
  100. options = self.parameterAsString(parameters, self.OPTIONS, context)
  101. if options:
  102. arguments.extend(GdalUtils.parseCreationOptions(options))
  103. if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
  104. extra = self.parameterAsString(parameters, self.EXTRA, context)
  105. arguments.append(extra)
  106. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]