rename_bands_algorithm.py 4.85 KB
Newer Older
PIERSON Julie's avatar
PIERSON Julie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
# -*- coding: utf-8 -*-

"""
/***************************************************************************
 RenameBands
                                 A QGIS plugin
 Rename and reorder raster bands
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2021-09-09
        copyright            : (C) 2021 by Julie Pierson
        email                : julie.pierson@univ-brest.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__ = 'Julie Pierson'
__date__ = '2021-09-09'
__copyright__ = '(C) 2021 by Julie Pierson'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterRasterLayer,
                       QgsProcessingParameterRasterDestination)
import processing


class RenameBandsAlgorithm(QgsProcessingAlgorithm):
    """
    This is an algorithm that takes a raster layer and
    rename and/or reorder its bands.
    """

    # Constants used to refer to parameters and outputs. They will be
    # used when calling the algorithm from another algorithm, or when
    # calling from the QGIS console.

    OUTPUT = 'OUTPUT'
    INPUT = 'INPUT'

    def initAlgorithm(self, config):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # input raster layer
        self.addParameter(
            QgsProcessingParameterRasterLayer(
                self.INPUT,
                self.tr('Input raster layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        
        # TODO
        # output raster layer
        self.addParameter(
            QgsProcessingParameterRasterDestination(
                'OUTPUT',
                self.tr('Raster output')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

PIERSON Julie's avatar
PIERSON Julie committed
83
        # Retrieve inputs and outputs
PIERSON Julie's avatar
PIERSON Julie committed
84 85 86 87 88 89 90 91 92 93 94 95 96
        input_raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
        output_path_raster = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
        
        param_translate = {'INPUT': input_raster,
                           'TARGET_CRS':None,
                           'NODATA':None,
                           'COPY_SUBDATASETS':False,
                           'OPTIONS':'',
                           'EXTRA':'','DATA_TYPE':0,
                           'OUTPUT':output_path_raster
                           }
        processing.run("gdal:translate", param_translate)

PIERSON Julie's avatar
PIERSON Julie committed
97
        # Return the results of the algorithm as a dictionary
PIERSON Julie's avatar
PIERSON Julie committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        return {self.OUTPUT: output_path_raster}

    def name(self):
        """
        Returns the algorithm name, used for identifying the algorithm. This
        string should be fixed for the algorithm, and must not be localised.
        The name should be unique within each provider. Names should contain
        lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'Rename Bands'

    def displayName(self):
        """
        Returns the translated algorithm name, which should be used for any
        user-visible display of the algorithm name.
        """
        return self.tr(self.name())

    def group(self):
        """
        Returns the name of the group this algorithm belongs to. This string
        should be localised.
        """
        return self.tr(self.groupId())

    def groupId(self):
        """
        Returns the unique ID of the group this algorithm belongs to. This
        string should be fixed for the algorithm, and must not be localised.
        The group id should be unique within each provider. Group id should
        contain lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return ''

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return RenameBandsAlgorithm()