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