2019-01-08 17:42:00 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2019-01-21 12:05:09 +01:00
|
|
|
OtbAlgorithmsTest.py
|
2019-01-08 17:42:00 +01:00
|
|
|
---------------------
|
|
|
|
Date : January 2019
|
|
|
|
Copyright : (C) 2019 by CNES
|
|
|
|
Author : otb att cnes dot fr
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Rashad Kanavath'
|
|
|
|
__date__ = 'Janauary 2019'
|
|
|
|
__copyright__ = '(C) 2019, CNES'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = ':%H$'
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import hashlib
|
|
|
|
import shutil
|
|
|
|
import nose2
|
|
|
|
from qgis.core import (QgsProcessingParameterNumber,
|
2019-01-10 11:41:13 +01:00
|
|
|
QgsApplication,
|
2019-01-08 17:42:00 +01:00
|
|
|
QgsProcessingParameterDefinition)
|
|
|
|
from qgis.testing import start_app, unittest
|
2019-01-21 12:05:09 +01:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
|
|
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
|
|
|
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
|
|
|
|
from processing.gui.wrappers import *
|
|
|
|
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
|
2019-01-08 17:42:00 +01:00
|
|
|
from processing.algs.otb.OtbAlgorithm import OtbAlgorithm
|
|
|
|
from processing.algs.otb.OtbAlgorithmProvider import OtbAlgorithmProvider
|
|
|
|
from processing.algs.otb.OtbSettings import OtbSettings
|
2019-01-21 12:05:09 +01:00
|
|
|
from processing.algs.otb.OtbChoiceWidget import OtbParameterChoice, OtbChoiceWidgetWrapper
|
2019-01-08 17:42:00 +01:00
|
|
|
import AlgorithmsTestBase
|
|
|
|
|
|
|
|
OTB_INSTALL_DIR = os.environ.get('OTB_INSTALL_DIR')
|
|
|
|
|
|
|
|
|
|
|
|
class TestOtbAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
|
|
|
|
|
|
|
|
def test_init_algorithms(self):
|
|
|
|
algs_txt = os.path.join(self.descrFolder, 'algs.txt')
|
|
|
|
with open(algs_txt) as lines:
|
|
|
|
line = lines.readline().strip('\n').strip()
|
|
|
|
if line != '' and line.startswith('#'):
|
|
|
|
version = line[1:]
|
|
|
|
print('version =', version)
|
|
|
|
line = lines.readline().strip('\n').strip()
|
|
|
|
while line != '' and not line.startswith('#'):
|
|
|
|
data = line.split('|')
|
|
|
|
descriptionFile = os.path.join(self.descrFolder, str(data[1]) + '.txt')
|
|
|
|
alg = OtbAlgorithm(data[0], data[1], descriptionFile)
|
2019-01-10 11:41:13 +01:00
|
|
|
self.assertIsInstance(alg, OtbAlgorithm)
|
2019-01-08 17:42:00 +01:00
|
|
|
ret, msg = alg.canExecute()
|
2019-01-10 11:41:13 +01:00
|
|
|
print("canExecute '{}' - {}".format(alg.id(), ret))
|
2019-01-08 17:42:00 +01:00
|
|
|
self.assertEqual(ret, True)
|
2019-01-10 11:41:13 +01:00
|
|
|
line = lines.readline().strip('\n').strip()
|
2019-01-08 17:42:00 +01:00
|
|
|
|
2019-01-21 12:05:09 +01:00
|
|
|
def test_OTBParameterChoice(self):
|
2019-01-08 17:42:00 +01:00
|
|
|
alg_smoothing = OtbAlgorithm('Image Filtering', 'Smoothing', os.path.join(self.descrFolder, 'Smoothing.txt'))
|
|
|
|
found = False
|
|
|
|
for param in alg_smoothing.parameterDefinitions():
|
|
|
|
## print (param.name(), param.type())
|
|
|
|
if param.type() == 'OTBParameterChoice':
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
self.assertEqual(found, True)
|
|
|
|
|
2019-01-21 12:05:09 +01:00
|
|
|
def test_OTBParameterChoice_Gui(self):
|
|
|
|
param = OtbParameterChoice('test')
|
|
|
|
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
|
|
|
|
# algorithm dialog
|
|
|
|
dlg = AlgorithmDialog(alg)
|
|
|
|
wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
|
|
|
|
self.assertIsNotNone(wrapper)
|
|
|
|
self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
|
|
|
|
self.assertEqual(wrapper.dialog, dlg)
|
|
|
|
self.assertIsNotNone(wrapper.widget)
|
|
|
|
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
|
|
|
|
# batch dialog
|
|
|
|
dlg = BatchAlgorithmDialog(alg)
|
|
|
|
wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
|
|
|
|
self.assertIsNotNone(wrapper)
|
|
|
|
self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
|
|
|
|
self.assertEqual(wrapper.dialog, dlg)
|
|
|
|
self.assertIsNotNone(wrapper.widget)
|
|
|
|
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
|
|
|
|
# modeler dialog
|
|
|
|
model = QgsProcessingModelAlgorithm()
|
|
|
|
dlg = ModelerParametersDialog(alg, model)
|
|
|
|
wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
|
|
|
|
self.assertIsNotNone(wrapper)
|
|
|
|
self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
|
|
|
|
self.assertEqual(wrapper.dialog, dlg)
|
|
|
|
self.assertIsNotNone(wrapper.widget)
|
|
|
|
|
2019-01-08 17:42:00 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
start_app()
|
|
|
|
from processing.core.Processing import Processing
|
|
|
|
Processing.initialize()
|
2019-01-10 11:41:13 +01:00
|
|
|
ProcessingConfig.setSettingValue("OTB_ACTIVATE", True)
|
2019-01-08 17:42:00 +01:00
|
|
|
ProcessingConfig.setSettingValue(OtbSettings.FOLDER, OTB_INSTALL_DIR)
|
|
|
|
ProcessingConfig.setSettingValue(OtbSettings.APP_FOLDER, os.path.join(OTB_INSTALL_DIR, 'lib', 'otb', 'applications'))
|
|
|
|
ProcessingConfig.readSettings()
|
2019-01-21 12:05:09 +01:00
|
|
|
# Refresh OTB Algorithms after settings are changed.
|
2019-01-10 11:41:13 +01:00
|
|
|
for p in QgsApplication.processingRegistry().providers():
|
|
|
|
if p.id() == "otb":
|
|
|
|
p.refreshAlgorithms()
|
2019-01-21 12:05:09 +01:00
|
|
|
cls.descrFolder = os.path.join(OTB_INSTALL_DIR, 'share', 'otb', 'description')
|
2019-01-08 17:42:00 +01:00
|
|
|
cls.cleanup_paths = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
from processing.core.Processing import Processing
|
|
|
|
Processing.deinitialize()
|
|
|
|
for path in cls.cleanup_paths:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
def test_definition_file(self):
|
2019-01-10 11:41:13 +01:00
|
|
|
print("OTB_INSTALL_DIR = '{}'".format(OTB_INSTALL_DIR))
|
2019-01-08 17:42:00 +01:00
|
|
|
return 'otb_algorithm_tests.yaml'
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
nose2.main()
|