2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
rgb2pct.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
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02: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-30 17:53:14 +10:00
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterRasterDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2017-09-06 16:59:48 +03:00
|
|
|
from processing.tools.system import isWindows
|
2012-09-15 18:25:25 +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 rgb2pct(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
NCOLORS = 'NCOLORS'
|
2012-09-15 18:25:25 +03: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-09-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.NCOLORS,
|
|
|
|
self.tr('Number of colors'),
|
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
minValue=0,
|
|
|
|
maxValue=255,
|
|
|
|
defaultValue=2))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('RGB to PCT')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'rgbtopct'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('RGB to PCT')
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster conversion')
|
|
|
|
|
2017-12-14 12:05:40 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rasterconversion'
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', '24-to-8-bits.png'))
|
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
def commandName(self):
|
|
|
|
return 'rgb2pct'
|
|
|
|
|
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 = []
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-n')
|
2017-09-01 09:24:46 +03:00
|
|
|
arguments.append(str(self.parameterAsInt(parameters, self.NCOLORS, context)))
|
2017-09-06 16:59:48 +03:00
|
|
|
|
2017-08-30 17:53:14 +10:00
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2017-09-06 16:59:48 +03:00
|
|
|
arguments.append('-of')
|
2017-08-31 15:16:44 +03:00
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
2018-05-06 10:29:01 +10:00
|
|
|
raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
|
|
|
if raster is None:
|
|
|
|
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
|
|
|
|
|
|
|
arguments.append(raster.source())
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments.append(out)
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2018-05-14 11:32:55 +07:00
|
|
|
commands = ['cmd.exe', '/C ', self.commandName() + '.bat',
|
2013-10-01 20:52:22 +03:00
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2018-05-14 11:32:55 +07:00
|
|
|
commands = [self.commandName() + '.py', GdalUtils.escapeAndJoin(arguments)]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|