PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/qgis/Quantum-GIS
Python | 166 lines | 130 code | 15 blank | 21 comment | 8 complexity | d8652392212b211cd6a21cdb541ab702 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, GPL-2.0, CC-BY-SA-3.0, MIT, 0BSD, BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. """
  3. ***************************************************************************
  4. SagaAlgorithmProvider.py
  5. ---------------------
  6. Date : August 2012
  7. Copyright : (C) 2012 by Victor Olaya
  8. Email : volayaf at gmail dot com
  9. ***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************
  17. """
  18. __author__ = 'Victor Olaya'
  19. __date__ = 'August 2012'
  20. __copyright__ = '(C) 2012, Victor Olaya'
  21. import os
  22. from qgis.PyQt.QtGui import QIcon
  23. from qgis.PyQt.QtCore import QCoreApplication
  24. from qgis.core import (Qgis,
  25. QgsProcessingProvider,
  26. QgsProcessingUtils,
  27. QgsApplication,
  28. QgsMessageLog)
  29. from processing.core.ProcessingConfig import ProcessingConfig, Setting
  30. from processing.tools.system import isWindows, isMac
  31. from .SagaAlgorithm import SagaAlgorithm
  32. from .SplitRGBBands import SplitRGBBands
  33. from . import SagaUtils
  34. pluginPath = os.path.normpath(os.path.join(
  35. os.path.split(os.path.dirname(__file__))[0], os.pardir))
  36. REQUIRED_VERSION = '2.3.'
  37. BETA_SUPPORT_VERSION = '7.3.'
  38. class SagaAlgorithmProvider(QgsProcessingProvider):
  39. def __init__(self):
  40. super().__init__()
  41. self.algs = []
  42. def load(self):
  43. ProcessingConfig.settingIcons[self.name()] = self.icon()
  44. ProcessingConfig.addSetting(Setting("SAGA", 'ACTIVATE_SAGA',
  45. self.tr('Activate'), True))
  46. ProcessingConfig.addSetting(Setting("SAGA",
  47. SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION,
  48. self.tr('Enable SAGA Import/Export optimizations'), False))
  49. ProcessingConfig.addSetting(Setting("SAGA",
  50. SagaUtils.SAGA_LOG_COMMANDS,
  51. self.tr('Log execution commands'), True))
  52. ProcessingConfig.addSetting(Setting("SAGA",
  53. SagaUtils.SAGA_LOG_CONSOLE,
  54. self.tr('Log console output'), True))
  55. ProcessingConfig.readSettings()
  56. self.refreshAlgorithms()
  57. return True
  58. def unload(self):
  59. ProcessingConfig.removeSetting('ACTIVATE_SAGA')
  60. ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_CONSOLE)
  61. ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_COMMANDS)
  62. def isActive(self):
  63. return ProcessingConfig.getSetting('ACTIVATE_SAGA')
  64. def setActive(self, active):
  65. ProcessingConfig.setSettingValue('ACTIVATE_SAGA', active)
  66. def canBeActivated(self):
  67. version = SagaUtils.getInstalledVersion(True)
  68. if version is not None and (version.startswith(REQUIRED_VERSION) or version.startswith(BETA_SUPPORT_VERSION)):
  69. return True
  70. return False
  71. def warningMessage(self):
  72. version = SagaUtils.getInstalledVersion(True)
  73. if version is not None and version.startswith(BETA_SUPPORT_VERSION):
  74. return self.tr('SAGA version {} is not officially supported - algorithms may encounter issues').format(version)
  75. return ''
  76. def loadAlgorithms(self):
  77. version = SagaUtils.getInstalledVersion(True)
  78. if version is None:
  79. QgsMessageLog.logMessage(self.tr('Problem with SAGA installation: SAGA was not found or is not correctly installed'),
  80. self.tr('Processing'), Qgis.Critical)
  81. return
  82. if not version.startswith(REQUIRED_VERSION) and not version.startswith(BETA_SUPPORT_VERSION):
  83. QgsMessageLog.logMessage(self.tr('Problem with SAGA installation: unsupported SAGA version (found: {}, required: {}).').format(version, REQUIRED_VERSION),
  84. self.tr('Processing'),
  85. Qgis.Critical)
  86. return
  87. self.algs = []
  88. folder = SagaUtils.sagaDescriptionPath()
  89. for descriptionFile in os.listdir(folder):
  90. if descriptionFile.endswith('txt'):
  91. try:
  92. alg = SagaAlgorithm(os.path.join(folder, descriptionFile))
  93. if alg.name().strip() != '':
  94. self.algs.append(alg)
  95. else:
  96. QgsMessageLog.logMessage(self.tr('Could not open SAGA algorithm: {}'.format(descriptionFile)),
  97. self.tr('Processing'), Qgis.Critical)
  98. except Exception as e:
  99. QgsMessageLog.logMessage(self.tr('Could not open SAGA algorithm: {}\n{}'.format(descriptionFile, str(e))),
  100. self.tr('Processing'), Qgis.Critical)
  101. self.algs.append(SplitRGBBands())
  102. for a in self.algs:
  103. self.addAlgorithm(a)
  104. def name(self):
  105. return 'SAGA'
  106. def longName(self):
  107. version = SagaUtils.getInstalledVersion()
  108. return 'SAGA ({})'.format(version) if version is not None else 'SAGA'
  109. def id(self):
  110. return 'saga'
  111. def defaultVectorFileExtension(self, hasGeometry=True):
  112. return 'shp' if hasGeometry else 'dbf'
  113. def defaultRasterFileExtension(self):
  114. return 'sdat'
  115. def supportedOutputRasterLayerExtensions(self):
  116. return ['sdat']
  117. def supportedOutputVectorLayerExtensions(self):
  118. return ['shp', 'dbf']
  119. def supportedOutputTableExtensions(self):
  120. return ['dbf']
  121. def flags(self):
  122. # push users towards alternative algorithms instead, SAGA algorithms should only be used by experienced
  123. # users who understand and can workaround the frequent issues encountered here
  124. return QgsProcessingProvider.FlagDeemphasiseSearchResults
  125. def supportsNonFileBasedOutput(self):
  126. """
  127. SAGA Provider doesn't support non file based outputs
  128. """
  129. return False
  130. def icon(self):
  131. return QgsApplication.getThemeIcon("/providerSaga.svg")
  132. def tr(self, string, context=''):
  133. if context == '':
  134. context = 'SagaAlgorithmProvider'
  135. return QCoreApplication.translate(context, string)