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
Docker-in-Docker (DinD) capabilities of public runners deactivated.
More info
Open sidebar
LETG
Rename bands
Commits
56d5fcc8
Commit
56d5fcc8
authored
Sep 14, 2021
by
PIERSON Julie
Browse files
removing unused input parameters
parent
fa52e9d2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
108 deletions
+55
-108
merge_rename_bands_algorithm.py
merge_rename_bands_algorithm.py
+55
-108
No files found.
merge_rename_bands_algorithm.py
View file @
56d5fcc8
...
...
@@ -45,7 +45,7 @@ from processing.gui.wrappers import WidgetWrapper
from
qgis.PyQt.QtWidgets
import
QTableWidget
from
osgeo
import
gdal
# custom table widget
# custom
band
table widget
class
ParameterBandTable
(
QgsProcessingParameterDefinition
):
def
__init__
(
self
,
name
=
''
,
description
=
''
,
parent
=
None
,
optional
=
True
):
...
...
@@ -60,30 +60,6 @@ class ParameterBandTable(QgsProcessingParameterDefinition):
return
ParameterBandTable
(
self
.
name
(),
self
.
description
(),
self
.
parent
,
self
.
flags
()
&
QgsProcessingParameterDefinition
.
FlagOptional
)
@
staticmethod
def
valueToColors
(
value
):
if
value
is
None
:
return
None
if
value
==
''
:
return
None
if
isinstance
(
value
,
str
):
return
value
.
split
(
';'
)
else
:
return
ParameterBandTable
.
colorsToString
(
value
)
@
staticmethod
def
colorsToString
(
colors
):
s
=
''
for
c
in
colors
:
s
+=
'{:f}, {:f}, {:d}, {:d}, {:d};'
.
format
(
c
[
0
],
c
[
1
],
c
[
2
],
c
[
3
],
c
[
4
])
return
s
[:
-
1
]
class
TableWidget
(
WidgetWrapper
):
"""
...
...
@@ -122,9 +98,6 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# calling from the QGIS console.
INPUT
=
'INPUT'
TABLE
=
'TABLE'
TABLEWIDGET
=
'TABLEWIDGET'
BANDTABLEWIDGET
=
'BANDTABLEWIDGET'
OUTPUT
=
'OUTPUT'
def
initAlgorithm
(
self
,
config
):
...
...
@@ -133,39 +106,15 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
with some other properties.
"""
#
input raster layer
#
custom parameter : band table widget
self
.
addParameter
(
QgsProcessingParameterRasterLayer
(
ParameterBandTable
(
self
.
INPUT
,
self
.
tr
(
'Input raster layer'
),
[
QgsProcessing
.
TypeVectorAnyGeometry
]
)
)
# table to reaorder and rename bands
self
.
addParameter
(
QgsProcessingParameterMatrix
(
self
.
TABLE
,
self
.
tr
(
'Band names and order'
),
numberRows
=
0
,
hasFixedNumberRows
=
False
,
headers
=
[
'future name'
,
'order'
],
defaultValue
=
[
None
,
None
]
)
)
# custom parameter
param
=
QgsProcessingParameterString
(
self
.
TABLEWIDGET
,
'Band table'
)
param
.
setMetadata
({
'widget_wrapper'
:
{
'class'
:
TableWidget
}})
self
.
addParameter
(
param
)
# custom parameter : band table widget
self
.
addParameter
(
ParameterBandTable
(
self
.
BANDTABLEWIDGET
,
self
.
tr
(
'Band table'
),
self
.
INPUT
,
True
))
True
)
)
# output raster layer
self
.
addParameter
(
...
...
@@ -183,58 +132,56 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# RETRIEVE INPUTS AND OUTPUT
input_raster
=
self
.
parameterAsRasterLayer
(
parameters
,
self
.
INPUT
,
context
)
band_table
=
self
.
parameterAsMatrix
(
parameters
,
self
.
TABLE
,
context
)
mytable
=
self
.
parameterAsString
(
parameters
,
self
.
TABLEWIDGET
,
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
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
# get number of bands of input raster
nbands
=
input_raster
.
RasterCount
# get number of rows in table
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
{}
# 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
=
output_raster
.
GetRasterBand
(
band_number
+
1
)
band
.
SetDescription
(
sorted_band_table
[
band_number
][
index_banddesc
])
#
# 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
#
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
#
#
# get number of bands of input raster
#
nbands = input_raster.RasterCount
#
# get number of rows in table
#
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{}
#
#
# 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 = output_raster.GetRasterBand(band_number + 1)
#
band.SetDescription(sorted_band_table[band_number][index_banddesc])
# RETURNING RESULTS
...
...
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