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
2d725296
Commit
2d725296
authored
Sep 15, 2021
by
PIERSON Julie
Browse files
bit of cleaning up
parent
ceebba1d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
29 deletions
+17
-29
BandTableWidget.py
BandTableWidget.py
+1
-0
merge_rename_bands_algorithm.py
merge_rename_bands_algorithm.py
+16
-29
No files found.
BandTableWidget.py
View file @
2d725296
...
...
@@ -136,6 +136,7 @@ class BandTableWidget(BASE, WIDGET):
if
item
:
row
=
[
item
.
text
(
0
),
item
.
text
(
1
),
item
.
text
(
2
)]
band_names
.
append
(
row
)
QMessageBox
.
information
(
None
,
self
.
tr
(
'tralala'
),
self
.
tr
(
str
(
band_names
)))
return
band_names
def
setLayer
(
self
,
layer
):
...
...
merge_rename_bands_algorithm.py
View file @
2d725296
...
...
@@ -55,7 +55,8 @@ class ParameterBandTable(QgsProcessingParameterDefinition):
# getting output values as a list, 1st element for layer then one element for each row :
# ['/path/to/layer.tif', 'band 1, red, green', 'band 2, blue, red', 'bande 3, green, blue']
# ['/path/to/layer.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
):
if
value
is
None
:
...
...
@@ -114,29 +115,24 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
input_param
=
ParameterBandTable
.
tableValuesAsList
(
parameters
[
self
.
INPUT
])
output_raster_path
=
self
.
parameterAsOutputLayer
(
parameters
,
self
.
OUTPUT
,
context
)
#
# GETTING USEFUL PARAMETERS
# GETTING USEFUL PARAMETERS
input_raster
=
input_param
[
0
]
input_raster
_path
=
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
# index of column used for old band name
index_oldname
=
1
# index of column used for new band name
index_newname
=
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
...
...
@@ -150,31 +146,22 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# RENAMING BANDS
# # get number of bands of input raster
# nbands = input_raster.RasterCount
# get number of rows in table
nb_row
=
len
(
band_table
)
# # check if band number = row count in band_table
# if nbands != nb_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_row
):
band
=
output_raster
.
GetRasterBand
(
band_number
+
1
)
band
.
SetDescription
(
band_table
[
band_number
][
index_banddesc
])
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
# Return the results of the algorithm as a dictionary
#
return {self.OUTPUT: output_raster_path}
return
{
'input_param'
:
input_param
}
return
{
self
.
OUTPUT
:
output_raster_path
}
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