Пример #1
0
 def test_OTBParameterChoiceExists(self):
     """
     This test is here to know if we have change `type()` method of :class: `OtbParameterChoice`
     That value is used by Otb when it creates descriptor files. So changes to this string must be test
     in a unit-test.
     """
     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)
Пример #2
0
 def createAlgsList(self):
     algs = []
     try:
         folder = OtbUtils.otbFolder()
         alg_names = []
         algs_txt = self.algsFile(folder)
         with open(algs_txt) as lines:
             line = lines.readline().strip('\n').strip()
             if line != '' and line.startswith('#'):
                 line = lines.readline().strip('\n').strip()
             while line != '' and not line.startswith('#'):
                 data = line.split('|')
                 descriptionFile = self.descrFile(folder,
                                                  str(data[1]) + '.txt')
                 group, name = str(data[0]), str(data[1])
                 if name not in alg_names:
                     algs.append(OtbAlgorithm(group, name, descriptionFile))
                     #avoid duplicate algorithms from algs.txt file (possible but rare)
                     alg_names.append(name)
                 line = lines.readline().strip('\n').strip()
     except Exception as e:
         import traceback
         errmsg = "Could not open OTB algorithm from file: \n" + descriptionFile + "\nError:\n" + traceback.format_exc(
         )
         QgsMessageLog.logMessage(self.tr(errmsg), self.tr('Processing'),
                                  Qgis.Critical)
     return algs
Пример #3
0
 def test_bug21373_mode_raster(self):
     """
     This issue is reported on qgis bug tracker: #21373
     """
     context = QgsProcessingContext()
     context.setProject(QgsProject.instance())
     feedback = QgsProcessingFeedback()
     parameters = {
         'in': TestOtbAlgorithms.__input_raster_layer(),
         'filter': 'meanshift',
         'mode': 'raster',
         'mode.raster.out': 'raster.tif'
     }
     alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
     results = alg.processAlgorithm(parameters, context, feedback)
     self.assertDictEqual(results, {'mode.raster.out': 'raster.tif'})
Пример #4
0
 def test_bug21373_mode_raster(self):
     """
     This issue is reported on qgis bug tracker: #21373
     """
     context = QgsProcessingContext()
     context.setProject(QgsProject.instance())
     feedback = QgsProcessingFeedback()
     parameters = {
         'in': TestOtbAlgorithms.__input_raster_layer(),
         'filter': 'meanshift',
         'mode': 'raster',
         'mode.raster.out': 'raster.tif'
     }
     alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
     results = alg.processAlgorithm(parameters, context, feedback)
     self.assertDictEqual(results, {'mode.raster.out': 'raster.tif'})
Пример #5
0
    def test_bug21374_Fail(self):
        """
        This issue is reported on qgis bug tracker: #21374
        """
        outdir = tempfile.mkdtemp()
        self.cleanup_paths.append(outdir)
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = QgsProcessingFeedback()
        parameters = {
            'in': TestOtbAlgorithms.__input_raster_layer(),
            'filter': 'cc',
            'mode.vector.out': os.path.join(outdir, 'vector.shp')
        }

        alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
        ok, msg = alg.checkParameterValues(parameters, context)
        self.assertFalse(ok, 'Algorithm failed checkParameterValues with result {}'.format(msg))
Пример #6
0
    def test_bug21374_Fail(self):
        """
        This issue is reported on qgis bug tracker: #21374
        """
        outdir = tempfile.mkdtemp()
        self.cleanup_paths.append(outdir)
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = QgsProcessingFeedback()
        parameters = {
            'in': TestOtbAlgorithms.__input_raster_layer(),
            'filter': 'cc',
            'mode.vector.out': os.path.join(outdir, 'vector.shp')
        }

        alg = OtbAlgorithm('Segmentation', 'Segmentation', os.path.join(self.descrFolder, 'Segmentation.txt'))
        ok, msg = alg.checkParameterValues(parameters, context)
        self.assertFalse(ok, 'Algorithm failed checkParameterValues with result {}'.format(msg))
Пример #7
0
 def test_init_algorithms(self):
     """
     This test will read each otb algorithm in 'algs.txt'
     and creates an instance of OtbAlgorithm and check if it can be executed
     This is done in :class: `OtbAlgorithmProvider` load() method
     """
     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)
             self.assertIsInstance(alg, OtbAlgorithm)
             ret, msg = alg.canExecute()
             print("canExecute '{}' - {}".format(alg.id(), ret))
             self.assertEqual(ret, True)
             line = lines.readline().strip('\n').strip()