Commit cc51d5d2 authored by PIERSON Julie's avatar PIERSON Julie
Browse files

using checkablecombobox for selecting single band rasters

parent ae70241a
# -*- coding: utf-8 -*-
Date : 2021-09-14
Copyright : (C) 2021 by J. Pierson, UMR 6554 LETG, CNRS
Email :
Based upon : (C) 2016 by Alexander Bruy
* *
* 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__ = 'J. Pierson, UMR 6554 LETG, CNRS'
__date__ = '2021-09-14'
__copyright__ = '(C) 2021 by J. Pierson, UMR 6554 LETG, CNRS'
import os
from qgis.PyQt import uic
from qgis.PyQt.QtCore import pyqtSlot
from qgis.PyQt.QtWidgets import (QTreeWidgetItem,
from qgis.core import (QgsApplication,
from processing.gui.wrappers import WidgetWrapper
from osgeo import gdal
pluginPath = os.path.dirname(__file__)
WIDGET, BASE = uic.loadUiType(os.path.join(pluginPath, 'mergebandtablewidgetbase.ui'))
class MergeBandTableWidget(BASE, WIDGET):
def __init__(self):
super(MergeBandTableWidget, self).__init__(None)
#self.multiLayers.setLayerType(QgsProcessing.TypeRaster) # 3 for TypeRaster
self.layer = None
# when alg is launched by user, populate combobox with loaded raster layers
# this is how to display a message for debugging
#QMessageBox.information(None,'window name'),'message'))
# populate checkable combobox with every loaded single band raster layer
def populateMultiLayers(self):
# get a list of loaded raster layers
layerList = QgsProject.instance().mapLayers().values()
rasterList = [i for i in list(layerList) if i.type() == QgsMapLayer.RasterLayer]
# keep only single band rasters in list
rasterNames = [ for i in list(rasterList) if i.bandCount() == 1]
# populate combobox
for r in rasterNames:
# when user changes selected layer in combobox
def layersChanged(self):
# remove table content
# get raster layers
rasterNames = self.multiLayers.checkedItems()
# if there is at least one selected raster
if len(rasterNames) != 0:
for r in rasterNames:
# # if there is one
# if layer != None:
# input_raster = gdal.Open(layer.source())
# # get number of bands in raster
# nbands = input_raster.RasterCount
# # for each band
# for band_number in range(nbands):
# band = input_raster.GetRasterBand(band_number + 1)
# # add band number to table
# bandnumber = str(band_number+1)
# bandname = band.GetDescription()
# self._addBandData(bandnumber, bandname)
# add selected raster names to table
def _addBandData(self, rastername):
item = QTreeWidgetItem()
item.setText(0, rastername)
# remove all rows from table
def _removeBandData(self,):
# move down selected line in table when down button is clicked
def on_btnDown_clicked(self):
selectedItems = self.bandClassTree.selectedItems()
for item in selectedItems:
currentIndex = self.bandClassTree.indexOfTopLevelItem(item)
if currentIndex < self.bandClassTree.topLevelItemCount() - 1:
self.bandClassTree.insertTopLevelItem(currentIndex + 1, item)
# move up selected line in table when up button is clicked
def on_btnUp_clicked(self):
selectedItems = self.bandClassTree.selectedItems()
for item in selectedItems:
currentIndex = self.bandClassTree.indexOfTopLevelItem(item)
if currentIndex > 0:
self.bandClassTree.insertTopLevelItem(currentIndex - 1, item)
# when a cell in 2nd column is clicked, open dialog box for entering value
@pyqtSlot(QTreeWidgetItem, int)
def on_bandClassTree_itemDoubleClicked(self, item, column):
if not item:
if column == 1:
d, ok = QInputDialog.getText(None,'Band name'),'Enter new band name')
if ok:
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']]
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)]
return band_names
def setLayers(self, layers):
self.layers = layers
def updateTable(self, layers):
item = QTreeWidgetItem()
item.setText(0, '0.00')
item.setText(1, '0.00')
def setValue(self, value):
param = value.split(';')
# setting input layer
layer_name = param[0]
# # if layer is loaded in project
# if QgsProject.instance().mapLayersByName(layer_name):
# layer = QgsProject.instance().mapLayersByName(layer_name)[0]
# self.multiLayers.setLayer(layer)
# # setting table values
# self.bandClassTree.clear()
# rows = param[2:]
# for r in rows:
# v = r.split(',')
# item = QTreeWidgetItem()
# item.setText(0, v[0])
# item.setText(1, v[1])
# item.setText(2, v[2])
# self.bandClassTree.addTopLevelItem(item)
# 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 table content to values
for b in self.bandNames():
allValues += '{0},{1},{2};'.format(b[0], b[1], b[2])
return allValues[:-1]
class MergeBandTableWidgetWrapper(WidgetWrapper):
def createWidget(self):
return MergeBandTableWidget()
def postInitialize(self, wrappers):
for wrapper in wrappers:
if == self.param.parent:
def parentValueChanged(self, wrapper):
def setLayer(self, layer):
if isinstance(layer, QgsMapLayer):
layer = layer.source()
def setValue(self, value):
def value(self):
return self.widget.value()
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<widget class="QWidget" name="Form">
<property name="geometry">
<property name="windowTitle">
<string notr="true">Form</string>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Raster layers</string>
<property name="buddy">
<item row="0" column="1">
<widget class="QgsCheckableComboBox" name="multiLayers">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<item row="1" column="0" colspan="2">
<widget class="QTreeWidget" name="bandClassTree">
<property name="text">
<string>Band name</string>
<property name="text">
<string>Band future name</string>
<item row="1" column="3">
<layout class="QVBoxLayout" name="verticalLayout">
<widget class="QToolButton" name="btnUp">
<property name="toolTip">
<string>Move up</string>
<property name="text">
<widget class="QToolButton" name="btnDown">
<property name="toolTip">
<string>Move down</string>
<property name="text">
<spacer name="verticalSpacer">
<property name="orientation">
<property name="sizeHint" stdset="0">
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