2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SagaAlgorithmProvider.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
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
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
import os
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2017-04-04 11:38:49 +10:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication
|
2017-04-24 14:35:50 +10:00
|
|
|
from qgis.core import (QgsProcessingProvider,
|
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsMessageLog)
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
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
|
|
|
from processing.tools.system import isWindows, isMac
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-02-08 16:33:20 +02:00
|
|
|
from .SagaAlgorithm import SagaAlgorithm
|
|
|
|
from .SplitRGBBands import SplitRGBBands
|
|
|
|
from . import SagaUtils
|
2015-11-04 12:28:24 +01: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))
|
|
|
|
|
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
class SagaAlgorithmProvider(QgsProcessingProvider):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __init__(self):
|
2017-01-09 09:18:25 +10:00
|
|
|
super().__init__()
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def load(self):
|
2017-04-04 12:22:29 +10:00
|
|
|
ProcessingConfig.settingIcons[self.name()] = self.icon()
|
|
|
|
ProcessingConfig.addSetting(Setting("SAGA", 'ACTIVATE_SAGA',
|
|
|
|
self.tr('Activate'), True))
|
2014-12-19 15:01:02 +01:00
|
|
|
ProcessingConfig.addSetting(Setting("SAGA",
|
2015-08-22 14:29:41 +02:00
|
|
|
SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION,
|
|
|
|
self.tr('Enable SAGA Import/Export optimizations'), False))
|
2014-12-19 15:01:02 +01:00
|
|
|
ProcessingConfig.addSetting(Setting("SAGA",
|
2015-08-22 14:29:41 +02:00
|
|
|
SagaUtils.SAGA_LOG_COMMANDS,
|
|
|
|
self.tr('Log execution commands'), True))
|
2014-12-19 15:01:02 +01:00
|
|
|
ProcessingConfig.addSetting(Setting("SAGA",
|
2015-08-22 14:29:41 +02:00
|
|
|
SagaUtils.SAGA_LOG_CONSOLE,
|
|
|
|
self.tr('Log console output'), True))
|
2017-04-04 13:28:44 +10:00
|
|
|
ProcessingConfig.readSettings()
|
|
|
|
self.refreshAlgorithms()
|
2017-04-04 09:46:46 +10:00
|
|
|
return True
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def unload(self):
|
2017-04-04 12:22:29 +10:00
|
|
|
ProcessingConfig.removeSetting('ACTIVATE_SAGA')
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_CONSOLE)
|
|
|
|
ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_COMMANDS)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 12:22:29 +10:00
|
|
|
def isActive(self):
|
|
|
|
return ProcessingConfig.getSetting('ACTIVATE_SAGA')
|
|
|
|
|
|
|
|
def setActive(self, active):
|
|
|
|
ProcessingConfig.setSettingValue('ACTIVATE_SAGA', active)
|
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def loadAlgorithms(self):
|
2016-12-29 15:16:37 +02:00
|
|
|
version = SagaUtils.getInstalledVersion(True)
|
2014-12-19 15:01:02 +01:00
|
|
|
if version is None:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Problem with SAGA installation: SAGA was not found or is not correctly installed'),
|
|
|
|
self.tr('Processing'), QgsMessageLog.CRITICAL)
|
2014-12-19 15:01:02 +01:00
|
|
|
return
|
2017-02-08 16:33:20 +02:00
|
|
|
|
2017-02-14 12:55:33 +02:00
|
|
|
if not version.startswith('2.3.'):
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Problem with SAGA installation: unsupported SAGA version found.'),
|
|
|
|
self.tr('Processing'),
|
|
|
|
QgsMessageLog.CRITICAL)
|
2017-02-08 16:33:20 +02:00
|
|
|
return
|
2014-12-19 15:01:02 +01:00
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2012-09-15 18:25:25 +03:00
|
|
|
folder = SagaUtils.sagaDescriptionPath()
|
|
|
|
for descriptionFile in os.listdir(folder):
|
2013-10-01 20:52:22 +03:00
|
|
|
if descriptionFile.endswith('txt'):
|
2017-02-08 16:33:20 +02:00
|
|
|
try:
|
|
|
|
alg = SagaAlgorithm(os.path.join(folder, descriptionFile))
|
2017-03-29 12:51:59 +10:00
|
|
|
if alg.name().strip() != '':
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs.append(alg)
|
2017-02-08 16:33:20 +02:00
|
|
|
else:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Could not open SAGA algorithm: {}'.format(descriptionFile)),
|
|
|
|
self.tr('Processing'), QgsMessageLog.CRITICAL)
|
2017-02-08 16:33:20 +02:00
|
|
|
except Exception as e:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Could not open SAGA algorithm: {}\n{}'.format(descriptionFile, str(e))),
|
|
|
|
self.tr('Processing'), QgsMessageLog.CRITICAL)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs.append(SplitRGBBands())
|
|
|
|
for a in self.algs:
|
|
|
|
self.addAlgorithm(a)
|
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
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def name(self):
|
2016-12-29 15:16:37 +02:00
|
|
|
version = SagaUtils.getInstalledVersion()
|
|
|
|
return 'SAGA ({})'.format(version) if version is not None else 'SAGA'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def id(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return 'saga'
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2017-04-03 15:37:55 +10:00
|
|
|
def supportedOutputVectorLayerExtensions(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return ['shp']
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2017-04-03 15:37:55 +10:00
|
|
|
def supportedOutputRasterLayerExtensions(self):
|
2015-06-22 09:10:01 +02:00
|
|
|
return ['sdat']
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2013-02-06 21:00:26 +01:00
|
|
|
def getSupportedOutputTableLayerExtensions(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return ['dbf']
|
2013-02-06 21:00:26 +01:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def icon(self):
|
2015-05-18 19:51:26 +03:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'saga.png'))
|
2017-04-04 11:38:49 +10:00
|
|
|
|
|
|
|
def tr(self, string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = 'SagaAlgorithmProvider'
|
|
|
|
return QCoreApplication.translate(context, string)
|