2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
merge.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
|
|
|
QgsProcessing,
|
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterMultipleLayers,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterString,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterRasterDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
from processing.tools.system import isWindows
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class merge(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
PCT = 'PCT'
|
|
|
|
SEPARATE = 'SEPARATE'
|
2017-09-19 14:32:44 +03:00
|
|
|
OPTIONS = 'OPTIONS'
|
|
|
|
DATA_TYPE = 'DATA_TYPE'
|
2016-12-30 15:24:34 +02:00
|
|
|
OUTPUT = 'OUTPUT'
|
2014-11-13 23:13:13 +01:00
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-27 17:20:57 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-09-19 14:32:44 +03:00
|
|
|
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')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'merge'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Merge')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster miscellaneous')
|
|
|
|
|
2017-12-14 12:05:40 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rastermiscellaneous'
|
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'merge.png'))
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2017-09-19 14:32:44 +03:00
|
|
|
layers = self.parameterAsLayerList(parameters, self.INPUT, context)
|
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
|
|
|
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments = []
|
2017-09-19 14:32:44 +03:00
|
|
|
if self.parameterAsBool(parameters, self.PCT, context):
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-pct')
|
2017-09-19 14:32:44 +03:00
|
|
|
|
|
|
|
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:
|
2018-03-21 15:02:39 +10:00
|
|
|
arguments.extend(GdalUtils.parseCreationOptions(options))
|
2016-12-30 15:24:34 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-o')
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments.append(out)
|
2017-09-19 14:32:44 +03:00
|
|
|
|
|
|
|
for layer in layers:
|
|
|
|
arguments.append(layer.source())
|
2012-12-04 00:15:03 +01:00
|
|
|
|
2013-04-10 12:15:55 +04:00
|
|
|
commands = []
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2013-10-01 20:52:22 +03:00
|
|
|
commands = ['cmd.exe', '/C ', 'gdal_merge.bat',
|
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
2013-04-10 12:15:55 +04:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
commands = ['gdal_merge.py', GdalUtils.escapeAndJoin(arguments)]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|