pct2rgb.py 3.9 KB

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