2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
pct2rgb.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2017-08-31 15:16:44 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
2018-05-06 10:29:01 +10:00
|
|
|
QgsProcessingException,
|
2017-08-31 15:16:44 +03:00
|
|
|
QgsProcessingParameterRasterLayer,
|
2017-08-31 12:10:45 +03:00
|
|
|
QgsProcessingParameterBand,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterRasterDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import isWindows
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-08-31 12:10:45 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class pct2rgb(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
2017-08-31 12:10:45 +03:00
|
|
|
BAND = 'BAND'
|
|
|
|
RGBA = 'RGBA'
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-27 17:20:57 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-08-31 12:10:45 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
|
2017-09-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterBand(self.BAND,
|
|
|
|
self.tr('Band number'),
|
2018-12-30 09:18:23 +02:00
|
|
|
1,
|
2017-09-06 16:59:48 +03:00
|
|
|
parentLayerParameterName=self.INPUT))
|
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.RGBA,
|
|
|
|
self.tr('Generate a RGBA file'),
|
|
|
|
defaultValue=False))
|
2017-08-31 12:10:45 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('PCT to RGB')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'pcttorgb'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('PCT to RGB')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster conversion')
|
|
|
|
|
2017-12-14 12:05:40 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rasterconversion'
|
|
|
|
|
2017-08-31 12:10:45 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', '8-to-24-bits.png'))
|
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
def commandName(self):
|
|
|
|
return 'pct2rgb'
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments = []
|
2017-08-31 12:10:45 +03:00
|
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
2018-05-06 10:29:01 +10:00
|
|
|
if inLayer is None:
|
|
|
|
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
|
|
|
|
2017-08-31 12:10:45 +03:00
|
|
|
arguments.append(inLayer.source())
|
|
|
|
|
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2019-02-05 12:48:43 +10:00
|
|
|
self.setOutputValue(self.OUTPUT, out)
|
2017-08-31 12:10:45 +03:00
|
|
|
arguments.append(out)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-of')
|
2017-08-31 15:16:44 +03:00
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
2017-08-31 12:10:45 +03:00
|
|
|
|
|
|
|
arguments.append('-b')
|
|
|
|
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
|
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.RGBA, context):
|
2017-08-31 12:10:45 +03:00
|
|
|
arguments.append('-rgba')
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2019-02-10 21:06:56 +01:00
|
|
|
commands = ["python3", "-m", self.commandName()]
|
|
|
|
else:
|
|
|
|
commands = [self.commandName() + '.py']
|
|
|
|
|
|
|
|
commands.append(GdalUtils.escapeAndJoin(arguments))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|