OtbAlgorithmsTest.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """
  2. ***************************************************************************
  3. OtbAlgorithmsTest.py
  4. ---------------------
  5. Date : January 2019
  6. Copyright : (C) 2019 by CNES
  7. Author : otb att cnes dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Rashad Kanavath'
  18. __date__ = 'Janauary 2019'
  19. __copyright__ = '(C) 2019, CNES'
  20. import os
  21. import shutil
  22. import nose2
  23. import tempfile
  24. from qgis.core import (QgsProcessingParameterNumber,
  25. QgsApplication,
  26. QgsCoordinateReferenceSystem,
  27. QgsRasterLayer,
  28. QgsMapLayer,
  29. QgsProject,
  30. QgsProcessingContext,
  31. QgsProcessingUtils,
  32. QgsProcessingFeedback,
  33. QgsProcessingParameterDefinition,
  34. QgsProcessingModelAlgorithm)
  35. import unittest
  36. from qgis.testing import start_app, QgisTestCase
  37. from processing.core.ProcessingConfig import ProcessingConfig, Setting
  38. from processing.gui.AlgorithmDialog import AlgorithmDialog
  39. from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
  40. from processing.gui.wrappers import WidgetWrapperFactory
  41. from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
  42. from otbprovider.OtbAlgorithm import OtbAlgorithm
  43. from otbprovider.OtbAlgorithmProvider import OtbAlgorithmProvider
  44. from otbprovider.OtbUtils import OtbUtils
  45. from otbprovider.OtbChoiceWidget import OtbParameterChoice, OtbChoiceWidgetWrapper
  46. import AlgorithmsTestBase
  47. import processing
  48. OTB_INSTALL_DIR = os.environ.get('OTB_INSTALL_DIR')
  49. class TestOtbAlgorithms(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
  50. @staticmethod
  51. def __input_raster_layer():
  52. options = QgsRasterLayer.LayerOptions()
  53. options.loadDefaultStyle = False
  54. return QgsRasterLayer(os.path.join(AlgorithmsTestBase.processingTestDataPath(), 'raster.tif'),
  55. "raster_input",
  56. 'gdal',
  57. options)
  58. def test_bug21373_mode_vector(self):
  59. """
  60. This issue is reported on qgis bug tracker: #21373
  61. This issue is reported on qgis-otb-plugin tracker: #30
  62. """
  63. context = QgsProcessingContext()
  64. context.setProject(QgsProject.instance())
  65. feedback = QgsProcessingFeedback()
  66. parameters = {
  67. 'in': TestOtbAlgorithms.__input_raster_layer(),
  68. 'filter': 'meanshift',
  69. 'mode.vector.out': 'vector.shp'
  70. }
  71. alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
  72. results = alg.processAlgorithm(parameters, context, feedback)
  73. self.assertDictEqual(results, {'mode.vector.out': 'vector.shp'})
  74. def test_bug21373_mode_raster(self):
  75. """
  76. This issue is reported on qgis bug tracker: #21373
  77. """
  78. context = QgsProcessingContext()
  79. context.setProject(QgsProject.instance())
  80. feedback = QgsProcessingFeedback()
  81. parameters = {
  82. 'in': TestOtbAlgorithms.__input_raster_layer(),
  83. 'filter': 'meanshift',
  84. 'mode': 'raster',
  85. 'mode.raster.out': 'raster.tif'
  86. }
  87. alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
  88. results = alg.processAlgorithm(parameters, context, feedback)
  89. self.assertDictEqual(results, {'mode.raster.out': 'raster.tif'})
  90. def test_bug21374_Fail(self):
  91. """
  92. This issue is reported on qgis bug tracker: #21374
  93. """
  94. outdir = tempfile.mkdtemp()
  95. self.cleanup_paths.append(outdir)
  96. context = QgsProcessingContext()
  97. context.setProject(QgsProject.instance())
  98. feedback = QgsProcessingFeedback()
  99. parameters = {
  100. 'in': TestOtbAlgorithms.__input_raster_layer(),
  101. 'filter': 'cc',
  102. 'mode.vector.out': os.path.join(outdir, 'vector.shp')
  103. }
  104. alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
  105. ok, msg = alg.checkParameterValues(parameters, context)
  106. self.assertFalse(ok, 'Algorithm failed checkParameterValues with result {}'.format(msg))
  107. def test_init_algorithms(self):
  108. """
  109. This test will read each otb algorithm in 'algs.txt'
  110. and creates an instance of OtbAlgorithm and check if it can be executed
  111. This is done in :class: `OtbAlgorithmProvider` load() method
  112. """
  113. algs_txt = os.path.join(self.descrFolder, 'algs.txt')
  114. with open(algs_txt) as lines:
  115. line = lines.readline().strip('\n').strip()
  116. if line != '' and line.startswith('#'):
  117. version = line[1:]
  118. print('version =', version)
  119. line = lines.readline().strip('\n').strip()
  120. while line != '' and not line.startswith('#'):
  121. data = line.split('|')
  122. descriptionFile = os.path.join(self.descrFolder, str(data[1]) + '.txt')
  123. alg = OtbAlgorithm(data[0], data[1], descriptionFile)
  124. self.assertIsInstance(alg, OtbAlgorithm)
  125. ret, msg = alg.canExecute()
  126. print("canExecute '{}' - {}".format(alg.id(), ret))
  127. self.assertEqual(ret, True)
  128. line = lines.readline().strip('\n').strip()
  129. def test_parameterAs_ScriptMode(self):
  130. """
  131. This test will pass an instance of QgsCoordinateReferenceSystem for 'epsg' parameter
  132. of otb::Rasterization. There is same test in otb_algorithm_tests.yaml which passes
  133. an instance of str for epsg parameter.
  134. """
  135. outdir = tempfile.mkdtemp()
  136. self.cleanup_paths.append(outdir)
  137. context = QgsProcessingContext()
  138. context.setProject(QgsProject.instance())
  139. feedback = QgsProcessingFeedback()
  140. vectorFile = os.path.join(AlgorithmsTestBase.processingTestDataPath(), 'polys.gml')
  141. vectorLayer = QgsProcessingUtils.mapLayerFromString(vectorFile, context)
  142. parameters = {
  143. 'in': vectorLayer,
  144. 'epsg': QgsCoordinateReferenceSystem('EPSG:4326'),
  145. 'spx': 1.0,
  146. 'spy': 1.0,
  147. 'outputpixeltype': 1,
  148. 'out': os.path.join(outdir, 'raster.tif')
  149. }
  150. results = processing.run('otb:Rasterization', parameters, None, feedback)
  151. result_lyr = QgsProcessingUtils.mapLayerFromString(results['out'], context)
  152. self.assertTrue(result_lyr.isValid())
  153. def test_OTBParameterChoiceExists(self):
  154. """
  155. This test is here to know if we have change `type()` method of :class: `OtbParameterChoice`
  156. That value is used by Otb when it creates descriptor files. So changes to this string must be test
  157. in a unit-test.
  158. """
  159. alg_smoothing = OtbAlgorithm('Image Filtering', 'Smoothing', os.path.join(self.descrFolder, 'Smoothing.txt'))
  160. found = False
  161. for param in alg_smoothing.parameterDefinitions():
  162. # print (param.name(), param.type())
  163. if param.type() == 'OTBParameterChoice':
  164. found = True
  165. break
  166. self.assertEqual(found, True)
  167. def test_OTBParameterChoice_Gui(self):
  168. """
  169. This test is similar to GuiTests in processing that is done on other parameter widget in processing
  170. Main difference is this test uses create_wrapper_from_metadata() rather than create_wrapper_from_class()
  171. like rest of processing widgets.
  172. """
  173. param = OtbParameterChoice('test')
  174. alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
  175. # algorithm dialog
  176. dlg = AlgorithmDialog(alg)
  177. wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
  178. self.assertIsNotNone(wrapper)
  179. self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
  180. self.assertEqual(wrapper.dialog, dlg)
  181. self.assertIsNotNone(wrapper.widget)
  182. alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
  183. # batch dialog
  184. dlg = BatchAlgorithmDialog(alg)
  185. wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
  186. self.assertIsNotNone(wrapper)
  187. self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
  188. self.assertEqual(wrapper.dialog, dlg)
  189. self.assertIsNotNone(wrapper.widget)
  190. alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
  191. # modeler dialog
  192. model = QgsProcessingModelAlgorithm()
  193. dlg = ModelerParametersDialog(alg, model)
  194. wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg)
  195. self.assertIsNotNone(wrapper)
  196. self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper)
  197. self.assertEqual(wrapper.dialog, dlg)
  198. self.assertIsNotNone(wrapper.widget)
  199. @classmethod
  200. def setUpClass(cls):
  201. start_app()
  202. cls.provider = OtbAlgorithmProvider()
  203. QgsApplication.processingRegistry().addProvider(cls.provider)
  204. ProcessingConfig.setSettingValue(OtbUtils.FOLDER, OTB_INSTALL_DIR)
  205. ProcessingConfig.setSettingValue(OtbUtils.APP_FOLDER, os.path.join(OTB_INSTALL_DIR, 'lib', 'otb', 'applications'))
  206. ProcessingConfig.readSettings()
  207. # Refresh OTB Algorithms after settings are changed.
  208. for p in QgsApplication.processingRegistry().providers():
  209. if p.id() == "otb":
  210. p.refreshAlgorithms()
  211. cls.descrFolder = os.path.join(OTB_INSTALL_DIR, 'share', 'otb', 'description')
  212. cls.cleanup_paths = []
  213. @classmethod
  214. def tearDownClass(cls):
  215. QgsApplication.processingRegistry().removeProvider(cls.provider)
  216. for path in cls.cleanup_paths:
  217. shutil.rmtree(path)
  218. def test_definition_file(self):
  219. """
  220. return name of yaml file containing test definitions
  221. """
  222. print("OTB_INSTALL_DIR = '{}'".format(OTB_INSTALL_DIR))
  223. return 'otb_algorithm_tests.yaml'
  224. if __name__ == '__main__':
  225. nose2.main()