2013-09-15 20:20:35 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
sieve.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
|
|
|
|
2013-09-15 20:20:35 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-15 20:20:35 +03: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-09-15 20:20:35 +03:00
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterRasterDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import isWindows
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-09-15 20:20:35 +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 sieve(GdalAlgorithm):
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
THRESHOLD = 'THRESHOLD'
|
2017-08-31 19:43:54 +03:00
|
|
|
EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
|
|
|
|
NO_MASK = 'NO_MASK'
|
|
|
|
MASK_LAYER = 'MASK_LAYER'
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2013-09-15 20:20:35 +03: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-08-31 19:43:54 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(
|
|
|
|
self.THRESHOLD, self.tr('Threshold'),
|
|
|
|
type=QgsProcessingParameterNumber.Integer,
|
|
|
|
minValue=0, maxValue=999999, defaultValue=10))
|
|
|
|
self.addParameter(QgsProcessingParameterBoolean(
|
|
|
|
self.EIGHT_CONNECTEDNESS, self.tr('Use 8-connectedness'),
|
|
|
|
defaultValue=False))
|
|
|
|
self.addParameter(QgsProcessingParameterBoolean(
|
|
|
|
self.NO_MASK, self.tr('Do not use the default validity mask for the input band'),
|
|
|
|
defaultValue=False))
|
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.MASK_LAYER, self.tr('Validity mask'), optional=True))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Sieved')))
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'sieve'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Sieve')
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster analysis')
|
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'sieve.png'))
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback):
|
2013-09-15 20:20:35 +03:00
|
|
|
arguments = []
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-st')
|
2017-08-31 19:43:54 +03:00
|
|
|
arguments.append(str(self.parameterAsInt(self.THRESHOLD)))
|
|
|
|
|
|
|
|
if self.parameterAsBool(parameters, self.EIGHT_CONNECTEDNESS, context):
|
|
|
|
arguments.append('-8')
|
|
|
|
else:
|
|
|
|
arguments.append('-4')
|
|
|
|
|
|
|
|
if self.parameterAsBool(parameters, self.NO_MASK, context):
|
|
|
|
arguments.append('-nomask')
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
mask = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
|
|
|
if mask:
|
|
|
|
arguments.append('-mask {}'.format(mask.source()))
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-of')
|
2017-08-31 19:43:54 +03:00
|
|
|
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2017-08-31 19:43:54 +03:00
|
|
|
arguments.append(self.parameterAsRasterLayer(parameters, self.INPUT, context).source())
|
|
|
|
arguments.append(out)
|
2013-09-15 20:20:35 +03:00
|
|
|
|
|
|
|
commands = []
|
|
|
|
if isWindows():
|
2013-10-01 20:52:22 +03:00
|
|
|
commands = ['cmd.exe', '/C ', 'gdal_sieve.bat',
|
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
2013-09-15 20:20:35 +03:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
commands = ['gdal_sieve.py', GdalUtils.escapeAndJoin(arguments)]
|
2013-09-15 20:20:35 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|