2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
nearblack.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
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
|
2017-08-31 15:16:44 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
2018-05-06 10:29:01 +10:00
|
|
|
QgsProcessingException,
|
2017-09-06 16:59:48 +03:00
|
|
|
QgsProcessingParameterDefinition,
|
2017-08-31 15:16:44 +03:00
|
|
|
QgsProcessingParameterRasterLayer,
|
2017-08-16 11:26:10 +07:00
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterNumber,
|
2017-09-06 16:59:48 +03:00
|
|
|
QgsProcessingParameterString,
|
2017-08-16 11:26:10 +07: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
|
2012-09-15 18:25:25 +03:00
|
|
|
|
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 nearblack(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
NEAR = 'NEAR'
|
|
|
|
WHITE = 'WHITE'
|
2017-09-06 16:59:48 +03:00
|
|
|
OPTIONS = 'OPTIONS'
|
2019-06-24 16:31:27 +03:00
|
|
|
EXTRA = 'EXTRA'
|
2017-09-06 16:59:48 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
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-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
|
2017-08-16 11:26:10 +07:00
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.NEAR,
|
|
|
|
self.tr('How far from black (white)'),
|
2017-09-06 16:59:48 +03:00
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
minValue=0,
|
|
|
|
defaultValue=15))
|
2017-08-16 11:26:10 +07:00
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.WHITE,
|
|
|
|
self.tr('Search for nearly white pixels instead of nearly black'),
|
|
|
|
defaultValue=False))
|
2017-09-06 16:59:48 +03:00
|
|
|
|
|
|
|
options_param = QgsProcessingParameterString(self.OPTIONS,
|
2018-08-02 12:03:45 +03:00
|
|
|
self.tr('Additional creation options'),
|
2017-09-06 16:59:48 +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:31:27 +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-08-16 11:26:10 +07:00
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Nearblack')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'nearblack'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Near black')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster analysis')
|
|
|
|
|
2017-12-14 12:05:40 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rasteranalysis'
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'nearblack.png'))
|
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
def commandName(self):
|
|
|
|
return 'nearblack'
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2017-09-06 16:59:48 +03:00
|
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
2018-05-06 10:29:01 +10:00
|
|
|
if inLayer is None:
|
|
|
|
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
2017-09-14 12:45:14 +03:00
|
|
|
|
|
|
|
arguments = []
|
2017-09-06 16:59:48 +03:00
|
|
|
arguments.append(inLayer.source())
|
2017-08-31 15:16:44 +03:00
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2019-02-05 12:48:43 +10:00
|
|
|
self.setOutputValue(self.OUTPUT, out)
|
2016-02-08 19:02:17 +00:00
|
|
|
arguments.append('-of')
|
2017-08-31 15:16:44 +03:00
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
2017-09-06 16:59:48 +03:00
|
|
|
arguments.append('-o')
|
|
|
|
arguments.append(out)
|
2017-08-31 15:16:44 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-near')
|
2017-08-16 11:26:10 +07:00
|
|
|
arguments.append(str(self.parameterAsInt(parameters, self.NEAR, context)))
|
2017-08-31 15:16:44 +03:00
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.WHITE, context):
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-white')
|
2017-08-31 15:16:44 +03:00
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
options = self.parameterAsString(parameters, self.OPTIONS, context)
|
|
|
|
if options:
|
2018-03-21 15:02:39 +10:00
|
|
|
arguments.extend(GdalUtils.parseCreationOptions(options))
|
2017-08-16 11:26:10 +07:00
|
|
|
|
2019-06-24 16:31:27 +03:00
|
|
|
if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
|
|
|
|
extra = self.parameterAsString(parameters, self.EXTRA, context)
|
|
|
|
arguments.append(extra)
|
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
|