Skip to content

Commit

Permalink
Start bringing back saga algs - split RGB bands
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and alexbruy committed Nov 9, 2017
1 parent d00efde commit 571a5ca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 68 deletions.
47 changes: 0 additions & 47 deletions python/plugins/processing/algs/replacer.py

This file was deleted.

54 changes: 54 additions & 0 deletions python/plugins/processing/algs/saga/SagaAlgorithmBase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
SagaAlgorithmBase.py
---------------------
Date : August 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson 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__ = 'Nyall Dawson'
__date__ = 'August 2017'
__copyright__ = '(C) 2017, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsProcessingAlgorithm


pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))


class SagaAlgorithmBase(QgsProcessingAlgorithm):

def __init__(self):
super().__init__()
self.output_values = {}

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'saga.png'))

def createInstance(self, config={}):
return self.__class__()

def tr(self, string, context=''):
if context == '':
context = self.__class__.__name__
return QCoreApplication.translate(context, string)
39 changes: 19 additions & 20 deletions python/plugins/processing/algs/saga/SplitRGBBands.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,32 @@
__revision__ = '$Format:%H$'

import os
from qgis.PyQt.QtGui import QIcon
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.outputs import OutputRaster
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterRasterDestination)
from processing.tools.system import getTempFilename
from . import SagaUtils
from .SagaAlgorithmBase import SagaAlgorithmBase

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))


class SplitRGBBands(GeoAlgorithm):
class SplitRGBBands(SagaAlgorithmBase):

INPUT = 'INPUT'
R = 'R'
G = 'G'
B = 'B'

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'saga.png'))

def __init__(self):
super().__init__()
self.addParameter(ParameterRaster(SplitRGBBands.INPUT,
self.tr('Input layer'), False))
self.addOutput(OutputRaster(SplitRGBBands.R,
self.tr('Output R band layer')))
self.addOutput(OutputRaster(SplitRGBBands.G,
self.tr('Output G band layer')))
self.addOutput(OutputRaster(SplitRGBBands.B,
self.tr('Output B band layer')))

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))

self.addParameter(QgsProcessingParameterRasterDestination(self.R, self.tr('Output R band layer')))
self.addParameter(QgsProcessingParameterRasterDestination(self.G, self.tr('Output G band layer')))
self.addParameter(QgsProcessingParameterRasterDestination(self.B, self.tr('Output B band layer')))

def name(self):
return 'splitrgbbands'
Expand All @@ -69,17 +64,19 @@ def group(self):

def processAlgorithm(self, parameters, context, feedback):
# TODO: check correct num of bands
input = self.getParameterValue(SplitRGBBands.INPUT)
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
input = inLayer.source()
temp = getTempFilename(None).replace('.', '')
basename = os.path.basename(temp)
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeBasename = ''.join(c for c in basename if c in validChars)
temp = os.path.join(os.path.dirname(temp), safeBasename)

r = self.getOutputValue(SplitRGBBands.R)
g = self.getOutputValue(SplitRGBBands.G)
b = self.getOutputValue(SplitRGBBands.B)
r = self.parameterAsOutputLayer(parameters, self.R, context)
g = self.parameterAsOutputLayer(parameters, self.G, context)
b = self.parameterAsOutputLayer(parameters, self.B, context)

commands = []
version = SagaUtils.getInstalledVersion(True)
trailing = ""
Expand All @@ -95,3 +92,5 @@ def processAlgorithm(self, parameters, context, feedback):

SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
SagaUtils.executeSaga(feedback)

return {self.R: r, self.G: g, self.B: b}
2 changes: 1 addition & 1 deletion python/plugins/processing/core/Processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider # NOQA
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider
from processing.algs.gdal.GdalAlgorithmProvider import GdalAlgorithmProvider # NOQA
#from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider # NOQA
from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider # NOQA
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider # NOQA
#from processing.preconfigured.PreconfiguredAlgorithmProvider import PreconfiguredAlgorithmProvider # NOQA

Expand Down

0 comments on commit 571a5ca

Please sign in to comment.