Skip to content

Commit 1664ef1

Browse files
committedSep 4, 2018
[FEATURE][processing] gdal's rearrange band algorithm
1 parent 13ecaa4 commit 1664ef1

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
 

‎python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from .polygonize import polygonize
6161
from .proximity import proximity
6262
from .rasterize import rasterize
63+
from .rearrange_bands import rearrange_bands
6364
from .retile import retile
6465
from .rgb2pct import rgb2pct
6566
from .roughness import roughness
@@ -166,6 +167,7 @@ def loadAlgorithms(self):
166167
polygonize(),
167168
proximity(),
168169
rasterize(),
170+
rearrange_bands(),
169171
retile(),
170172
rgb2pct(),
171173
roughness(),
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
translate.py
6+
---------------------
7+
Date : August 2018
8+
Copyright : (C) 2018 by Mathieu Pellerin
9+
Email : nirvn dot asia at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Mathieu Pellerin'
21+
__date__ = 'August 2018'
22+
__copyright__ = '(C) 2018, Mathieu Pellerin'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
import re
30+
31+
from qgis.PyQt.QtGui import QIcon
32+
33+
from qgis.core import (QgsRasterFileWriter,
34+
QgsProcessingException,
35+
QgsProcessingParameterEnum,
36+
QgsProcessingParameterDefinition,
37+
QgsProcessingParameterRasterLayer,
38+
QgsProcessingParameterBand,
39+
QgsProcessingParameterString,
40+
QgsProcessingParameterRasterDestination)
41+
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
42+
from processing.algs.gdal.GdalUtils import GdalUtils
43+
44+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
45+
46+
47+
class rearrange_bands(GdalAlgorithm):
48+
49+
INPUT = 'INPUT'
50+
BANDS = 'BANDS'
51+
OPTIONS = 'OPTIONS'
52+
DATA_TYPE = 'DATA_TYPE'
53+
OUTPUT = 'OUTPUT'
54+
55+
TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
56+
57+
def __init__(self):
58+
super().__init__()
59+
60+
def initAlgorithm(self, config=None):
61+
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
62+
self.addParameter(QgsProcessingParameterBand(self.BANDS,
63+
self.tr('Selected band(s)'),
64+
None,
65+
self.INPUT,
66+
allowMultiple=True))
67+
68+
options_param = QgsProcessingParameterString(self.OPTIONS,
69+
self.tr('Additional creation options'),
70+
defaultValue='',
71+
optional=True)
72+
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
73+
options_param.setMetadata({
74+
'widget_wrapper': {
75+
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
76+
self.addParameter(options_param)
77+
78+
dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE,
79+
self.tr('Output data type'),
80+
self.TYPES,
81+
allowMultiple=False,
82+
defaultValue=5)
83+
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
84+
self.addParameter(dataType_param)
85+
86+
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
87+
self.tr('Converted')))
88+
89+
def name(self):
90+
return 'rearrange_bands'
91+
92+
def displayName(self):
93+
return self.tr('Rearrange bands')
94+
95+
def group(self):
96+
return self.tr('Raster conversion')
97+
98+
def groupId(self):
99+
return 'rasterconversion'
100+
101+
def icon(self):
102+
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'translate.png'))
103+
104+
def shortHelpString(self):
105+
return self.tr("This algorithm creates a new raster using selected band(s) from a given raster layer.\n\n"
106+
"The reordering of bands is possible by dragging individual band names in the multiple selection dialog.")
107+
108+
def commandName(self):
109+
return 'gdal_translate'
110+
111+
def getConsoleCommands(self, parameters, context, feedback, executing=True):
112+
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
113+
if inLayer is None:
114+
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
115+
116+
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
117+
118+
arguments = []
119+
120+
bands = self.parameterAsFields(parameters, self.BANDS, context)
121+
for band in bands:
122+
match = re.search('(?:\A|[^0-9])([0-9]+)(?:\Z|[^0-9]|)', band)
123+
if match:
124+
band_nb = match.group(1)
125+
arguments.append('-b {}'.format(band_nb))
126+
127+
arguments.append('-ot')
128+
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
129+
130+
arguments.append('-of')
131+
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
132+
133+
options = self.parameterAsString(parameters, self.OPTIONS, context)
134+
if options:
135+
arguments.extend(GdalUtils.parseCreationOptions(options))
136+
137+
arguments.append(inLayer.source())
138+
arguments.append(out)
139+
140+
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]

0 commit comments

Comments
 (0)
Please sign in to comment.