rgb2pct.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. ***************************************************************************
  3. rgb2pct.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. QgsProcessingParameterRasterLayer,
  25. QgsProcessingParameterNumber,
  26. QgsProcessingParameterRasterDestination)
  27. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  28. from processing.algs.gdal.GdalUtils import GdalUtils
  29. from processing.tools.system import isWindows
  30. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  31. class rgb2pct(GdalAlgorithm):
  32. INPUT = 'INPUT'
  33. OUTPUT = 'OUTPUT'
  34. NCOLORS = 'NCOLORS'
  35. def __init__(self):
  36. super().__init__()
  37. def initAlgorithm(self, config=None):
  38. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  39. self.addParameter(QgsProcessingParameterNumber(self.NCOLORS,
  40. self.tr('Number of colors'),
  41. type=QgsProcessingParameterNumber.Integer,
  42. minValue=0,
  43. maxValue=255,
  44. defaultValue=2))
  45. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('RGB to PCT')))
  46. def name(self):
  47. return 'rgbtopct'
  48. def displayName(self):
  49. return self.tr('RGB to PCT')
  50. def group(self):
  51. return self.tr('Raster conversion')
  52. def groupId(self):
  53. return 'rasterconversion'
  54. def icon(self):
  55. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', '24-to-8-bits.png'))
  56. def commandName(self):
  57. return 'rgb2pct'
  58. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  59. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  60. self.setOutputValue(self.OUTPUT, out)
  61. raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  62. if raster is None:
  63. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  64. output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
  65. if not output_format:
  66. raise QgsProcessingException(self.tr('Output format is invalid'))
  67. arguments = [
  68. '-n',
  69. str(self.parameterAsInt(parameters, self.NCOLORS, context)),
  70. '-of',
  71. output_format,
  72. raster.source(),
  73. out
  74. ]
  75. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]