Skip to content

Commit eca9bdc

Browse files
nyalldawsonalexbruy
authored andcommittedNov 9, 2017
Start porting saga algorithm base to new API
1 parent 7ad764d commit eca9bdc

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed
 

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828

2929
import os
3030
import importlib
31-
from qgis.PyQt.QtCore import QCoreApplication
32-
from qgis.PyQt.QtGui import QIcon
3331
from qgis.core import (QgsProcessingUtils,
3432
QgsMessageLog)
35-
from processing.core.GeoAlgorithm import GeoAlgorithm
3633
from processing.core.ProcessingConfig import ProcessingConfig
3734
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3835
from processing.core.parameters import (getParameterFromString,
@@ -53,32 +50,37 @@
5350
from processing.tools.system import getTempFilename
5451
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
5552
from . import SagaUtils
53+
from .SagaAlgorithmBase import SagaAlgorithmBase
5654

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

6058
sessionExportedLayers = {}
6159

6260

63-
class SagaAlgorithm(GeoAlgorithm):
61+
class SagaAlgorithm(SagaAlgorithmBase):
6462

6563
OUTPUT_EXTENT = 'OUTPUT_EXTENT'
6664

6765
def __init__(self, descriptionfile):
68-
GeoAlgorithm.__init__(self)
69-
self.hardcodedStrings = []
70-
self.allowUnmatchingGridExtents = False
71-
self.descriptionFile = descriptionfile
72-
self.defineCharacteristicsFromFile()
73-
self._icon = None
66+
super().__init__()
67+
self.hardcoded_strings = []
68+
self.allow_nonmatching_grid_extents = False
69+
self.description_file = descriptionfile
70+
self.undecorated_group = None
7471
self._name = ''
7572
self._display_name = ''
7673
self._group = ''
74+
self.params = []
75+
self.defineCharacteristicsFromFile()
76+
77+
def createInstance(self):
78+
return SagaAlgorithm(self.description_file)
7779

78-
def icon(self):
79-
if self._icon is None:
80-
self._icon = QIcon(os.path.join(pluginPath, 'images', 'saga.png'))
81-
return self._icon
80+
def initAlgorithm(self, config=None):
81+
#for p in self.params:
82+
# self.addParameter(p)
83+
pass
8284

8385
def name(self):
8486
return self._name
@@ -93,7 +95,7 @@ def shortHelpString(self):
9395
return shortHelp.get(self.id(), None)
9496

9597
def defineCharacteristicsFromFile(self):
96-
with open(self.descriptionFile) as lines:
98+
with open(self.description_file) as lines:
9799
line = lines.readline().strip('\n').strip()
98100
self._name = line
99101
if '|' in self._name:
@@ -104,33 +106,34 @@ def defineCharacteristicsFromFile(self):
104106

105107
else:
106108
self.cmdname = self._name
107-
self._display_name = QCoreApplication.translate("SAGAAlgorithm", str(self._name))
109+
self._display_name = self.tr(str(self._name))
108110
self._name = decoratedAlgorithmName(self._name)
109-
self._display_name = QCoreApplication.translate("SAGAAlgorithm", str(self._name))
111+
self._display_name = self.tr(str(self._name))
110112

111113
self._name = self._name.lower()
112114
validChars = \
113115
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
114116
self._name = ''.join(c for c in self._name if c in validChars)
115117

116118
line = lines.readline().strip('\n').strip()
117-
self.undecoratedGroup = line
118-
self._group = QCoreApplication.translate("SAGAAlgorithm", decoratedGroupName(self.undecoratedGroup))
119+
self.undecorated_group = line
120+
self._group = self.tr(decoratedGroupName(self.undecorated_group))
119121
line = lines.readline().strip('\n').strip()
120122
while line != '':
121123
if line.startswith('Hardcoded'):
122-
self.hardcodedStrings.append(line[len('Hardcoded|'):])
124+
self.hardcoded_strings.append(line[len('Hardcoded|'):])
123125
elif line.startswith('Parameter'):
124-
self.addParameter(getParameterFromString(line))
126+
self.params.append(getParameterFromString(line))
125127
elif line.startswith('AllowUnmatching'):
126-
self.allowUnmatchingGridExtents = True
128+
self.allow_nonmatching_grid_extents = True
127129
elif line.startswith('Extent'):
128130
# An extent parameter that wraps 4 SAGA numerical parameters
129131
self.extentParamNames = line[6:].strip().split(' ')
130-
self.addParameter(ParameterExtent(self.OUTPUT_EXTENT,
131-
'Output extent'))
132+
self.params.append(ParameterExtent(self.OUTPUT_EXTENT,
133+
'Output extent'))
132134
else:
133-
self.addOutput(getOutputFromString(line))
135+
pass # TODO
136+
#self.addOutput(getOutputFromString(line))
134137
line = lines.readline().strip('\n').strip()
135138

136139
def processAlgorithm(self, parameters, context, feedback):
@@ -206,8 +209,8 @@ def processAlgorithm(self, parameters, context, feedback):
206209
extent = QgsProcessingUtils.combineLayerExtents([layer])
207210

208211
# 2: Set parameters and outputs
209-
command = self.undecoratedGroup + ' "' + self.cmdname + '"'
210-
command += ' ' + ' '.join(self.hardcodedStrings)
212+
command = self.undecorated_group + ' "' + self.cmdname + '"'
213+
command += ' ' + ' '.join(self.hardcoded_strings)
211214

212215
for param in self.parameterDefinitions():
213216
if not param.name() in parameters or parameters[param.name()] is None:
@@ -365,7 +368,7 @@ def checkParameterValues(self, parameters, context):
365368
if layer.bandCount() > 1:
366369
return False, self.tr('Input layer {0} has more than one band.\n'
367370
'Multiband layers are not supported by SAGA').format(layer.name())
368-
if not self.allowUnmatchingGridExtents:
371+
if not self.allow_nonmatching_grid_extents:
369372
if extent is None:
370373
extent = (layer.extent(), layer.height(), layer.width())
371374
else:

‎python/plugins/processing/algs/saga/SagaAlgorithmBase.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@ class SagaAlgorithmBase(QgsProcessingAlgorithm):
4040

4141
def __init__(self):
4242
super().__init__()
43-
self.output_values = {}
4443

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

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

51-
def tr(self, string, context=''):
52-
if context == '':
53-
context = self.__class__.__name__
54-
return QCoreApplication.translate(context, string)
50+
def tr(self, string):
51+
return QCoreApplication.translate("SAGAAlgorithm", string)

0 commit comments

Comments
 (0)
Please sign in to comment.