Commit 56d5fcc8 authored by PIERSON Julie's avatar PIERSON Julie
Browse files

removing unused input parameters

parent fa52e9d2
......@@ -45,7 +45,7 @@ from processing.gui.wrappers import WidgetWrapper
from qgis.PyQt.QtWidgets import QTableWidget
from osgeo import gdal
# custom table widget
# custom band table widget
class ParameterBandTable(QgsProcessingParameterDefinition):
def __init__(self, name='', description='', parent=None, optional=True):
......@@ -59,30 +59,6 @@ class ParameterBandTable(QgsProcessingParameterDefinition):
def clone(self):
return ParameterBandTable(self.name(), self.description(), self.parent,
self.flags() & QgsProcessingParameterDefinition.FlagOptional)
@staticmethod
def valueToColors(value):
if value is None:
return None
if value == '':
return None
if isinstance(value, str):
return value.split(';')
else:
return ParameterBandTable.colorsToString(value)
@staticmethod
def colorsToString(colors):
s = ''
for c in colors:
s += '{:f}, {:f}, {:d}, {:d}, {:d};'.format(c[0],
c[1],
c[2],
c[3],
c[4])
return s[:-1]
class TableWidget(WidgetWrapper):
......@@ -122,9 +98,6 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# calling from the QGIS console.
INPUT = 'INPUT'
TABLE = 'TABLE'
TABLEWIDGET = 'TABLEWIDGET'
BANDTABLEWIDGET = 'BANDTABLEWIDGET'
OUTPUT = 'OUTPUT'
def initAlgorithm(self, config):
......@@ -132,40 +105,16 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
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]
)
)
# table to reaorder and rename bands
# custom parameter : band table widget
self.addParameter(
QgsProcessingParameterMatrix(
self.TABLE,
self.tr('Band names and order'),
numberRows=0,
hasFixedNumberRows = False,
headers=['future name', 'order'],
defaultValue=[None,None]
ParameterBandTable(
self.INPUT,
self.tr('Band table'),
self.INPUT,
True
)
)
# custom parameter
param = QgsProcessingParameterString(self.TABLEWIDGET, 'Band table')
param.setMetadata({
'widget_wrapper': {
'class': TableWidget}})
self.addParameter(param)
# custom parameter : band table widget
self.addParameter(ParameterBandTable(self.BANDTABLEWIDGET,
self.tr('Band table'),
self.INPUT,
True))
# output raster layer
self.addParameter(
......@@ -183,58 +132,56 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# RETRIEVE INPUTS AND OUTPUT
input_raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
band_table = self.parameterAsMatrix(parameters, self.TABLE, context)
mytable = self.parameterAsString(parameters, self.TABLEWIDGET, context)
output_raster_path = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
# GETTING USEFUL PAREMETERS
# number of columns in table is used later on, change it here if necessary
nb_col = 2
# index of column used for band order
index_bandorder = 1
# index of column used for band description
index_banddesc = 0
# transform band_table in a nested list, with 1 element per band, and name and order of each band
# [['band 1', 1], ['band 2', 2], ['band 3', 3]]
band_table = [band_table[n:n+nb_col] for n in range(0, len(band_table), nb_col)]
# get input raster path
input_raster_path = input_raster.source()
# read raster with gdal
input_raster = gdal.Open(input_raster_path)
# REORDERING BANDS
# get order of bands as a list, i.e. [3,2,1]
band_order = [i[index_bandorder] for i in band_table]
# reordering bands using gdal_translate
kwargs = {
'bandList': band_order
}
output_raster = gdal.Translate(output_raster_path, input_raster_path, **kwargs)
# RENAMING BANDS
# get number of bands of input raster
nbands = input_raster.RasterCount
# get number of rows in table
nrows = len(band_table)
# check if band number = row count in band_table
if nbands != nrows:
message = 'Number of rows in table is different from number of bands in input raster'
feedback.reportError(QCoreApplication.translate('Rename bands', message))
return{}
# reorder band table so that bands are in same order as in output raster
sorted_band_table = sorted(band_table, key = lambda x: x[index_bandorder])
# iterate over each band to set its description using table input
for band_number in range(nbands):
band = output_raster.GetRasterBand(band_number + 1)
band.SetDescription(sorted_band_table[band_number][index_banddesc])
# # GETTING USEFUL PAREMETERS
#
# # number of columns in table is used later on, change it here if necessary
# nb_col = 2
# # index of column used for band order
# index_bandorder = 1
# # index of column used for band description
# index_banddesc = 0
#
# # transform band_table in a nested list, with 1 element per band, and name and order of each band
# # [['band 1', 1], ['band 2', 2], ['band 3', 3]]
# band_table = [band_table[n:n+nb_col] for n in range(0, len(band_table), nb_col)]
# # get input raster path
# input_raster_path = input_raster.source()
# # read raster with gdal
# input_raster = gdal.Open(input_raster_path)
#
#
# # REORDERING BANDS
#
# # get order of bands as a list, i.e. [3,2,1]
# band_order = [i[index_bandorder] for i in band_table]
# # reordering bands using gdal_translate
# kwargs = {
# 'bandList': band_order
# }
# output_raster = gdal.Translate(output_raster_path, input_raster_path, **kwargs)
#
#
# # RENAMING BANDS
#
# # get number of bands of input raster
# nbands = input_raster.RasterCount
# # get number of rows in table
# nrows = len(band_table)
# # check if band number = row count in band_table
# if nbands != nrows:
# message = 'Number of rows in table is different from number of bands in input raster'
# feedback.reportError(QCoreApplication.translate('Rename bands', message))
# return{}
#
# # reorder band table so that bands are in same order as in output raster
# sorted_band_table = sorted(band_table, key = lambda x: x[index_bandorder])
#
# # iterate over each band to set its description using table input
# for band_number in range(nbands):
# band = output_raster.GetRasterBand(band_number + 1)
# band.SetDescription(sorted_band_table[band_number][index_banddesc])
# RETURNING RESULTS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment