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
|
|
|
|
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,
|
2018-03-21 15:27:18 +10:00
|
|
|
QgsProcessingParameterNumber,
|
2019-01-07 15:05:33 +01:00
|
|
|
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'
|
2019-06-24 16:19:07 +03:00
|
|
|
EXTRA = 'EXTRA'
|
2017-09-19 14:32:44 +03:00
|
|
|
DATA_TYPE = 'DATA_TYPE'
|
2018-03-21 15:27:18 +10:00
|
|
|
NODATA_INPUT = 'NODATA_INPUT'
|
|
|
|
NODATA_OUTPUT = 'NODATA_OUTPUT'
|
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))
|
|
|
|
|
2018-03-21 15:27:18 +10:00
|
|
|
nodata_param = QgsProcessingParameterNumber(self.NODATA_INPUT,
|
|
|
|
self.tr('Input pixel value to treat as "nodata"'),
|
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
defaultValue=None,
|
|
|
|
optional=True)
|
|
|
|
nodata_param.setFlags(nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(nodata_param)
|
|
|
|
|
|
|
|
nodata_out_param = QgsProcessingParameterNumber(self.NODATA_OUTPUT,
|
2018-03-21 21:05:31 +10:00
|
|
|
self.tr('Assign specified "nodata" value to output'),
|
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
defaultValue=None,
|
|
|
|
optional=True)
|
2018-03-21 15:27:18 +10:00
|
|
|
nodata_out_param.setFlags(nodata_out_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(nodata_out_param)
|
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
options_param = QgsProcessingParameterString(self.OPTIONS,
|
2018-08-02 12:03:45 +03:00
|
|
|
self.tr('Additional creation options'),
|
2017-09-19 14:32:44 +03:00
|
|
|
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)
|
|
|
|
|
2019-06-24 16:19:07 +03:00
|
|
|
extra_param = QgsProcessingParameterString(self.EXTRA,
|
|
|
|
self.tr('Additional command-line parameters'),
|
|
|
|
defaultValue=None,
|
|
|
|
optional=True)
|
|
|
|
extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(extra_param)
|
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
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'))
|
|
|
|
|
2018-05-14 11:32:55 +07:00
|
|
|
def commandName(self):
|
|
|
|
return 'gdal_merge'
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2017-09-19 14:32:44 +03:00
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2019-02-05 12:48:43 +10:00
|
|
|
self.setOutputValue(self.OUTPUT, out)
|
2017-09-19 14:32:44 +03:00
|
|
|
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments = []
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.PCT, context):
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-pct')
|
2017-09-19 14:32:44 +03:00
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.SEPARATE, context):
|
2017-09-19 14:32:44 +03:00
|
|
|
arguments.append('-separate')
|
|
|
|
|
2018-03-21 15:27:18 +10:00
|
|
|
if self.NODATA_INPUT in parameters and parameters[self.NODATA_INPUT] is not None:
|
|
|
|
nodata_input = self.parameterAsInt(parameters, self.NODATA_INPUT, context)
|
|
|
|
arguments.append('-n')
|
|
|
|
arguments.append(str(nodata_input))
|
|
|
|
|
|
|
|
if self.NODATA_OUTPUT in parameters and parameters[self.NODATA_OUTPUT] is not None:
|
|
|
|
nodata_output = self.parameterAsInt(parameters, self.NODATA_OUTPUT, context)
|
|
|
|
arguments.append('-a_nodata')
|
|
|
|
arguments.append(str(nodata_output))
|
|
|
|
|
2017-09-19 14:32:44 +03:00
|
|
|
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
|
|
|
|
2019-06-24 16:19:07 +03:00
|
|
|
if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
|
|
|
|
extra = self.parameterAsString(parameters, self.EXTRA, context)
|
|
|
|
arguments.append(extra)
|
|
|
|
|
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
|
|
|
|
2018-03-21 13:43:41 +10:00
|
|
|
# Always write input files to a text file in case there are many of them and the
|
|
|
|
# length of the command will be longer then allowed in command prompt
|
2018-03-21 21:05:31 +10:00
|
|
|
list_file = GdalUtils.writeLayerParameterToTextFile(filename='mergeInputFiles.txt', alg=self, parameters=parameters, parameter_name=self.INPUT, context=context, quote=True, executing=executing)
|
2018-03-21 13:43:41 +10:00
|
|
|
arguments.append('--optfile')
|
2018-03-21 21:05:31 +10:00
|
|
|
arguments.append(list_file)
|
2012-12-04 00:15:03 +01:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2019-02-10 21:06:56 +01:00
|
|
|
commands = ["python3", "-m", self.commandName()]
|
|
|
|
else:
|
|
|
|
commands = [self.commandName() + '.py']
|
|
|
|
|
|
|
|
commands.append(GdalUtils.escapeAndJoin(arguments))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|