Skip to content

Commit

Permalink
[TEST] add test for OTB processing provider
Browse files Browse the repository at this point in the history
This includes yaml test like SAGA, GRASS and also test for loading
OTB Algorithms
  • Loading branch information
Rashad Kanavath authored and nyalldawson committed Feb 22, 2019
1 parent 3b87f7b commit 30bcfeb
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/plugins/processing/tests/CMakeLists.txt
Expand Up @@ -17,6 +17,7 @@ IF(ENABLE_TESTS)
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsImageryTest Grass7AlgorithmsImageryTest.py)
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsRasterTest Grass7AlgorithmsRasterTest.py)
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsVectorTest Grass7AlgorithmsVectorTest.py)
ADD_PYTHON_TEST(ProcessingOtbAlgorithmsTest OtbAlgorithmsTest.py)
ADD_PYTHON_TEST(ProcessingSagaAlgorithmsTest SagaAlgorithmsTest.py)
ADD_PYTHON_TEST(ProcessingCheckValidityAlgorithmTest CheckValidityAlgorithm.py)
ENDIF(ENABLE_TESTS)
109 changes: 109 additions & 0 deletions python/plugins/processing/tests/OtbAlgorithmsTest.py
@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
OtbAlgorithmsTests.py
---------------------
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,
QgsProcessingParameterDefinition)
from qgis.testing import start_app, unittest
#from processing.algs.otb.OtbChoiceWidget import OtbParameterChoice
from processing.algs.otb.OtbAlgorithm import OtbAlgorithm
from processing.algs.otb.OtbAlgorithmProvider import OtbAlgorithmProvider
from processing.algs.otb.OtbSettings import OtbSettings
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.tools import dataobjects
import AlgorithmsTestBase

#export QGIS_DISABLE_MESSAGE_HOOKS=1
#sys.path.append('/home/rashad/projects/qgis/qgis/build/output/python')
#sys.path.append('/home/rashad/projects/qgis/qgis/build/output/python/plugins')
#sys.path.append('/home/rashad/projects/qgis/otb-plugin')

# /home/rashad/projects/otb/gitlab/build"
OTB_INSTALL_DIR = os.environ.get('OTB_INSTALL_DIR')


class TestOtbAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):

#algList = []
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)
print("Loading Algorithm: '{}' - OK".format(alg.id()))
ret, msg = alg.canExecute()
line = lines.readline().strip('\n').strip()
self.assertEqual(ret, True)

def test_choice_parameter_smoothing(self):
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)

@classmethod
def setUpClass(cls):
start_app()
cls.descrFolder = os.path.join(OTB_INSTALL_DIR, 'share', 'otb', 'description')
from processing.core.Processing import Processing
Processing.initialize()
ProcessingConfig.setSettingValue(OtbSettings.FOLDER, OTB_INSTALL_DIR)
ProcessingConfig.setSettingValue(OtbSettings.APP_FOLDER, os.path.join(OTB_INSTALL_DIR, 'lib', 'otb', 'applications'))
ProcessingConfig.setSettingValue("OTB_ACTIVATE", True)
ProcessingConfig.readSettings()
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):
return 'otb_algorithm_tests.yaml'


if __name__ == '__main__':
nose2.main()
52 changes: 52 additions & 0 deletions python/plugins/processing/tests/testdata/otb_algorithm_tests.yaml
@@ -0,0 +1,52 @@
# See ../README.md for a description of the file format

tests:
- algorithm: otb:Smoothing
name: Test (otb:Smoothing anidif)
params:
in:
name: raster.tif
type: file
type: anidif
type.anidif.conductance: 2.0
type.anidif.nbiter: 10
type.anidif.timestep: 0.125
results:
out:
hash: 481539d39d92ed9a63c493235a4696be79d82f25608f190ddedb4cdf
type: rasterhash

- algorithm: otb:Smoothing
name: Test (otb:Smoothing mean)
params:
in:
name: raster.tif
type: file
type: gaussian
type.gaussian.radius: 4.0
results:
out:
hash: b3fbccd6f41052317a435567a2633dae1d9b66772a4d8a3323d9b1c5
type: rasterhash

- algorithm: otb:HaralickTextureExtraction
name: Test (otb:HaralickTextureExtraction)
params:
channel: 1
in:
name: raster with spaces.tif
type: file
parameters.max: 255.0
parameters.min: 0.0
parameters.nbbin: 8
parameters.xoff: 1
parameters.xrad: 2
parameters.yoff: 1
parameters.yrad: 2
step: 1
texture: simple
results:
out:
hash: 3132281993d8474d5404f01fb5afa68c9648c282da5546823281cc1c
type: rasterhash

0 comments on commit 30bcfeb

Please sign in to comment.