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
0e07d282
Commit
0e07d282
authored
Sep 10, 2021
by
PIERSON Julie
Browse files
bands can be renamed through table, very basic
parent
39362c27
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
2 deletions
+38
-2
rename_bands_algorithm.py
rename_bands_algorithm.py
+38
-2
No files found.
rename_bands_algorithm.py
View file @
0e07d282
...
...
@@ -34,8 +34,11 @@ from qgis.PyQt.QtCore import QCoreApplication
from
qgis.core
import
(
QgsProcessing
,
QgsProcessingAlgorithm
,
QgsProcessingParameterRasterLayer
,
QgsProcessingParameterMatrix
,
QgsProcessingParameterBand
,
QgsProcessingParameterRasterDestination
)
import
processing
from
osgeo
import
gdal
class
RenameBandsAlgorithm
(
QgsProcessingAlgorithm
):
...
...
@@ -48,8 +51,9 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
OUTPUT
=
'OUTPUT'
INPUT
=
'INPUT'
TABLE
=
'TABLE'
OUTPUT
=
'OUTPUT'
def
initAlgorithm
(
self
,
config
):
"""
...
...
@@ -66,6 +70,18 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
)
)
# table to reaorder and rename bands
self
.
addParameter
(
QgsProcessingParameterMatrix
(
self
.
TABLE
,
self
.
tr
(
'Nom et ordre des bandes'
),
numberRows
=
0
,
hasFixedNumberRows
=
False
,
headers
=
[
'futur nom'
,
'ordre'
],
defaultValue
=
[
None
,
None
]
)
)
# TODO
# output raster layer
self
.
addParameter
(
...
...
@@ -82,8 +98,28 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
# Retrieve inputs and outputs
input_raster
=
self
.
parameterAsRasterLayer
(
parameters
,
self
.
INPUT
,
context
)
band_table
=
self
.
parameterAsMatrix
(
parameters
,
self
.
TABLE
,
context
)
output_path_raster
=
self
.
parameterAsOutputLayer
(
parameters
,
self
.
OUTPUT
,
context
)
# get input raster path
raster_path
=
input_raster
.
source
()
# read raster with gdal
raster_gdal
=
gdal
.
Open
(
raster_path
)
# get number of bands
nbands
=
raster_gdal
.
RasterCount
# TODO : check if band number = row number in band_table
# 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]]
nb_col
=
2
band_table
=
[
band_table
[
n
:
n
+
nb_col
]
for
n
in
range
(
0
,
len
(
band_table
),
nb_col
)]
# iterate over each band to set its description
for
band_number
in
range
(
nbands
):
band
=
raster_gdal
.
GetRasterBand
(
band_number
+
1
)
band
.
SetDescription
(
band_table
[
band_number
][
0
])
# running gdal:translate processing algorithm
param_translate
=
{
'INPUT'
:
input_raster
,
'TARGET_CRS'
:
None
,
'NODATA'
:
None
,
...
...
@@ -95,7 +131,7 @@ class RenameBandsAlgorithm(QgsProcessingAlgorithm):
processing
.
run
(
"gdal:translate"
,
param_translate
)
# Return the results of the algorithm as a dictionary
return
{
self
.
OUTPUT
:
output_path_raster
}
return
{
self
.
OUTPUT
:
output_path_raster
,
'matrix'
:
band_table
}
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