Skip to content
Permalink
Browse files

[processing] restore gdal_merge algorithm

  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent 68863a9 commit fd12d296153836549f8591b47a206d55d4620cd5
@@ -53,6 +53,7 @@
from .GridLinear import GridLinear
from .GridNearestNeighbor import GridNearestNeighbor
from .hillshade import hillshade
from .merge import merge
from .nearblack import nearblack
from .pct2rgb import pct2rgb
from .polygonize import polygonize
@@ -67,7 +68,6 @@
from .tri import tri
from .warp import warp

# from .merge import merge
# from .rasterize import rasterize
# from .extractprojection import ExtractProjection
# from .gdalcalc import gdalcalc
@@ -156,6 +156,7 @@ def loadAlgorithms(self):
GridLinear(),
GridNearestNeighbor(),
hillshade(),
merge(),
nearblack(),
pct2rgb(),
polygonize(),
@@ -169,7 +170,6 @@ def loadAlgorithms(self):
tpi(),
tri(),
warp(),
# merge(),
# rasterize(),
# ExtractProjection(),
# gdalcalc(),
@@ -29,56 +29,65 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsRasterFileWriter,
QgsProcessing,
QgsProcessingParameterDefinition,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import (ParameterBoolean,
ParameterString,
ParameterSelection,
ParameterMultipleInput)
from processing.core.outputs import OutputRaster
from processing.tools.system import isWindows
from processing.tools import dataobjects

from processing.algs.gdal.GdalUtils import GdalUtils

from processing.tools.system import isWindows

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class merge(GdalAlgorithm):

INPUT = 'INPUT'
OPTIONS = 'OPTIONS'
PCT = 'PCT'
SEPARATE = 'SEPARATE'
RTYPE = 'RTYPE'
OPTIONS = 'OPTIONS'
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'merge.png'))
TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterMultipleInput(self.INPUT,
self.tr('Input layers'),
dataobjects.TYPE_RASTER))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True,
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
self.addParameter(ParameterBoolean(self.PCT,
self.tr('Grab pseudocolor table from first layer'),
False))
self.addParameter(ParameterBoolean(self.SEPARATE,
self.tr('Place each input file into a separate band'),
False))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'),
self.TYPE, 5))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Merged')))
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
self.tr('Input layers'),
QgsProcessing.TypeRaster))
self.addParameter(QgsProcessingParameterBoolean(self.PCT,
self.tr('Grab pseudocolor table from first layer'),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE,
self.tr('Place each input file into a separate band'),
defaultValue=False))

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation parameters'),
defaultValue='',
optional=True)
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
options_param.setMetadata({
'widget_wrapper': {
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5))

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Merged')))

def name(self):
return 'merge'
@@ -89,25 +98,36 @@ def displayName(self):
def group(self):
return self.tr('Raster miscellaneous')

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'merge.png'))

def getConsoleCommands(self, parameters, context, feedback):
layers = self.parameterAsLayerList(parameters, self.INPUT, context)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

arguments = []
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
if self.getParameterValue(self.SEPARATE):
arguments.append('-separate')
if self.getParameterValue(self.PCT):
if self.parameterAsBool(parameters, self.PCT, context):
arguments.append('-pct')
opts = self.getParameterValue(self.OPTIONS)
if opts:

if self.parameterAsBool(parameters, self.SEPARATE, context):
arguments.append('-separate')

arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
arguments.append('-co')
arguments.append(opts)
arguments.append(options)

arguments.append('-o')
out = self.getOutputValue(self.OUTPUT)
arguments.append(out)
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.extend(self.getParameterValue(self.INPUT).split(';'))

for layer in layers:
arguments.append(layer.source())

commands = []
if isWindows():

0 comments on commit fd12d29

Please sign in to comment.
You can’t perform that action at this time.