Remove OtbSettings class and put constants for key names in OtbUtils

This commit is contained in:
Rashad Kanavath 2019-03-06 04:54:21 +01:00 committed by Nyall Dawson
parent 07d17a0f17
commit d3a1c65368
4 changed files with 57 additions and 84 deletions

View File

@ -36,7 +36,6 @@ from qgis import utils
from processing.core.ProcessingConfig import ProcessingConfig, Setting from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.algs.otb.OtbUtils import OtbUtils from processing.algs.otb.OtbUtils import OtbUtils
from processing.algs.otb.OtbSettings import OtbSettings
from processing.algs.otb.OtbAlgorithm import OtbAlgorithm from processing.algs.otb.OtbAlgorithm import OtbAlgorithm
class OtbAlgorithmProvider(QgsProcessingProvider): class OtbAlgorithmProvider(QgsProcessingProvider):
@ -50,35 +49,35 @@ class OtbAlgorithmProvider(QgsProcessingProvider):
def load(self): def load(self):
group = self.name() group = self.name()
ProcessingConfig.settingIcons[group] = self.icon() ProcessingConfig.settingIcons[group] = self.icon()
ProcessingConfig.addSetting(Setting(group, OtbSettings.ACTIVATE, self.tr('Activate'), True)) ProcessingConfig.addSetting(Setting(group, OtbUtils.ACTIVATE, self.tr('Activate'), True))
ProcessingConfig.addSetting(Setting(group, OtbSettings.FOLDER, ProcessingConfig.addSetting(Setting(group, OtbUtils.FOLDER,
self.tr("OTB folder"), self.tr("OTB folder"),
OtbUtils.otbFolder(), OtbUtils.otbFolder(),
valuetype=Setting.FOLDER, valuetype=Setting.FOLDER,
validator=self.validateOtbFolder validator=self.validateOtbFolder
)) ))
ProcessingConfig.addSetting(Setting(group, OtbSettings.APP_FOLDER, ProcessingConfig.addSetting(Setting(group, OtbUtils.APP_FOLDER,
self.tr("OTB application folder"), self.tr("OTB application folder"),
OtbUtils.appFolder(), OtbUtils.appFolder(),
valuetype=Setting.MULTIPLE_FOLDERS, valuetype=Setting.MULTIPLE_FOLDERS,
validator=self.validateAppFolders validator=self.validateAppFolders
)) ))
ProcessingConfig.addSetting(Setting(group, OtbSettings.SRTM_FOLDER, ProcessingConfig.addSetting(Setting(group, OtbUtils.SRTM_FOLDER,
self.tr("SRTM tiles folder"), self.tr("SRTM tiles folder"),
OtbUtils.srtmFolder(), OtbUtils.srtmFolder(),
valuetype=Setting.FOLDER valuetype=Setting.FOLDER
)) ))
ProcessingConfig.addSetting(Setting(group, OtbSettings.GEOID_FILE, ProcessingConfig.addSetting(Setting(group, OtbUtils.GEOID_FILE,
self.tr("Geoid file"), self.tr("Geoid file"),
OtbUtils.geoidFile(), OtbUtils.geoidFile(),
valuetype=Setting.FOLDER valuetype=Setting.FOLDER
)) ))
ProcessingConfig.addSetting(Setting(group, OtbSettings.MAX_RAM_HINT, ProcessingConfig.addSetting(Setting(group, OtbUtils.MAX_RAM_HINT,
self.tr("Maximum RAM to use"), self.tr("Maximum RAM to use"),
OtbUtils.maxRAMHint(), OtbUtils.maxRAMHint(),
valuetype=Setting.STRING valuetype=Setting.STRING
)) ))
ProcessingConfig.addSetting(Setting(group, OtbSettings.LOGGER_LEVEL, ProcessingConfig.addSetting(Setting(group, OtbUtils.LOGGER_LEVEL,
self.tr("Logger level"), self.tr("Logger level"),
OtbUtils.loggerLevel(), OtbUtils.loggerLevel(),
valuetype=Setting.STRING, valuetype=Setting.STRING,
@ -89,14 +88,14 @@ class OtbAlgorithmProvider(QgsProcessingProvider):
return True return True
def unload(self): def unload(self):
for setting in OtbSettings.keys(): for setting in OtbUtils.settingNames():
ProcessingConfig.removeSetting(setting) ProcessingConfig.removeSetting(setting)
def isActive(self): def isActive(self):
return ProcessingConfig.getSetting(OtbSettings.ACTIVATE) return ProcessingConfig.getSetting(OtbUtils.ACTIVATE)
def setActive(self, active): def setActive(self, active):
ProcessingConfig.setSettingValue(OtbSettings.ACTIVATE, active) ProcessingConfig.setSettingValue(OtbUtils.ACTIVATE, active)
def createAlgsList(self): def createAlgsList(self):
algs = [] algs = []

View File

@ -1,60 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
from builtins import str
__author__ = 'Rashad Kanavath'
__date__ = 'January 2019'
__copyright__ = '(C) CNES 2019'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
class OtbSettings(object):
"""
OtbSetting's key names
"""
# Checkbox to enable/disable otb provider (bool).
ACTIVATE = "OTB_ACTIVATE"
# Path to otb installation folder (string, directory).
FOLDER = "OTB_FOLDER"
# Path to otb application folder. multiple paths are supported (string, directory).
APP_FOLDER = "OTB_APP_FOLDER"
# A string to hold current version number. Useful for bug reporting.
VERSION = "OTB_VERSION"
# Default directory were DEM tiles are stored. It should only contain ```.hgt`` or or georeferenced ``.tif`` files. Empty if not set (no directory set).
SRTM_FOLDER = "OTB_SRTM_FOLDER"
# Default path to the geoid file that will be used to retrieve height of DEM above ellipsoid. Empty if not set (no geoid set).
GEOID_FILE = "OTB_GEOID_FILE"
# Default maximum memory that OTB should use for processing, in MB. If not set, default value is 128 MB.
# This is set through environment variable ``OTB_MAX_RAM_HINT``
MAX_RAM_HINT = 'OTB_MAX_RAM_HINT'
# ``OTB_LOGGER_LEVEL``: Default level of logging for OTB. Should be one of ``DEBUG``, ``INFO``, ``WARNING``, ``CRITICAL`` or ``FATAL``, by increasing order of priority. Only messages with a higher priority than the level of logging will be displayed. If not set, default level is ``INFO``.
LOGGER_LEVEL = 'OTB_LOGGER_LEVEL'
@staticmethod
def keys():
return [
OtbSettings.ACTIVATE,
OtbSettings.FOLDER,
OtbSettings.SRTM_FOLDER,
OtbSettings.GEOID_FILE,
OtbSettings.LOGGER_LEVEL,
OtbSettings.MAX_RAM_HINT
]

View File

@ -31,39 +31,73 @@ __copyright__ = '(C) 2012, Victor Olaya'
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'
import os import os
import sys
import re import re
import subprocess import subprocess
from processing.core.ProcessingConfig import ProcessingConfig from processing.core.ProcessingConfig import ProcessingConfig
from qgis.core import (Qgis, QgsApplication, QgsMessageLog) from qgis.core import (Qgis, QgsApplication, QgsMessageLog)
from qgis.PyQt.QtCore import QCoreApplication from qgis.PyQt.QtCore import QCoreApplication
from processing.algs.otb.OtbSettings import OtbSettings
class OtbUtils: class OtbUtils:
# Checkbox to enable/disable otb provider (bool).
ACTIVATE = "OTB_ACTIVATE"
# Path to otb installation folder (string, directory).
FOLDER = "OTB_FOLDER"
# Path to otb application folder. multiple paths are supported (string, directory).
APP_FOLDER = "OTB_APP_FOLDER"
# A string to hold current version number. Useful for bug reporting.
VERSION = "OTB_VERSION"
# Default directory were DEM tiles are stored. It should only contain ```.hgt`` or or georeferenced ``.tif`` files. Empty if not set (no directory set).
SRTM_FOLDER = "OTB_SRTM_FOLDER"
# Default path to the geoid file that will be used to retrieve height of DEM above ellipsoid. Empty if not set (no geoid set).
GEOID_FILE = "OTB_GEOID_FILE"
# Default maximum memory that OTB should use for processing, in MB. If not set, default value is 128 MB.
# This is set through environment variable ``OTB_MAX_RAM_HINT``
MAX_RAM_HINT = 'OTB_MAX_RAM_HINT'
# ``OTB_LOGGER_LEVEL``: Default level of logging for OTB. Should be one of ``DEBUG``, ``INFO``, ``WARNING``, ``CRITICAL`` or ``FATAL``, by increasing order of priority. Only messages with a higher priority than the level of logging will be displayed. If not set, default level is ``INFO``.
LOGGER_LEVEL = 'OTB_LOGGER_LEVEL'
@staticmethod
def settingNames():
return [
OtbUtils.ACTIVATE,
OtbUtils.FOLDER,
OtbUtils.SRTM_FOLDER,
OtbUtils.GEOID_FILE,
OtbUtils.LOGGER_LEVEL,
OtbUtils.MAX_RAM_HINT
]
@staticmethod @staticmethod
def version(): def version():
return ProcessingConfig.getSetting(OtbSettings.VERSION) or '0.0.0' return ProcessingConfig.getSetting(OtbUtils.VERSION) or '0.0.0'
@staticmethod @staticmethod
def loggerLevel(): def loggerLevel():
return ProcessingConfig.getSetting(OtbSettings.LOGGER_LEVEL) or 'INFO' return ProcessingConfig.getSetting(OtbUtils.LOGGER_LEVEL) or 'INFO'
@staticmethod @staticmethod
def maxRAMHint(): def maxRAMHint():
return ProcessingConfig.getSetting(OtbSettings.MAX_RAM_HINT) or '' return ProcessingConfig.getSetting(OtbUtils.MAX_RAM_HINT) or ''
@staticmethod @staticmethod
def otbFolder(): def otbFolder():
if ProcessingConfig.getSetting(OtbSettings.FOLDER): if ProcessingConfig.getSetting(OtbUtils.FOLDER):
return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OtbSettings.FOLDER)))) return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OtbUtils.FOLDER))))
else: else:
return None return None
@staticmethod @staticmethod
def appFolder(): def appFolder():
app_folder = ProcessingConfig.getSetting(OtbSettings.APP_FOLDER) app_folder = ProcessingConfig.getSetting(OtbUtils.APP_FOLDER)
if app_folder: if app_folder:
return os.pathsep.join(app_folder.split(';')) return os.pathsep.join(app_folder.split(';'))
else: else:
@ -71,11 +105,11 @@ class OtbUtils:
@staticmethod @staticmethod
def srtmFolder(): def srtmFolder():
return ProcessingConfig.getSetting(OtbSettings.SRTM_FOLDER) or '' return ProcessingConfig.getSetting(OtbUtils.SRTM_FOLDER) or ''
@staticmethod @staticmethod
def geoidFile(): def geoidFile():
return ProcessingConfig.getSetting(OtbSettings.GEOID_FILE) or '' return ProcessingConfig.getSetting(OtbUtils.GEOID_FILE) or ''
@staticmethod @staticmethod
def getExecutableInPath(path, exe): def getExecutableInPath(path, exe):

