2012-10-27 23:06:38 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SplitRGBBands.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-27 23:06:38 +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-27 23:06:38 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
import os
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterRaster
|
|
|
|
from processing.core.outputs import OutputRaster
|
2016-03-15 16:43:52 +01:00
|
|
|
from processing.tools.system import getTempFilename
|
2016-03-21 04:58:12 +01:00
|
|
|
from . import SagaUtils
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.normpath(os.path.join(
|
|
|
|
os.path.split(os.path.dirname(__file__))[0], os.pardir))
|
|
|
|
|
2012-10-27 23:06:38 +02:00
|
|
|
|
|
|
|
class SplitRGBBands(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
R = 'R'
|
|
|
|
G = 'G'
|
|
|
|
B = 'B'
|
2012-10-27 23:06:38 +02:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-03-14 20:26:58 +01:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'saga.png'))
|
2012-10-27 23:06:38 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-16 16:25:12 +02:00
|
|
|
self.addParameter(ParameterRaster(SplitRGBBands.INPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer'), False))
|
2015-01-16 16:25:12 +02:00
|
|
|
self.addOutput(OutputRaster(SplitRGBBands.R,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Output R band layer')))
|
2015-01-16 16:25:12 +02:00
|
|
|
self.addOutput(OutputRaster(SplitRGBBands.G,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Output G band layer')))
|
2015-01-16 16:25:12 +02:00
|
|
|
self.addOutput(OutputRaster(SplitRGBBands.B,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Output B band layer')))
|
2012-10-27 23:06:38 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'splitrgbbands'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Split RGB bands')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Image tools')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2013-10-01 20:52:22 +03:00
|
|
|
# TODO: check correct num of bands
|
2012-12-10 00:12:07 +01:00
|
|
|
input = self.getParameterValue(SplitRGBBands.INPUT)
|
2013-10-01 20:52:22 +03:00
|
|
|
temp = getTempFilename(None).replace('.', '')
|
2013-03-12 20:01:29 +01:00
|
|
|
basename = os.path.basename(temp)
|
2013-10-01 20:52:22 +03:00
|
|
|
validChars = \
|
|
|
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
2013-09-14 14:25:58 +02:00
|
|
|
safeBasename = ''.join(c for c in basename if c in validChars)
|
2013-03-12 20:01:29 +01:00
|
|
|
temp = os.path.join(os.path.dirname(temp), safeBasename)
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2012-10-27 23:06:38 +02:00
|
|
|
r = self.getOutputValue(SplitRGBBands.R)
|
|
|
|
g = self.getOutputValue(SplitRGBBands.G)
|
|
|
|
b = self.getOutputValue(SplitRGBBands.B)
|
|
|
|
commands = []
|
2016-03-21 04:58:12 +01:00
|
|
|
version = SagaUtils.getSagaInstalledVersion(True) # NOQA
|
2015-05-14 18:59:25 +01:00
|
|
|
trailing = ""
|
|
|
|
lib = ""
|
2015-04-28 08:57:57 +02:00
|
|
|
commands.append('%sio_gdal 0 -GRIDS "%s" -FILES "%s"' % (lib, temp, input)
|
2015-05-14 18:59:25 +01:00
|
|
|
)
|
2015-08-22 14:29:41 +02:00
|
|
|
commands.append('%sio_gdal 1 -GRIDS "%s_%s1.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, r)
|
2015-04-28 08:57:57 +02:00
|
|
|
)
|
2015-08-22 14:29:41 +02:00
|
|
|
commands.append('%sio_gdal 1 -GRIDS "%s_%s2.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, g)
|
2015-04-28 08:57:57 +02:00
|
|
|
)
|
2015-08-22 14:29:41 +02:00
|
|
|
commands.append('%sio_gdal 1 -GRIDS "%s_%s3.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, b)
|
2015-04-28 08:57:57 +02:00
|
|
|
)
|
|
|
|
|
2012-12-10 00:12:07 +01:00
|
|
|
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
|
2017-01-06 20:04:00 +10:00
|
|
|
SagaUtils.executeSaga(feedback)
|