1# -*- coding: utf-8 -*- 2 3""" 4*************************************************************************** 5 SplitRGBBands.py 6 --------------------- 7 Date : August 2012 8 Copyright : (C) 2012 by Victor Olaya 9 Email : volayaf at gmail dot com 10*************************************************************************** 11* * 12* This program is free software; you can redistribute it and/or modify * 13* it under the terms of the GNU General Public License as published by * 14* the Free Software Foundation; either version 2 of the License, or * 15* (at your option) any later version. * 16* * 17*************************************************************************** 18""" 19 20__author__ = 'Victor Olaya' 21__date__ = 'August 2012' 22__copyright__ = '(C) 2012, Victor Olaya' 23 24import os 25from qgis.core import (QgsProcessingParameterRasterLayer, 26 QgsProcessingParameterRasterDestination) 27from processing.tools.system import getTempFilename 28from . import SagaUtils 29from .SagaAlgorithmBase import SagaAlgorithmBase 30 31pluginPath = os.path.normpath(os.path.join( 32 os.path.split(os.path.dirname(__file__))[0], os.pardir)) 33 34 35class SplitRGBBands(SagaAlgorithmBase): 36 INPUT = 'INPUT' 37 R = 'R' 38 G = 'G' 39 B = 'B' 40 41 def __init__(self): 42 super().__init__() 43 44 def initAlgorithm(self, config=None): 45 self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) 46 47 self.addParameter(QgsProcessingParameterRasterDestination(self.R, self.tr('Output R band layer'))) 48 self.addParameter(QgsProcessingParameterRasterDestination(self.G, self.tr('Output G band layer'))) 49 self.addParameter(QgsProcessingParameterRasterDestination(self.B, self.tr('Output B band layer'))) 50 51 def name(self): 52 return 'splitrgbbands' 53 54 def displayName(self): 55 return self.tr('Split RGB bands') 56 57 def group(self): 58 return self.tr('Raster tools') 59 60 def groupId(self): 61 return 'rastertools' 62 63 def processAlgorithm(self, parameters, context, feedback): 64 # TODO: check correct num of bands 65 inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context) 66 input = inLayer.source() 67 temp = getTempFilename(None).replace('.', '') 68 basename = os.path.basename(temp) 69 validChars = \ 70 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' 71 safeBasename = ''.join(c for c in basename if c in validChars) 72 temp = os.path.join(os.path.dirname(temp), safeBasename) 73 74 r = self.parameterAsOutputLayer(parameters, self.R, context) 75 g = self.parameterAsOutputLayer(parameters, self.G, context) 76 b = self.parameterAsOutputLayer(parameters, self.B, context) 77 78 commands = [] 79 version = SagaUtils.getInstalledVersion(True) 80 trailing = "" 81 lib = "" 82 commands.append('%sio_gdal 0 -GRIDS "%s" -FILES "%s"' % (lib, temp, input) 83 ) 84 commands.append('%sio_gdal 1 -GRIDS "%s_%s1.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, r) 85 ) 86 commands.append('%sio_gdal 1 -GRIDS "%s_%s2.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, g) 87 ) 88 commands.append('%sio_gdal 1 -GRIDS "%s_%s3.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' % (lib, temp, trailing, b) 89 ) 90 91 SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) 92 SagaUtils.executeSaga(feedback) 93 94 return {self.R: r, self.G: g, self.B: b} 95