View File

@ -48,7 +48,7 @@ from processing.gui.wrappers import *
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
from processing.algs.otb.OtbAlgorithm import OtbAlgorithm from processing.algs.otb.OtbAlgorithm import OtbAlgorithm
from processing.algs.otb.OtbAlgorithmProvider import OtbAlgorithmProvider from processing.algs.otb.OtbAlgorithmProvider import OtbAlgorithmProvider
from processing.algs.otb.OtbSettings import OtbSettings from processing.algs.otb.OtbUtils import OtbUtils
from processing.algs.otb.OtbChoiceWidget import OtbParameterChoice, OtbChoiceWidgetWrapper from processing.algs.otb.OtbChoiceWidget import OtbParameterChoice, OtbChoiceWidgetWrapper
import AlgorithmsTestBase import AlgorithmsTestBase
@ -165,8 +165,8 @@ class TestOtbAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
from processing.core.Processing import Processing from processing.core.Processing import Processing
Processing.initialize() Processing.initialize()
ProcessingConfig.setSettingValue("OTB_ACTIVATE", True) ProcessingConfig.setSettingValue("OTB_ACTIVATE", True)
ProcessingConfig.setSettingValue(OtbSettings.FOLDER, OTB_INSTALL_DIR) ProcessingConfig.setSettingValue(OtbUtils.FOLDER, OTB_INSTALL_DIR)
ProcessingConfig.setSettingValue(OtbSettings.APP_FOLDER, os.path.join(OTB_INSTALL_DIR, 'lib', 'otb', 'applications')) ProcessingConfig.setSettingValue(OtbUtils.APP_FOLDER, os.path.join(OTB_INSTALL_DIR, 'lib', 'otb', 'applications'))
ProcessingConfig.readSettings() ProcessingConfig.readSettings()
# Refresh OTB Algorithms after settings are changed. # Refresh OTB Algorithms after settings are changed.
for p in QgsApplication.processingRegistry().providers(): for p in QgsApplication.processingRegistry().providers():