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
ceebba1d
Commit
ceebba1d
authored
Sep 15, 2021
by
PIERSON Julie
Browse files
renaming and reordering ok
parent
2818dc08
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
45 deletions
+50
-45
BandTableWidget.py
BandTableWidget.py
+3
-4
merge_rename_bands_algorithm.py
merge_rename_bands_algorithm.py
+47
-41
No files found.
BandTableWidget.py
View file @
ceebba1d
...
...
@@ -66,8 +66,8 @@ class BandTableWidget(BASE, WIDGET):
# for each band
for
band_number
in
range
(
nbands
):
band
=
input_raster
.
GetRasterBand
(
band_number
+
1
)
# add band
name and
number to table
bandnumber
=
'bande '
+
str
(
band_number
+
1
)
# add band number to table
bandnumber
=
str
(
band_number
+
1
)
bandname
=
band
.
GetDescription
()
self
.
_addBandData
(
bandnumber
,
bandname
)
...
...
@@ -139,7 +139,6 @@ class BandTableWidget(BASE, WIDGET):
return
band_names
def
setLayer
(
self
,
layer
):
QMessageBox
.
information
(
None
,
self
.
tr
(
'tralala'
),
self
.
tr
(
'coucou'
))
self
.
layer
=
layer
self
.
updateTable
(
layer
)
...
...
@@ -163,7 +162,7 @@ class BandTableWidget(BASE, WIDGET):
allValues
=
self
.
cmbLayers
.
currentLayer
().
source
()
allValues
+=
';'
for
b
in
self
.
bandNames
():
allValues
+=
'{0},
{1},
{2};'
.
format
(
b
[
0
],
b
[
1
],
b
[
2
])
allValues
+=
'{0},{1},{2};'
.
format
(
b
[
0
],
b
[
1
],
b
[
2
])
return
allValues
[:
-
1
]
...
...
merge_rename_bands_algorithm.py
View file @
ceebba1d
...
...
@@ -111,64 +111,70 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
"""
# RETRIEVE INPUTS AND OUTPUT
band_data
=
ParameterBandTable
.
tableValuesAsList
(
parameters
[
self
.
INPUT
])
input_param
=
ParameterBandTable
.
tableValuesAsList
(
parameters
[
self
.
INPUT
])
output_raster_path
=
self
.
parameterAsOutputLayer
(
parameters
,
self
.
OUTPUT
,
context
)
# # GETTING USEFUL PARAMETERS
#
# # 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
#
input_raster
=
input_param
[
0
]
band_data
=
input_param
[
1
:]
# number of rows in table
nb_row
=
len
(
band_data
)
# # number of columns in table
# nb_col = len(band_data[0].split(','))
# index of column used for band order
index_bandorder
=
0
# index of column used for band description
index_banddesc
=
2
# 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
]
# getting input raster path
input_raster_path
=
input_raster
# read raster with gdal
input_raster
=
gdal
.
Open
(
input_raster
)
# 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
#
nrow
s
= len(band_table)
# get number of rows in table
n
b_
row
=
len
(
band_table
)
# # check if band number = row count in band_table
# if nbands != nrow
s
:
# if nbands != n
b_
row:
# 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(nb
ands
):
#
band = output_raster.GetRasterBand(band_number + 1)
#
band.SetDescription(
sorted_
band_table[band_number][index_banddesc])
# 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
)
band
.
SetDescription
(
band_table
[
band_number
][
index_banddesc
])
# RETURNING RESULTS
# Return the results of the algorithm as a dictionary
#return {self.OUTPUT: output_raster_path}
return
{
'
band data'
:
band_data
}
return
{
'
input_param'
:
input_param
}
def
name
(
self
):
"""
...
...
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