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
64fa9343
Commit
64fa9343
authored
Sep 17, 2021
by
PIERSON Julie
Browse files
merging selected raster ok
parent
cc51d5d2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
40 deletions
+49
-40
MergeBandTableWidget.py
MergeBandTableWidget.py
+10
-9
merge_rename_bands_algorithm.py
merge_rename_bands_algorithm.py
+39
-31
No files found.
MergeBandTableWidget.py
View file @
64fa9343
...
...
@@ -141,13 +141,13 @@ class MergeBandTableWidget(BASE, WIDGET):
item
.
setText
(
1
,
str
(
d
))
# return table values in a list, one element for each row
# [['band 1',
'old name 1',
'new name 1'], ['band 2',
'old name 2',
'new name 2']]
# [['band 1', 'new name 1'], ['band 2', 'new name 2']]
def
bandNames
(
self
):
band_names
=
[]
for
i
in
range
(
self
.
bandClassTree
.
topLevelItemCount
()):
item
=
self
.
bandClassTree
.
topLevelItem
(
i
)
if
item
:
row
=
[
item
.
text
(
0
),
item
.
text
(
1
)
,
item
.
text
(
2
)
]
row
=
[
item
.
text
(
0
),
item
.
text
(
1
)]
band_names
.
append
(
row
)
return
band_names
...
...
@@ -184,15 +184,16 @@ class MergeBandTableWidget(BASE, WIDGET):
# return a list where 1st element is input raster path
# and then each element for a table row
def
value
(
self
):
# adding input layer names to values (useful to select again this layer when using toolbox history)
allValues
=
self
.
multiLayers
.
currentLayer
().
name
()
allValues
+=
';'
# adding input layer path to values
allValues
+=
self
.
multiLayers
.
currentLayer
().
source
()
allValues
+=
';'
# # adding input layer names to values (useful to select again this layer when using toolbox history)
# allValues = self.multiLayers.currentLayer().name()
# allValues += ';'
# # adding input layer path to values
# allValues += self.multiLayers.currentLayer().source()
# allValues += ';'
allValues
=
''
# adding table content to values
for
b
in
self
.
bandNames
():
allValues
+=
'{0},{1}
,{2}
;'
.
format
(
b
[
0
],
b
[
1
]
,
b
[
2
]
)
allValues
+=
'{0},{1};'
.
format
(
b
[
0
],
b
[
1
])
return
allValues
[:
-
1
]
...
...
merge_rename_bands_algorithm.py
View file @
64fa9343
...
...
@@ -32,23 +32,25 @@ __revision__ = '$Format:%H$'
from
qgis.PyQt.QtCore
import
QCoreApplication
from
qgis.core
import
(
QgsProcessingAlgorithm
,
QgsProject
,
QgsProcessingParameterDefinition
,
QgsProcessingParameterRasterDestination
)
import
processing
from
osgeo
import
gdal
# custom band table widget
class
ParameterBandTable
(
QgsProcessingParameterDefinition
):
class
Parameter
Merge
BandTable
(
QgsProcessingParameterDefinition
):
def
__init__
(
self
,
name
=
''
,
description
=
''
,
parent
=
None
,
optional
=
False
):
super
().
__init__
(
name
,
description
,
None
,
optional
)
self
.
parent
=
parent
self
.
setMetadata
({
'widget_wrapper'
:
'BandTableWidget.BandTableWidgetWrapper'
})
self
.
setMetadata
({
'widget_wrapper'
:
'
Merge
BandTableWidget.
Merge
BandTableWidgetWrapper'
})
def
type
(
self
):
return
'Input parameters'
def
clone
(
self
):
return
ParameterBandTable
(
self
.
name
(),
self
.
description
(),
self
.
parent
,
self
.
flags
())
return
Parameter
Merge
BandTable
(
self
.
name
(),
self
.
description
(),
self
.
parent
,
self
.
flags
())
# getting output values as a list, 1st element for layer name, 2nd for layer path,
...
...
@@ -88,7 +90,7 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
# custom parameter : band table widget
self
.
addParameter
(
ParameterBandTable
(
Parameter
Merge
BandTable
(
self
.
INPUT
,
self
.
tr
(
'Input parameters'
),
False
# True if optional
...
...
@@ -110,45 +112,51 @@ class MergeAndRenameAlgorithm(QgsProcessingAlgorithm):
"""
# RETRIEVE INPUTS AND OUTPUT
input_param
=
ParameterBandTable
.
tableValuesAsList
(
parameters
[
self
.
INPUT
])
input_param
=
Parameter
Merge
BandTable
.
tableValuesAsList
(
parameters
[
self
.
INPUT
])
output_raster_path
=
self
.
parameterAsOutputLayer
(
parameters
,
self
.
OUTPUT
,
context
)
# GETTING USEFUL PARAMETERS
message
=
'input_param : '
+
str
(
input_param
)
feedback
.
pushInfo
(
QCoreApplication
.
translate
(
'merge and rename'
,
message
))
input_raster_path
=
input_param
[
1
]
band_data
=
input_param
[
2
:]
# GETTING USEFUL PARAMETERS
# number of rows in table
nb_row
=
len
(
band_data
)
# index of column used for band order
index_bandorder
=
0
# number of rows in table = number of bands to merge
nb_row
=
len
(
input_param
)
# index of column used for old band name
index_oldname
=
1
index_oldname
=
0
# index of column used for new band name
index_newname
=
2
index_newname
=
1
# 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
]
band_table
=
[
i
.
split
(
','
)
for
i
in
input_param
]
# REORDERING BANDS
# getting path of each input raster
rasterNames
=
[
i
[
index_oldname
]
for
i
in
band_table
]
rasterLayers
=
[
QgsProject
.
instance
().
mapLayersByName
(
i
)[
0
]
for
i
in
rasterNames
]
rasterPaths
=
[
i
.
source
()
for
i
in
rasterLayers
]
# 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
# 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
)
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
)
# MERGING BANDS
merge_param
=
{
'INPUT'
:
rasterPaths
,
'SEPARATE'
:
True
,
'OUTPUT'
:
output_raster_path
}
merge_result
=
processing
.
run
(
"gdal:merge"
,
merge_param
,
context
=
context
,
feedback
=
feedback
)
#
# # RENAMING BANDS
#
# # 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)
# 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
...
...
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