16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from builtins import str
20
19
21
20
__author__ = 'Victor Olaya'
22
21
__date__ = 'August 2012'
30
29
31
30
from qgis .PyQt .QtGui import QIcon
32
31
32
+ from qgis .core import (QgsRasterFileWriter ,
33
+ QgsProcessingParameterRasterLayer ,
34
+ QgsProcessingParameterNumber ,
35
+ QgsProcessingParameterBoolean ,
36
+ QgsProcessingParameterRasterDestination )
33
37
from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
34
-
35
- from processing .core .parameters import ParameterRaster
36
- from processing .core .parameters import ParameterSelection
37
- from processing .core .parameters import ParameterNumber
38
- from processing .core .outputs import OutputRaster
39
-
40
38
from processing .tools .system import isWindows
41
-
42
39
from processing .algs .gdal .GdalUtils import GdalUtils
43
40
44
41
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
@@ -48,25 +45,29 @@ class sieve(GdalAlgorithm):
48
45
49
46
INPUT = 'INPUT'
50
47
THRESHOLD = 'THRESHOLD'
51
- CONNECTIONS = 'CONNECTIONS'
48
+ EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
49
+ NO_MASK = 'NO_MASK'
50
+ MASK_LAYER = 'MASK_LAYER'
52
51
OUTPUT = 'OUTPUT'
53
52
54
- PIXEL_CONNECTIONS = ['4' , '8' ]
55
-
56
- def icon (self ):
57
- return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' , 'sieve.png' ))
58
-
59
53
def __init__ (self ):
60
54
super ().__init__ ()
61
55
62
56
def initAlgorithm (self , config = None ):
63
- self .addParameter (ParameterRaster (self .INPUT , self .tr ('Input layer' ), False ))
64
- self .addParameter (ParameterNumber (self .THRESHOLD ,
65
- self .tr ('Threshold' ), 0 , 9999 , 2 ))
66
- self .addParameter (ParameterSelection (self .CONNECTIONS ,
67
- self .tr ('Pixel connection' ), self .PIXEL_CONNECTIONS , 0 ))
68
-
69
- self .addOutput (OutputRaster (self .OUTPUT , self .tr ('Sieved' )))
57
+ self .addParameter (QgsProcessingParameterRasterLayer (self .INPUT , self .tr ('Input layer' )))
58
+ self .addParameter (QgsProcessingParameterNumber (
59
+ self .THRESHOLD , self .tr ('Threshold' ),
60
+ type = QgsProcessingParameterNumber .Integer ,
61
+ minValue = 0 , maxValue = 999999 , defaultValue = 10 ))
62
+ self .addParameter (QgsProcessingParameterBoolean (
63
+ self .EIGHT_CONNECTEDNESS , self .tr ('Use 8-connectedness' ),
64
+ defaultValue = False ))
65
+ self .addParameter (QgsProcessingParameterBoolean (
66
+ self .NO_MASK , self .tr ('Do not use the default validity mask for the input band' ),
67
+ defaultValue = False ))
68
+ self .addParameter (QgsProcessingParameterRasterLayer (self .MASK_LAYER , self .tr ('Validity mask' ), optional = True ))
69
+
70
+ self .addParameter (QgsProcessingParameterRasterDestination (self .OUTPUT , self .tr ('Sieved' )))
70
71
71
72
def name (self ):
72
73
return 'sieve'
@@ -77,22 +78,32 @@ def displayName(self):
77
78
def group (self ):
78
79
return self .tr ('Raster analysis' )
79
80
80
- def getConsoleCommands (self , parameters , context , feedback ):
81
- output = self . getOutputValue ( self . OUTPUT )
81
+ def icon (self ):
82
+ return QIcon ( os . path . join ( pluginPath , 'images' , 'gdaltools' , 'sieve.png' ) )
82
83
84
+ def getConsoleCommands (self , parameters , context , feedback ):
83
85
arguments = []
84
86
arguments .append ('-st' )
85
- arguments .append (str (self .getParameterValue (self .THRESHOLD )))
87
+ arguments .append (str (self .parameterAsInt (self .THRESHOLD )))
88
+
89
+ if self .parameterAsBool (parameters , self .EIGHT_CONNECTEDNESS , context ):
90
+ arguments .append ('-8' )
91
+ else :
92
+ arguments .append ('-4' )
93
+
94
+ if self .parameterAsBool (parameters , self .NO_MASK , context ):
95
+ arguments .append ('-nomask' )
86
96
87
- arguments . append ( '-' +
88
- self . PIXEL_CONNECTIONS [ self . getParameterValue (
89
- self . CONNECTIONS )] )
97
+ mask = self . parameterAsRasterLayer ( parameters , self . INPUT , context )
98
+ if mask :
99
+ arguments . append ( '-mask {}' . format ( mask . source ()) )
90
100
101
+ out = self .parameterAsOutputLayer (parameters , self .OUTPUT , context )
91
102
arguments .append ('-of' )
92
- arguments .append (GdalUtils . getFormatShortNameFromFilename ( output ))
103
+ arguments .append (QgsRasterFileWriter . driverForExtension ( os . path . splitext ( out )[ 1 ] ))
93
104
94
- arguments .append (self .getParameterValue ( self .INPUT ))
95
- arguments .append (output )
105
+ arguments .append (self .parameterAsRasterLayer ( parameters , self .INPUT , context ). source ( ))
106
+ arguments .append (out )
96
107
97
108
commands = []
98
109
if isWindows ():
0 commit comments