Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
LETG
Rename bands
Commits
c3b57a8d
Commit
c3b57a8d
authored
Sep 15, 2021
by
PIERSON Julie
Browse files
alg now ok to be launched from toolbox history
parent
2d725296
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
15 deletions
+29
-15
BandTableWidget.py
BandTableWidget.py
+22
-3
merge_rename_bands_algorithm.py
merge_rename_bands_algorithm.py
+7
-12
No files found.
BandTableWidget.py
View file @
c3b57a8d
...
...
@@ -31,7 +31,9 @@ from qgis.PyQt.QtWidgets import (QTreeWidgetItem,
QInputDialog
,
)
from
qgis.core
import
(
QgsApplication
,
QgsProject
,
QgsMapLayer
,
QgsRasterLayer
,
QgsMapLayerProxyModel
)
from
processing.gui.wrappers
import
WidgetWrapper
from
osgeo
import
gdal
...
...
@@ -59,7 +61,7 @@ class BandTableWidget(BASE, WIDGET):
self
.
_removeBandData
()
# get raster layer
layer
=
self
.
cmbLayers
.
currentLayer
()
#QMessageBox.information(None, self.tr('tralala'), self.tr(str(layer)))
#QMessageBox.information(None, self.tr('tralala'), self.tr(str(layer
.name()
)))
input_raster
=
gdal
.
Open
(
layer
.
source
())
# get number of bands in raster
nbands
=
input_raster
.
RasterCount
...
...
@@ -151,20 +153,37 @@ class BandTableWidget(BASE, WIDGET):
def
setValue
(
self
,
value
):
self
.
bandClassTree
.
clear
()
rows
=
value
.
split
(
';'
)
param
=
value
.
split
(
';'
)
# setting input layer
layer_name
=
param
[
0
]
layer
=
QgsProject
.
instance
().
mapLayersByName
(
layer_name
)[
0
]
self
.
cmbLayers
.
setLayer
(
layer
)
# setting table values
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
):
allValues
=
self
.
cmbLayers
.
currentLayer
().
source
()
# adding input layer names to values (useful to select again this layer when using toolbox history)
allValues
=
self
.
cmbLayers
.
currentLayer
().
name
()
allValues
+=
';'
# adding input layer path to values
allValues
+=
self
.
cmbLayers
.
currentLayer
().
source
()
# allValues = [self.cmbLayers.currentLayer().source()]
# allValues += self.bandNames()
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
]
#return allValues
class
BandTableWidgetWrapper
(
WidgetWrapper
):
...
...
merge_rename_bands_algorithm.py
View file @
c3b57a8d
...
...
@@ -33,9 +33,8 @@ __revision__ = '$Format:%H$'
from
qgis.PyQt.QtCore
import
QCoreApplication
from
qgis.core
import
(
QgsProcessing
,
QgsProcessingAlgorithm
,
Qgs
ProcessingParameter
RasterLayer
,
QgsRasterLayer
,
QgsProcessingParameterDefinition
,
QgsProcessingParameterString
,
QgsProcessingParameterRasterDestination
)
from
osgeo
import
gdal
...
...
@@ -54,8 +53,9 @@ class ParameterBandTable(QgsProcessingParameterDefinition):
return
ParameterBandTable
(
self
.
name
(),
self
.
description
(),
self
.
parent
,
self
.
flags
())
# getting output values as a list, 1st element for layer then one element for each row :
# ['/path/to/layer.tif', 'band 1, old name 1, new name 1', 'band 2, old name 2, new name 2']
# getting output values as a list, 1st element for layer name, 2nd for layer path,
# then one element for each row :
# ['mylayer', '/path/to/mylayer.tif', 'band 1, old name 1, new name 1', 'band 2, old name 2, new name 2']
# (see BandTableWidget.py to change this)
@
staticmethod
def
tableValuesAsList
(
value
):
...
...
@@ -117,8 +117,8 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# GETTING USEFUL PARAMETERS
input_raster_path
=
input_param
[
0
]
band_data
=
input_param
[
1
:]
input_raster_path
=
input_param
[
1
]
band_data
=
input_param
[
2
:]
# number of rows in table
nb_row
=
len
(
band_data
)
...
...
@@ -133,18 +133,14 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# [['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
}
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
...
...
@@ -156,7 +152,6 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
new_name
=
band_table
[
band_number
][
index_oldname
]
band
.
SetDescription
(
new_name
)
# RETURNING RESULTS
# Return the results of the algorithm as a dictionary
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment