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
4a638b96
Commit
4a638b96
authored
Sep 13, 2021
by
PIERSON Julie
Browse files
reordering ok with gdal_translate
parent
7cff2732
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
14 deletions
+49
-14
rename_bands_algorithm.py
rename_bands_algorithm.py
+49
-14
No files found.
rename_bands_algorithm.py
View file @
4a638b96
...
...
@@ -38,6 +38,7 @@ from qgis.core import (QgsProcessing,
QgsProcessingParameterBand
,
QgsProcessingParameterRasterDestination
)
import
processing
import
subprocess
from
osgeo
import
gdal
...
...
@@ -81,6 +82,15 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
defaultValue
=
[
None
,
None
]
)
)
# output raster layer
self
.
addParameter
(
QgsProcessingParameterRasterDestination
(
'OUTPUT'
,
self
.
tr
(
'Raster output'
)
)
)
def
processAlgorithm
(
self
,
parameters
,
context
,
feedback
):
...
...
@@ -88,40 +98,65 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
Here is where the processing itself takes place.
"""
# R
etrieve inputs and outputs
# R
ETRIEVE INPUTS AND OUTPUT
input_raster
=
self
.
parameterAsRasterLayer
(
parameters
,
self
.
INPUT
,
context
)
band_table
=
self
.
parameterAsMatrix
(
parameters
,
self
.
TABLE
,
context
)
output_raster_path
=
self
.
parameterAsOutputLayer
(
parameters
,
self
.
OUTPUT
,
context
)
# GETTING USEFUL PAREMETERS
# 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
raster_path
=
input_raster
.
source
()
input_
raster_path
=
input_raster
.
source
()
# read raster with gdal
raster_gdal
=
gdal
.
Open
(
raster_path
)
# get number of bands
nbands
=
raster_gdal
.
RasterCount
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
# get number of bands of input raster
nbands
=
input_raster
.
RasterCount
# get number of rows in table
nrows
=
len
(
band_table
)
/
nb_col
nrows
=
len
(
band_table
)
# check if band number = row count in band_table
if
nbands
!=
nrows
:
message
=
'Number of rows in table is different from number of bands in input raster'
feedback
.
reportError
(
QCoreApplication
.
translate
(
'Rename bands'
,
message
))
return
{}
# 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
)]
# 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
(
nbands
):
band
=
raster_gdal
.
GetRasterBand
(
band_number
+
1
)
band
.
SetDescription
(
band_table
[
band_number
][
0
])
band
=
output_raster
.
GetRasterBand
(
band_number
+
1
)
band
.
SetDescription
(
sorted_band_table
[
band_number
][
index_banddesc
])
#
set band order using table input
#
RETURNING RESULTS
# Return the results of the algorithm as a dictionary
return
{}
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