Commit 64fa9343 authored by PIERSON Julie's avatar PIERSON Julie
Browse files

merging selected raster ok

parent cc51d5d2
......@@ -141,13 +141,13 @@ class MergeBandTableWidget(BASE, WIDGET):
item.setText(1, str(d))
# return table values in a list, one element for each row
# [['band 1', 'old name 1', 'new name 1'], ['band 2', 'old name 2', 'new name 2']]
# [['band 1', 'new name 1'], ['band 2', 'new name 2']]
def bandNames(self):
band_names = []
for i in range(self.bandClassTree.topLevelItemCount()):
item = self.bandClassTree.topLevelItem(i)
if item:
row = [item.text(0), item.text(1), item.text(2)]
row = [item.text(0), item.text(1)]
band_names.append(row)
return band_names
......@@ -184,15 +184,16 @@ class MergeBandTableWidget(BASE, WIDGET):
# return a list where 1st element is input raster path
# and then each element for a table row
def value(self):
# adding input layer names to values (useful to select again this layer when using toolbox history)
allValues = self.multiLayers.currentLayer().name()
allValues += ';'
# adding input layer path to values
allValues += self.multiLayers.currentLayer().source()
allValues += ';'
# # adding input layer names to values (useful to select again this layer when using toolbox history)
# allValues = self.multiLayers.currentLayer().name()
# allValues += ';'
# # adding input layer path to values
# allValues += self.multiLayers.currentLayer().source()
# allValues += ';'
allValues = ''
# adding table content to values
for b in self.bandNames():
allValues += '{0},{1},{2};'.format(b[0], b[1], b[2])
allValues += '{0},{1};'.format(b[0], b[1])
return allValues[:-1]
......
......@@ -32,23 +32,25 @@ __revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessingAlgorithm,
QgsProject,
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterDestination)
import processing
from osgeo import gdal
# custom band table widget
class ParameterBandTable(QgsProcessingParameterDefinition):
class ParameterMergeBandTable(QgsProcessingParameterDefinition):
def __init__(self, name='', description='', parent=None, optional=False):
super().__init__(name, description, None, optional)
self.parent = parent
self.setMetadata({'widget_wrapper': 'BandTableWidget.BandTableWidgetWrapper'})
self.setMetadata({'widget_wrapper': 'MergeBandTableWidget.MergeBandTableWidgetWrapper'})
def type(self):
return 'Input parameters'
def clone(self):
return ParameterBandTable(self.name(), self.description(), self.parent, self.flags())
return ParameterMergeBandTable(self.name(), self.description(), self.parent, self.flags())
# getting output values as a list, 1st element for layer name, 2nd for layer path,
......@@ -88,7 +90,7 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# custom parameter : band table widget
self.addParameter(
ParameterBandTable(
ParameterMergeBandTable(
self.INPUT,
self.tr('Input parameters'),
False # True if optional
......@@ -110,45 +112,51 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
"""
# RETRIEVE INPUTS AND OUTPUT
input_param = ParameterBandTable.tableValuesAsList(parameters[self.INPUT])
input_param = ParameterMergeBandTable.tableValuesAsList(parameters[self.INPUT])
output_raster_path = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
# GETTING USEFUL PARAMETERS
message = 'input_param : ' + str(input_param)
feedback.pushInfo(QCoreApplication.translate('merge and rename', message))
input_raster_path = input_param[1]
band_data = input_param[2:]
# GETTING USEFUL PARAMETERS
# number of rows in table
nb_row = len(band_data)
# index of column used for band order
index_bandorder = 0
# number of rows in table = number of bands to merge
nb_row = len(input_param)
# index of column used for old band name
index_oldname = 1
index_oldname = 0
# index of column used for new band name
index_newname = 2
index_newname = 1
# 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 = [i.split(',') for i in band_data]
# 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
# iterate over each band to set its description using table input
for band_number in range(nb_row):
band = output_raster.GetRasterBand(band_number + 1)
new_name = band_table[band_number][index_newname]
# if no new name was set, use old name
if new_name == '':
new_name = band_table[band_number][index_oldname]
band.SetDescription(new_name)
band_table = [i.split(',') for i in input_param]
# getting path of each input raster
rasterNames = [i[index_oldname] for i in band_table]
rasterLayers = [QgsProject.instance().mapLayersByName(i)[0] for i in rasterNames]
rasterPaths = [i.source() for i in rasterLayers]
# MERGING BANDS
merge_param = {
'INPUT' : rasterPaths,
'SEPARATE' : True,
'OUTPUT' : output_raster_path
}
merge_result = processing.run("gdal:merge", merge_param, context=context, feedback=feedback)
#
# # RENAMING BANDS
#
# # iterate over each band to set its description using table input
# for band_number in range(nb_row):
# band = output_raster.GetRasterBand(band_number + 1)
# new_name = band_table[band_number][index_newname]
# # if no new name was set, use old name
# if new_name == '':
# new_name = band_table[band_number][index_oldname]
# band.SetDescription(new_name)
# 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