Skip to content
Permalink
Browse files

[processing][SAGA] Fix output type for RGB Composite should be tif, n…

…ot sdat format

(cherry picked from commit 71b32f0)
  • Loading branch information
nyalldawson committed Dec 4, 2018
1 parent 3f0bc70 commit 1440ddd54ec7d00528cea7e5b8b9488790980f20
@@ -49,10 +49,10 @@
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.parameters import getParameterFromString
from processing.algs.help import shortHelp
from processing.tools.system import getTempFilename
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
from processing.algs.saga.SagaParameters import Parameters
from . import SagaUtils
from .SagaAlgorithmBase import SagaAlgorithmBase

@@ -143,8 +143,8 @@ def defineCharacteristicsFromFile(self):
while line != '':
if line.startswith('Hardcoded'):
self.hardcoded_strings.append(line[len('Hardcoded|'):])
elif line.startswith('QgsProcessingParameter') or line.startswith('Parameter'):
self.params.append(getParameterFromString(line))
elif Parameters.is_parameter_line(line):
self.params.append(Parameters.create_parameter_from_line(line))
elif line.startswith('AllowUnmatching'):
self.allow_nonmatching_grid_extents = True
else:
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
SagaParameters.py
---------------------
Date : December 2018
Copyright : (C) 2018 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__ = 'December 2018'
__copyright__ = '(C) 2018, Nyall Dawson'

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

__revision__ = '$Format:%H$'

import os
import importlib
from qgis.core import QgsProcessingParameterRasterDestination
from processing.core.parameters import getParameterFromString


class SagaImageOutputParam(QgsProcessingParameterRasterDestination):

"""
Custom raster destination parameter for SAGA algorithms which create a raster image
output, instead of SAGA's usual 'sdat' raster grid outputs.
These outputs differ from the usual SAGA outputs and are always generated as TIF files instead
of sdat.
"""

def defaultFileExtension(self):
return 'tif'

def supportedOutputRasterLayerExtensions(self):
return ['tif']


class Parameters:

@staticmethod
def is_parameter_line(line):
"""
Returns true if the given line corresponds to a SAGA parameter definition
"""
return line.startswith('SagaImageOutput') or line.startswith('QgsProcessingParameter') or line.startswith('Parameter') or line.startswith('*QgsProcessingParameter')

@staticmethod
def create_parameter_from_line(line):
"""
Creates a parameter from a definition line.
"""
if line.startswith('SagaImageOutput'):
tokens = line.split("|")
params = [t if str(t) != str(None) else None for t in tokens[1:]]
if len(params) > 3:
params[3] = True if params[3].lower() == 'true' else False
if len(params) > 4:
params[4] = True if params[4].lower() == 'true' else False
return SagaImageOutputParam(*params)
else:
return getParameterFromString(line)
@@ -21,4 +21,4 @@ QgsProcessingParameterNumber|B_RANGE_MAX|Rescale Range for BLUE max|QgsProcessin
QgsProcessingParameterNumber|B_PERCTL_MIN|Percentiles Range for BLUE max|QgsProcessingParameterNumber.Integer|1|False|1|99
QgsProcessingParameterNumber|B_PERCTL_MAX|Percentiles Range for BLUE max|QgsProcessingParameterNumber.Integer|99|False|1|99
QgsProcessingParameterNumber|B_STDDEV|Standard deviation for BLUE|QgsProcessingParameterNumber.Double|2.0|False|0|None
QgsProcessingParameterRasterDestination|RGB|Output RGB
SagaImageOutput|RGB|Output RGB
@@ -28,7 +28,11 @@
import nose2
import shutil

from qgis.core import (QgsProcessingParameterNumber,
QgsProcessingParameterDefinition)
from qgis.testing import start_app, unittest

from processing.algs.saga.SagaParameters import Parameters, SagaImageOutputParam
import AlgorithmsTestBase


@@ -51,6 +55,33 @@ def tearDownClass(cls):
def test_definition_file(self):
return 'saga_algorithm_tests.yaml'

def test_is_parameter_line(self):
# Test determining whether a line is a parameter line
self.assertFalse(Parameters.is_parameter_line(''))
self.assertFalse(Parameters.is_parameter_line('xxxxxxxxx'))
self.assertTrue(Parameters.is_parameter_line('QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99'))
self.assertTrue(Parameters.is_parameter_line('*QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99'))
self.assertTrue(Parameters.is_parameter_line('SagaImageOutput|RGB|Output RGB'))

def test_param_line(self):
# Test creating a parameter from a description line
param = Parameters.create_parameter_from_line('QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99')
self.assertIsInstance(param, QgsProcessingParameterNumber)
self.assertEqual(param.name(), 'R_PERCTL_MIN')
self.assertEqual(param.description(), 'Percentiles Range for RED max')
self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer)
self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional)
self.assertEqual(param.minimum(), 1)
self.assertEqual(param.maximum(), 99)

# Test SagaImageOutputParam line
param = Parameters.create_parameter_from_line('SagaImageOutput|RGB|Output RGB')
self.assertIsInstance(param, SagaImageOutputParam)
self.assertEqual(param.name(), 'RGB')
self.assertEqual(param.description(), 'Output RGB')
self.assertEqual(param.defaultFileExtension(), 'tif')
self.assertEqual(param.supportedOutputRasterLayerExtensions(), ['tif'])


if __name__ == '__main__':
nose2.main()

0 comments on commit 1440ddd

Please sign in to comment.
You can’t perform that action at this time.