2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SagaAlgorithm.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2014-12-19 15:01:02 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import os
|
2019-01-25 09:39:45 +01:00
|
|
|
import shutil
|
2013-07-23 14:28:24 +02:00
|
|
|
import importlib
|
2018-02-05 22:11:34 -04:00
|
|
|
from qgis.core import (Qgis,
|
2018-02-26 10:36:20 +02:00
|
|
|
QgsApplication,
|
2018-02-05 22:11:34 -04:00
|
|
|
QgsProcessingUtils,
|
2017-08-15 17:08:29 +10:00
|
|
|
QgsProcessingException,
|
2017-08-15 17:32:05 +10:00
|
|
|
QgsMessageLog,
|
2017-08-18 02:32:06 +10:00
|
|
|
QgsProcessing,
|
2018-01-29 12:21:41 +10:00
|
|
|
QgsProcessingAlgorithm,
|
2017-08-15 17:32:05 +10:00
|
|
|
QgsProcessingParameterRasterLayer,
|
2017-08-18 02:05:16 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
2017-08-15 17:39:27 +10:00
|
|
|
QgsProcessingParameterBoolean,
|
2017-08-15 17:48:32 +10:00
|
|
|
QgsProcessingParameterNumber,
|
2017-08-18 02:32:06 +10:00
|
|
|
QgsProcessingParameterEnum,
|
2017-08-18 03:34:03 +10:00
|
|
|
QgsProcessingParameterMultipleLayers,
|
|
|
|
QgsProcessingParameterMatrix,
|
|
|
|
QgsProcessingParameterString,
|
2017-08-18 04:29:28 +10:00
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterFile,
|
|
|
|
QgsProcessingParameterExtent,
|
|
|
|
QgsProcessingParameterRasterDestination,
|
|
|
|
QgsProcessingParameterVectorDestination)
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.help import shortHelp
|
2017-06-21 18:23:45 +10:00
|
|
|
from processing.tools.system import getTempFilename
|
2016-03-15 16:43:52 +01:00
|
|
|
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
|
2018-12-04 14:18:58 +10:00
|
|
|
from processing.algs.saga.SagaParameters import Parameters
|
2016-03-21 04:58:12 +01:00
|
|
|
from . import SagaUtils
|
2017-08-15 03:57:19 +10:00
|
|
|
from .SagaAlgorithmBase import SagaAlgorithmBase
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.normpath(os.path.join(
|
|
|
|
os.path.split(os.path.dirname(__file__))[0], os.pardir))
|
|
|
|
|
2013-09-14 17:41:08 +02:00
|
|
|
sessionExportedLayers = {}
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-08-15 03:57:19 +10:00
|
|
|
class SagaAlgorithm(SagaAlgorithmBase):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT_EXTENT = 'OUTPUT_EXTENT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-05-21 21:25:18 +02:00
|
|
|
def __init__(self, descriptionfile):
|
2017-08-15 03:57:19 +10:00
|
|
|
super().__init__()
|
|
|
|
self.hardcoded_strings = []
|
|
|
|
self.allow_nonmatching_grid_extents = False
|
|
|
|
self.description_file = descriptionfile
|
|
|
|
self.undecorated_group = None
|
2017-03-29 12:51:59 +10:00
|
|
|
self._name = ''
|
|
|
|
self._display_name = ''
|
|
|
|
self._group = ''
|
2017-12-14 11:42:24 +02:00
|
|
|
self._groupId = ''
|
2017-08-15 03:57:19 +10:00
|
|
|
self.params = []
|
2019-03-05 17:05:12 +10:00
|
|
|
self.known_issues = False
|
2017-08-15 03:57:19 +10:00
|
|
|
self.defineCharacteristicsFromFile()
|
|
|
|
|
|
|
|
def createInstance(self):
|
|
|
|
return SagaAlgorithm(self.description_file)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-08-15 03:57:19 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2017-08-15 05:07:27 +10:00
|
|
|
for p in self.params:
|
2017-08-15 16:59:16 +10:00
|
|
|
self.addParameter(p)
|
2016-09-14 06:42:04 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self._display_name
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self._group
|
|
|
|
|
2017-12-14 11:42:24 +02:00
|
|
|
def groupId(self):
|
|
|
|
return self._groupId
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
def shortHelpString(self):
|
|
|
|
return shortHelp.get(self.id(), None)
|
|
|
|
|
2018-02-26 10:36:20 +02:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerSaga.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerSaga.svg")
|
|
|
|
|
2018-01-29 12:21:41 +10:00
|
|
|
def flags(self):
|
|
|
|
# TODO - maybe it's safe to background thread this?
|
2019-03-05 17:05:12 +10:00
|
|
|
f = super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
|
|
|
if self.known_issues:
|
|
|
|
f = f | QgsProcessingAlgorithm.FlagKnownIssues
|
|
|
|
return f
|
2018-01-29 12:21:41 +10:00
|
|
|
|
2014-05-21 21:25:18 +02:00
|
|
|
def defineCharacteristicsFromFile(self):
|
2018-06-04 15:00:56 +10:00
|
|
|
with open(self.description_file, encoding="utf-8") as lines:
|
2016-11-07 10:35:15 +10:00
|
|
|
line = lines.readline().strip('\n').strip()
|
2019-03-05 17:05:12 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
self._name = line
|
|
|
|
if '|' in self._name:
|
|
|
|
tokens = self._name.split('|')
|
|
|
|
self._name = tokens[0]
|
2017-03-04 16:23:36 +01:00
|
|
|
# cmdname is the name of the algorithm in SAGA, that is, the name to use to call it in the console
|
2016-11-07 10:35:15 +10:00
|
|
|
self.cmdname = tokens[1]
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2017-03-29 12:51:59 +10:00
|
|
|
self.cmdname = self._name
|
2017-08-15 03:57:19 +10:00
|
|
|
self._display_name = self.tr(str(self._name))
|
2017-03-29 12:51:59 +10:00
|
|
|
self._name = decoratedAlgorithmName(self._name)
|
2017-08-15 03:57:19 +10:00
|
|
|
self._display_name = self.tr(str(self._name))
|
2017-03-29 17:09:39 +10:00
|
|
|
|
|
|
|
self._name = self._name.lower()
|
|
|
|
validChars = \
|
|
|
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
|
|
|
|
self._name = ''.join(c for c in self._name if c in validChars)
|
|
|
|
|
2016-11-07 10:35:15 +10:00
|
|
|
line = lines.readline().strip('\n').strip()
|
2019-03-05 17:05:12 +10:00
|
|
|
if line == '##known_issues':
|
|
|
|
self.known_issues = True
|
|
|
|
line = lines.readline().strip('\n').strip()
|
|
|
|
|
2017-08-15 03:57:19 +10:00
|
|
|
self.undecorated_group = line
|
|
|
|
self._group = self.tr(decoratedGroupName(self.undecorated_group))
|
2017-12-14 11:42:24 +02:00
|
|
|
|
|
|
|
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
|
|
|
|
grpName = decoratedGroupName(self.undecorated_group).lower()
|
|
|
|
self._groupId = ''.join(c for c in grpName if c in validChars)
|
2013-10-01 20:52:22 +03:00
|
|
|
line = lines.readline().strip('\n').strip()
|
2016-11-07 10:35:15 +10:00
|
|
|
while line != '':
|
|
|
|
if line.startswith('Hardcoded'):
|
2017-08-15 03:57:19 +10:00
|
|
|
self.hardcoded_strings.append(line[len('Hardcoded|'):])
|
2018-12-04 14:18:58 +10:00
|
|
|
elif Parameters.is_parameter_line(line):
|
|
|
|
self.params.append(Parameters.create_parameter_from_line(line))
|
2016-11-07 10:35:15 +10:00
|
|
|
elif line.startswith('AllowUnmatching'):
|
2017-08-15 03:57:19 +10:00
|
|
|
self.allow_nonmatching_grid_extents = True
|
2016-11-07 10:35:15 +10:00
|
|
|
else:
|
2017-08-15 03:57:19 +10:00
|
|
|
pass # TODO
|
|
|
|
#self.addOutput(getOutputFromString(line))
|
2016-11-07 10:35:15 +10:00
|
|
|
line = lines.readline().strip('\n').strip()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2012-09-15 18:25:25 +03:00
|
|
|
commands = list()
|
|
|
|
self.exportedLayers = {}
|
2013-08-25 10:45:52 +02:00
|
|
|
|
2013-07-23 13:05:24 +02:00
|
|
|
self.preProcessInputs()
|
2017-10-16 23:21:51 +02:00
|
|
|
extent = None
|
2017-11-06 13:57:41 +02:00
|
|
|
crs = None
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# 1: Export rasters to sgrd and vectors to shp
|
|
|
|
# Tables must be in dbf format. We check that.
|
2017-05-22 14:07:53 +10:00
|
|
|
for param in self.parameterDefinitions():
|
2017-08-15 17:32:05 +10:00
|
|
|
if isinstance(param, QgsProcessingParameterRasterLayer):
|
2017-05-22 14:07:53 +10:00
|
|
|
if param.name() not in parameters or parameters[param.name()] is None:
|
2012-09-15 18:25:25 +03:00
|
|
|
continue
|
2017-09-05 13:21:18 +03:00
|
|
|
|
2017-11-07 12:16:15 +02:00
|
|
|
if isinstance(parameters[param.name()], str):
|
|
|
|
if parameters[param.name()].lower().endswith('sdat'):
|
|
|
|
self.exportedLayers[param.name()] = parameters[param.name()][:-4] + 'sgrd'
|
2019-03-29 10:37:34 +01:00
|
|
|
elif parameters[param.name()].lower().endswith('sgrd'):
|
2017-11-07 12:16:15 +02:00
|
|
|
self.exportedLayers[param.name()] = parameters[param.name()]
|
|
|
|
else:
|
|
|
|
layer = self.parameterAsRasterLayer(parameters, param.name(), context)
|
|
|
|
exportCommand = self.exportRasterLayer(param.name(), layer)
|
|
|
|
if exportCommand is not None:
|
|
|
|
commands.append(exportCommand)
|
2017-09-05 13:21:18 +03:00
|
|
|
else:
|
2017-11-07 12:16:15 +02:00
|
|
|
if parameters[param.name()].source().lower().endswith('sdat'):
|
|
|
|
self.exportedLayers[param.name()] = parameters[param.name()].source()[:-4] + 'sgrd'
|
|
|
|
if parameters[param.name()].source().lower().endswith('sgrd'):
|
|
|
|
self.exportedLayers[param.name()] = parameters[param.name()].source()
|
|
|
|
else:
|
|
|
|
exportCommand = self.exportRasterLayer(param.name(), parameters[param.name()])
|
|
|
|
if exportCommand is not None:
|
|
|
|
commands.append(exportCommand)
|
2017-08-18 02:05:16 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterFeatureSource):
|
2017-05-22 14:07:53 +10:00
|
|
|
if param.name() not in parameters or parameters[param.name()] is None:
|
2012-09-15 18:25:25 +03:00
|
|
|
continue
|
2017-08-18 04:29:28 +10:00
|
|
|
|
|
|
|
if not crs:
|
|
|
|
source = self.parameterAsSource(parameters, param.name(), context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, param.name()))
|
|
|
|
|
2017-08-18 04:29:28 +10:00
|
|
|
crs = source.sourceCrs()
|
|
|
|
|
|
|
|
layer_path = self.parameterAsCompatibleSourceLayerPath(parameters, param.name(), context, ['shp'], 'shp', feedback=feedback)
|
2017-08-18 02:05:16 +10:00
|
|
|
if layer_path:
|
2017-08-18 04:29:28 +10:00
|
|
|
self.exportedLayers[param.name()] = layer_path
|
2017-08-18 02:05:16 +10:00
|
|
|
else:
|
2017-08-15 17:08:29 +10:00
|
|
|
raise QgsProcessingException(
|
2015-01-16 16:25:12 +02:00
|
|
|
self.tr('Unsupported file format'))
|
2017-08-18 02:32:06 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterMultipleLayers):
|
2017-05-22 14:07:53 +10:00
|
|
|
if param.name() not in parameters or parameters[param.name()] is None:
|
2012-09-15 18:25:25 +03:00
|
|
|
continue
|
2017-09-05 13:21:18 +03:00
|
|
|
|
2017-08-18 02:32:06 +10:00
|
|
|
layers = self.parameterAsLayerList(parameters, param.name(), context)
|
2013-10-01 20:52:22 +03:00
|
|
|
if layers is None or len(layers) == 0:
|
2012-09-15 18:25:25 +03:00
|
|
|
continue
|
2017-08-18 02:32:06 +10:00
|
|
|
if param.layerType() == QgsProcessing.TypeRaster:
|
2017-09-05 13:21:18 +03:00
|
|
|
files = []
|
|
|
|
for i, layer in enumerate(layers):
|
|
|
|
if layer.source().lower().endswith('sdat'):
|
2019-02-18 16:15:12 +01:00
|
|
|
files.append(layer.source()[:-4] + 'sgrd')
|
2019-05-23 20:18:56 +02:00
|
|
|
elif layer.source().lower().endswith('sgrd'):
|
2019-02-18 16:15:12 +01:00
|
|
|
files.append(layer.source())
|
2017-09-05 13:21:18 +03:00
|
|
|
else:
|
|
|
|
exportCommand = self.exportRasterLayer(param.name(), layer)
|
|
|
|
files.append(self.exportedLayers[param.name()])
|
2013-09-15 18:45:11 +02:00
|
|
|
if exportCommand is not None:
|
2013-09-20 12:44:16 +02:00
|
|
|
commands.append(exportCommand)
|
2017-09-05 13:21:18 +03:00
|
|
|
|
|
|
|
self.exportedLayers[param.name()] = files
|
2017-08-18 02:32:06 +10:00
|
|
|
else:
|
|
|
|
for layer in layers:
|
2018-06-04 13:43:20 +10:00
|
|
|
temp_params = {}
|
2017-08-18 02:32:06 +10:00
|
|
|
temp_params[param.name()] = layer
|
2017-08-18 04:29:28 +10:00
|
|
|
|
|
|
|
if not crs:
|
|
|
|
source = self.parameterAsSource(temp_params, param.name(), context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, param.name()))
|
|
|
|
|
2017-08-18 04:29:28 +10:00
|
|
|
crs = source.sourceCrs()
|
|
|
|
|
2018-06-04 13:43:20 +10:00
|
|
|
layer_path = self.parameterAsCompatibleSourceLayerPath(temp_params, param.name(), context, ['shp'], 'shp',
|
2017-08-18 02:32:06 +10:00
|
|
|
feedback=feedback)
|
|
|
|
if layer_path:
|
|
|
|
if param.name() in self.exportedLayers:
|
|
|
|
self.exportedLayers[param.name()].append(layer_path)
|
|
|
|
else:
|
|
|
|
self.exportedLayers[param.name()] = [layer_path]
|
|
|
|
else:
|
2017-08-15 17:08:29 +10:00
|
|
|
raise QgsProcessingException(
|
2015-01-16 16:25:12 +02:00
|
|
|
self.tr('Unsupported file format'))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# 2: Set parameters and outputs
|
2017-08-15 03:57:19 +10:00
|
|
|
command = self.undecorated_group + ' "' + self.cmdname + '"'
|
|
|
|
command += ' ' + ' '.join(self.hardcoded_strings)
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2017-05-22 14:07:53 +10:00
|
|
|
for param in self.parameterDefinitions():
|
|
|
|
if not param.name() in parameters or parameters[param.name()] is None:
|
2012-09-15 18:25:25 +03:00
|
|
|
continue
|
2017-08-18 04:29:28 +10:00
|
|
|
if param.isDestination():
|
|
|
|
continue
|
|
|
|
|
2017-08-18 02:19:45 +10:00
|
|
|
if isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterFeatureSource)):
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} "{}"'.format(param.name(), self.exportedLayers[param.name()])
|
2017-08-18 02:32:06 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterMultipleLayers):
|
2018-06-04 12:47:20 +10:00
|
|
|
if parameters[param.name()]: # parameter may have been an empty list
|
|
|
|
command += ' -{} "{}"'.format(param.name(), ';'.join(self.exportedLayers[param.name()]))
|
2017-08-15 17:39:27 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterBoolean):
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, param.name(), context):
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} true'.format(param.name().strip())
|
2017-04-04 11:01:06 +02:00
|
|
|
else:
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} false'.format(param.name().strip())
|
2017-08-18 03:34:03 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterMatrix):
|
2013-10-01 20:52:22 +03:00
|
|
|
tempTableFile = getTempFilename('txt')
|
2016-11-07 10:35:15 +10:00
|
|
|
with open(tempTableFile, 'w') as f:
|
2017-08-18 03:34:03 +10:00
|
|
|
f.write('\t'.join([col for col in param.headers()]) + '\n')
|
|
|
|
values = self.parameterAsMatrix(parameters, param.name(), context)
|
2016-11-07 10:35:15 +10:00
|
|
|
for i in range(0, len(values), 3):
|
2018-06-04 11:41:30 +10:00
|
|
|
s = '{}\t{}\t{}\n'.format(values[i], values[i + 1], values[i + 2])
|
2016-11-07 10:35:15 +10:00
|
|
|
f.write(s)
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} "{}"'.format(param.name(), tempTableFile)
|
2017-08-18 04:29:28 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterExtent):
|
2013-10-01 20:52:22 +03:00
|
|
|
# 'We have to substract/add half cell size, since SAGA is
|
|
|
|
# center based, not corner based
|
2017-08-18 04:29:28 +10:00
|
|
|
halfcell = self.getOutputCellsize(parameters, context) / 2
|
2012-10-27 10:59:23 +02:00
|
|
|
offset = [halfcell, -halfcell, halfcell, -halfcell]
|
2017-08-18 04:29:28 +10:00
|
|
|
rect = self.parameterAsExtent(parameters, param.name(), context)
|
|
|
|
|
|
|
|
values = []
|
|
|
|
values.append(rect.xMinimum())
|
|
|
|
values.append(rect.xMaximum())
|
2018-02-24 22:56:39 +01:00
|
|
|
values.append(rect.yMinimum())
|
2017-08-18 04:29:28 +10:00
|
|
|
values.append(rect.yMaximum())
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
for i in range(4):
|
2018-03-10 20:10:51 +01:00
|
|
|
command += ' -{} {}'.format(param.name().split(' ')[i], float(values[i]) + offset[i])
|
2017-08-18 03:34:03 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterNumber):
|
2017-09-05 13:55:15 +03:00
|
|
|
if param.dataType() == QgsProcessingParameterNumber.Integer:
|
|
|
|
command += ' -{} {}'.format(param.name(), self.parameterAsInt(parameters, param.name(), context))
|
|
|
|
else:
|
|
|
|
command += ' -{} {}'.format(param.name(), self.parameterAsDouble(parameters, param.name(), context))
|
2017-08-18 03:34:03 +10:00
|
|
|
elif isinstance(param, QgsProcessingParameterEnum):
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} {}'.format(param.name(), self.parameterAsEnum(parameters, param.name(), context))
|
2017-08-18 04:29:28 +10:00
|
|
|
elif isinstance(param, (QgsProcessingParameterString, QgsProcessingParameterFile)):
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} "{}"'.format(param.name(), self.parameterAsFile(parameters, param.name(), context))
|
2017-08-18 04:29:28 +10:00
|
|
|
elif isinstance(param, (QgsProcessingParameterString, QgsProcessingParameterField)):
|
2017-09-05 13:21:18 +03:00
|
|
|
command += ' -{} "{}"'.format(param.name(), self.parameterAsString(parameters, param.name(), context))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-08-18 04:29:28 +10:00
|
|
|
output_layers = []
|
|
|
|
output_files = {}
|
2019-01-25 09:39:45 +01:00
|
|
|
#If the user has entered an output file that has non-ascii chars, we use a different path with only ascii chars
|
|
|
|
output_files_nonascii = {}
|
2017-08-18 04:29:28 +10:00
|
|
|
for out in self.destinationParameterDefinitions():
|
2017-09-05 13:21:18 +03:00
|
|
|
filePath = self.parameterAsOutputLayer(parameters, out.name(), context)
|
2017-08-18 04:29:28 +10:00
|
|
|
if isinstance(out, (QgsProcessingParameterRasterDestination, QgsProcessingParameterVectorDestination)):
|
2017-09-05 13:21:18 +03:00
|
|
|
output_layers.append(filePath)
|
2019-01-25 09:39:45 +01:00
|
|
|
try:
|
|
|
|
filePath.encode('ascii')
|
|
|
|
except UnicodeEncodeError:
|
2019-01-28 09:34:15 +01:00
|
|
|
nonAsciiFilePath = filePath
|
2019-01-25 09:39:45 +01:00
|
|
|
filePath = QgsProcessingUtils.generateTempFilename(out.name() + os.path.splitext(filePath)[1])
|
|
|
|
output_files_nonascii[filePath] = nonAsciiFilePath
|
2019-01-28 09:34:15 +01:00
|
|
|
|
2017-09-05 13:21:18 +03:00
|
|
|
output_files[out.name()] = filePath
|
|
|
|
command += ' -{} "{}"'.format(out.name(), filePath)
|
2019-02-21 10:05:46 +01:00
|
|
|
commands.append(command)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-06-22 09:10:01 +02:00
|
|
|
# special treatment for RGB algorithm
|
2017-03-04 16:23:36 +01:00
|
|
|
# TODO: improve this and put this code somewhere else
|
2017-08-18 04:29:28 +10:00
|
|
|
for out in self.destinationParameterDefinitions():
|
|
|
|
if isinstance(out, QgsProcessingParameterRasterDestination):
|
2017-09-05 13:21:18 +03:00
|
|
|
filename = self.parameterAsOutputLayer(parameters, out.name(), context)
|
|
|
|
filename2 = os.path.splitext(filename)[0] + '.sgrd'
|
2013-10-15 20:40:48 +01:00
|
|
|
if self.cmdname == 'RGB Composite':
|
2018-11-05 11:53:12 +00:00
|
|
|
commands.append('io_grid_image 0 -COLOURING 4 -GRID:"{}" -FILE:"{}"'.format(filename2, filename))
|
2013-08-25 10:45:52 +02:00
|
|
|
|
2015-06-22 09:10:01 +02:00
|
|
|
# 3: Run SAGA
|
2013-07-23 13:05:24 +02:00
|
|
|
commands = self.editCommands(commands)
|
2012-09-15 18:25:25 +03:00
|
|
|
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
|
|
|
|
loglines = []
|
2015-01-16 16:25:12 +02:00
|
|
|
loglines.append(self.tr('SAGA execution commands'))
|
2012-09-15 18:25:25 +03:00
|
|
|
for line in commands:
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.pushCommandInfo(line)
|
2012-09-15 18:25:25 +03:00
|
|
|
loglines.append(line)
|
2013-08-12 20:44:27 +02:00
|
|
|
if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS):
|
2018-02-05 22:11:34 -04:00
|
|
|
QgsMessageLog.logMessage('\n'.join(loglines), self.tr('Processing'), Qgis.Info)
|
2017-01-06 20:04:00 +10:00
|
|
|
SagaUtils.executeSaga(feedback)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2017-08-18 04:29:28 +10:00
|
|
|
if crs is not None:
|
|
|
|
for out in output_layers:
|
2017-09-05 13:21:18 +03:00
|
|
|
prjFile = os.path.splitext(out)[0] + '.prj'
|
|
|
|
with open(prjFile, 'w') as f:
|
2017-08-18 04:29:28 +10:00
|
|
|
f.write(crs.toWkt())
|
|
|
|
|
2019-01-25 09:39:45 +01:00
|
|
|
for old, new in output_files_nonascii.items():
|
|
|
|
oldFolder = os.path.dirname(old)
|
|
|
|
newFolder = os.path.dirname(new)
|
|
|
|
newName = os.path.splitext(os.path.basename(new))[0]
|
|
|
|
files = [f for f in os.listdir(oldFolder)]
|
|
|
|
for f in files:
|
|
|
|
ext = os.path.splitext(f)[1]
|
|
|
|
newPath = os.path.join(newFolder, newName + ext)
|
|
|
|
oldPath = os.path.join(oldFolder, f)
|
|
|
|
shutil.move(oldPath, newPath)
|
2017-08-18 04:29:28 +10:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
for o in self.outputDefinitions():
|
|
|
|
if o.name() in output_files:
|
|
|
|
result[o.name()] = output_files[o.name()]
|
|
|
|
return result
|
2015-07-10 12:41:14 +02:00
|
|
|
|
2013-07-23 13:05:24 +02:00
|
|
|
def preProcessInputs(self):
|
2017-04-03 20:35:03 +10:00
|
|
|
name = self.name().replace('.', '_')
|
2013-07-23 13:05:24 +02:00
|
|
|
try:
|
2014-04-17 01:41:25 +02:00
|
|
|
module = importlib.import_module('processing.algs.saga.ext.' + name)
|
2013-07-23 13:05:24 +02:00
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
if hasattr(module, 'preProcessInputs'):
|
2013-10-01 20:52:22 +03:00
|
|
|
func = getattr(module, 'preProcessInputs')
|
2013-07-23 13:05:24 +02:00
|
|
|
func(self)
|
2013-08-25 10:45:52 +02:00
|
|
|
|
2013-07-23 13:05:24 +02:00
|
|
|
def editCommands(self, commands):
|
|
|
|
try:
|
2017-04-03 20:35:03 +10:00
|
|
|
module = importlib.import_module('processing.algs.saga.ext.' + self.name())
|
2013-07-23 13:05:24 +02:00
|
|
|
except ImportError:
|
|
|
|
return commands
|
|
|
|
if hasattr(module, 'editCommands'):
|
2013-10-01 20:52:22 +03:00
|
|
|
func = getattr(module, 'editCommands')
|
2013-07-23 13:05:24 +02:00
|
|
|
return func(commands)
|
|
|
|
else:
|
|
|
|
return commands
|
|
|
|
|
2017-08-18 04:29:28 +10:00
|
|
|
def getOutputCellsize(self, parameters, context):
|
2016-12-21 16:21:40 +01:00
|
|
|
"""Tries to guess the cell size of the output, searching for
|
2013-10-01 20:52:22 +03:00
|
|
|
a parameter with an appropriate name for it.
|
2017-05-22 14:07:53 +10:00
|
|
|
:param parameters:
|
2013-10-01 20:52:22 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
cellsize = 0
|
2017-05-22 14:07:53 +10:00
|
|
|
for param in self.parameterDefinitions():
|
|
|
|
if param.name() in parameters and param.name() == 'USER_SIZE':
|
2017-08-18 04:29:28 +10:00
|
|
|
cellsize = self.parameterAsDouble(parameters, param.name(), context)
|
2013-10-01 20:52:22 +03:00
|
|
|
break
|
2012-10-20 21:38:46 +02:00
|
|
|
return cellsize
|
2014-05-21 21:25:18 +02:00
|
|
|
|
2017-09-05 13:21:18 +03:00
|
|
|
def exportRasterLayer(self, parameterName, layer):
|
2014-05-20 01:10:23 +02:00
|
|
|
global sessionExportedLayers
|
2017-09-05 13:21:18 +03:00
|
|
|
if layer.source() in sessionExportedLayers:
|
|
|
|
exportedLayer = sessionExportedLayers[layer.source()]
|
2015-01-20 11:32:47 +01:00
|
|
|
if os.path.exists(exportedLayer):
|
2017-09-05 13:21:18 +03:00
|
|
|
self.exportedLayers[parameterName] = exportedLayer
|
2015-01-20 11:32:47 +01:00
|
|
|
return None
|
|
|
|
else:
|
2017-09-05 13:21:18 +03:00
|
|
|
del sessionExportedLayers[layer.source()]
|
|
|
|
|
2013-09-02 00:54:25 +02:00
|
|
|
if layer:
|
2017-09-05 13:21:18 +03:00
|
|
|
filename = layer.name()
|
2013-09-02 00:54:25 +02:00
|
|
|
else:
|
2017-09-05 13:21:18 +03:00
|
|
|
filename = os.path.basename(layer.source())
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
|
2013-09-14 14:25:58 +02:00
|
|
|
filename = ''.join(c for c in filename if c in validChars)
|
2017-09-05 13:21:18 +03:00
|
|
|
|
2013-09-02 00:54:25 +02:00
|
|
|
if len(filename) == 0:
|
2013-10-01 20:52:22 +03:00
|
|
|
filename = 'layer'
|
2017-09-05 13:21:18 +03:00
|
|
|
|
2017-06-21 18:23:45 +10:00
|
|
|
destFilename = QgsProcessingUtils.generateTempFilename(filename + '.sgrd')
|
2017-09-05 13:21:18 +03:00
|
|
|
sessionExportedLayers[layer.source()] = destFilename
|
|
|
|
self.exportedLayers[parameterName] = destFilename
|
|
|
|
|
|
|
|
return 'io_gdal 0 -TRANSFORM 1 -RESAMPLING 3 -GRIDS "{}" -FILES "{}"'.format(destFilename, layer.source())
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2017-05-16 15:21:41 +10:00
|
|
|
def checkParameterValues(self, parameters, context):
|
2013-10-01 20:52:22 +03:00
|
|
|
"""
|
2014-05-18 12:01:36 +02:00
|
|
|
We check that there are no multiband layers, which are not
|
|
|
|
supported by SAGA, and that raster layers have the same grid extent
|
2014-05-21 21:25:18 +02:00
|
|
|
"""
|
2014-05-18 12:01:36 +02:00
|
|
|
extent = None
|
2018-05-28 15:23:20 +10:00
|
|
|
raster_layer_params = []
|
2017-05-16 15:21:41 +10:00
|
|
|
for param in self.parameterDefinitions():
|
2018-05-28 15:23:20 +10:00
|
|
|
if param not in parameters or parameters[param.name()] is None:
|
|
|
|
continue
|
|
|
|
|
2017-08-15 17:32:05 +10:00
|
|
|
if isinstance(param, QgsProcessingParameterRasterLayer):
|
2018-05-28 15:23:20 +10:00
|
|
|
raster_layer_params.append(param.name())
|
2019-01-28 09:34:15 +01:00
|
|
|
elif (isinstance(param, QgsProcessingParameterMultipleLayers)
|
|
|
|
and param.layerType() == QgsProcessing.TypeRaster):
|
2018-05-28 15:23:20 +10:00
|
|
|
raster_layer_params.extend(param.name())
|
|
|
|
|
|
|
|
for layer_param in raster_layer_params:
|
|
|
|
layer = self.parameterAsRasterLayer(parameters, layer_param, context)
|
2017-11-07 11:23:25 +02:00
|
|
|
|
2018-05-28 15:23:20 +10:00
|
|
|
if layer is None:
|
2017-11-07 11:23:25 +02:00
|
|
|
continue
|
|
|
|
if layer.bandCount() > 1:
|
|
|
|
return False, self.tr('Input layer {0} has more than one band.\n'
|
|
|
|
'Multiband layers are not supported by SAGA').format(layer.name())
|
|
|
|
if not self.allow_nonmatching_grid_extents:
|
|
|
|
if extent is None:
|
|
|
|
extent = (layer.extent(), layer.height(), layer.width())
|
|
|
|
else:
|
|
|
|
extent2 = (layer.extent(), layer.height(), layer.width())
|
|
|
|
if extent != extent2:
|
|
|
|
return False, self.tr("Input layers do not have the same grid extent.")
|
2017-05-16 15:21:41 +10:00
|
|
|
return super(SagaAlgorithm, self).checkParameterValues(parameters, context)
|