Skip to content

Commit 71b32f0

Browse files
committed
[processing][SAGA] Fix output type for RGB Composite should be tif, not sdat format
1 parent 5349f24 commit 71b32f0

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

python/plugins/processing/algs/saga/SagaAlgorithm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
QgsProcessingParameterRasterDestination,
5050
QgsProcessingParameterVectorDestination)
5151
from processing.core.ProcessingConfig import ProcessingConfig
52-
from processing.core.parameters import getParameterFromString
5352
from processing.algs.help import shortHelp
5453
from processing.tools.system import getTempFilename
5554
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
55+
from processing.algs.saga.SagaParameters import Parameters
5656
from . import SagaUtils
5757
from .SagaAlgorithmBase import SagaAlgorithmBase
5858

@@ -143,8 +143,8 @@ def defineCharacteristicsFromFile(self):
143143
while line != '':
144144
if line.startswith('Hardcoded'):
145145
self.hardcoded_strings.append(line[len('Hardcoded|'):])
146-
elif line.startswith('QgsProcessingParameter') or line.startswith('Parameter'):
147-
self.params.append(getParameterFromString(line))
146+
elif Parameters.is_parameter_line(line):
147+
self.params.append(Parameters.create_parameter_from_line(line))
148148
elif line.startswith('AllowUnmatching'):
149149
self.allow_nonmatching_grid_extents = True
150150
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SagaParameters.py
6+
---------------------
7+
Date : December 2018
8+
Copyright : (C) 2018 by Nyall Dawson
9+
Email : nyall dot dawson 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+
21+
__author__ = 'Nyall Dawson'
22+
__date__ = 'December 2018'
23+
__copyright__ = '(C) 2018, Nyall Dawson'
24+
25+
# This will get replaced with a git SHA1 when you do a git archive
26+
27+
__revision__ = '$Format:%H$'
28+
29+
import os
30+
import importlib
31+
from qgis.core import QgsProcessingParameterRasterDestination
32+
from processing.core.parameters import getParameterFromString
33+
34+
35+
class SagaImageOutputParam(QgsProcessingParameterRasterDestination):
36+
37+
"""
38+
Custom raster destination parameter for SAGA algorithms which create a raster image
39+
output, instead of SAGA's usual 'sdat' raster grid outputs.
40+
41+
These outputs differ from the usual SAGA outputs and are always generated as TIF files instead
42+
of sdat.
43+
"""
44+
45+
def defaultFileExtension(self):
46+
return 'tif'
47+
48+
def supportedOutputRasterLayerExtensions(self):
49+
return ['tif']
50+
51+
52+
class Parameters:
53+
54+
@staticmethod
55+
def is_parameter_line(line):
56+
"""
57+
Returns true if the given line corresponds to a SAGA parameter definition
58+
"""
59+
return line.startswith('SagaImageOutput') or line.startswith('QgsProcessingParameter') or line.startswith('Parameter') or line.startswith('*QgsProcessingParameter')
60+
61+
@staticmethod
62+
def create_parameter_from_line(line):
63+
"""
64+
Creates a parameter from a definition line.
65+
"""
66+
if line.startswith('SagaImageOutput'):
67+
tokens = line.split("|")
68+
params = [t if str(t) != str(None) else None for t in tokens[1:]]
69+
if len(params) > 3:
70+
params[3] = True if params[3].lower() == 'true' else False
71+
if len(params) > 4:
72+
params[4] = True if params[4].lower() == 'true' else False
73+
return SagaImageOutputParam(*params)
74+
else:
75+
return getParameterFromString(line)

python/plugins/processing/algs/saga/description/RGBComposite.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ QgsProcessingParameterNumber|B_RANGE_MAX|Rescale Range for BLUE max|QgsProcessin
2121
QgsProcessingParameterNumber|B_PERCTL_MIN|Percentiles Range for BLUE max|QgsProcessingParameterNumber.Integer|1|False|1|99
2222
QgsProcessingParameterNumber|B_PERCTL_MAX|Percentiles Range for BLUE max|QgsProcessingParameterNumber.Integer|99|False|1|99
2323
QgsProcessingParameterNumber|B_STDDEV|Standard deviation for BLUE|QgsProcessingParameterNumber.Double|2.0|False|0|None
24-
QgsProcessingParameterRasterDestination|RGB|Output RGB
24+
SagaImageOutput|RGB|Output RGB

python/plugins/processing/tests/SagaAlgorithmsTest.py

+31
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import nose2
2929
import shutil
3030

31+
from qgis.core import (QgsProcessingParameterNumber,
32+
QgsProcessingParameterDefinition)
3133
from qgis.testing import start_app, unittest
34+
35+
from processing.algs.saga.SagaParameters import Parameters, SagaImageOutputParam
3236
import AlgorithmsTestBase
3337

3438

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

58+
def test_is_parameter_line(self):
59+
# Test determining whether a line is a parameter line
60+
self.assertFalse(Parameters.is_parameter_line(''))
61+
self.assertFalse(Parameters.is_parameter_line('xxxxxxxxx'))
62+
self.assertTrue(Parameters.is_parameter_line('QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99'))
63+
self.assertTrue(Parameters.is_parameter_line('*QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99'))
64+
self.assertTrue(Parameters.is_parameter_line('SagaImageOutput|RGB|Output RGB'))
65+
66+
def test_param_line(self):
67+
# Test creating a parameter from a description line
68+
param = Parameters.create_parameter_from_line('QgsProcessingParameterNumber|R_PERCTL_MIN|Percentiles Range for RED max|QgsProcessingParameterNumber.Integer|1|False|1|99')
69+
self.assertIsInstance(param, QgsProcessingParameterNumber)
70+
self.assertEqual(param.name(), 'R_PERCTL_MIN')
71+
self.assertEqual(param.description(), 'Percentiles Range for RED max')
72+
self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer)
73+
self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional)
74+
self.assertEqual(param.minimum(), 1)
75+
self.assertEqual(param.maximum(), 99)
76+
77+
# Test SagaImageOutputParam line
78+
param = Parameters.create_parameter_from_line('SagaImageOutput|RGB|Output RGB')
79+
self.assertIsInstance(param, SagaImageOutputParam)
80+
self.assertEqual(param.name(), 'RGB')
81+
self.assertEqual(param.description(), 'Output RGB')
82+
self.assertEqual(param.defaultFileExtension(), 'tif')
83+
self.assertEqual(param.supportedOutputRasterLayerExtensions(), ['tif'])
84+
5485

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

0 commit comments

Comments
 (0)