[processing][SAGA] Fix output type for RGB Composite should be tif, not sdat format

This commit is contained in:
Nyall Dawson 2018-12-04 14:18:58 +10:00
parent 5349f24cae
commit 71b32f072c
4 changed files with 110 additions and 4 deletions

View File

@ -49,10 +49,10 @@ from qgis.core import (Qgis,
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 @@ class SagaAlgorithm(SagaAlgorithmBase):
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:

View File

@ -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)

View File

@ -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

View File

@ -28,7 +28,11 @@ __revision__ = ':%H$'
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 @@ class TestSagaAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
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()