| pktools 2.6.7 Processing Kernel for geospatial data

pkfilter_spatial.py
1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5 pkfilter_spatial.py
6 ---------------------
7 Date : April 2015
8 Copyright : (C) 2015 by Pieter Kempeneers
9 Email : kempenep at gmail dot com
10***************************************************************************
11* *
12* This program is free software; you can redistribute it and/or modify *
13* it under the terms of the GNU General Public License as published by *
14* the Free Software Foundation; either version 2 of the License, or *
15* (at your option) any later version. *
16* *
17***************************************************************************
18"""
19
20__author__ = 'Pieter Kempeneers'
21__date__ = 'April 2015'
22__copyright__ = '(C) 2015, Pieter Kempeneers'
23# This will get replaced with a git SHA1 when you do a git archive
24__revision__ = '$Format:%H$'
25
26 import os
27 from pktoolsUtils import pktoolsUtils
28 from pktoolsAlgorithm import pktoolsAlgorithm
29 from processing.core.parameters import ParameterMultipleInput
30 from processing.core.parameters import ParameterRaster
31 from processing.core.outputs import OutputRaster
32 from processing.core.parameters import ParameterSelection
33 from processing.core.parameters import ParameterNumber
34 from processing.core.parameters import ParameterString
35 from processing.core.parameters import ParameterBoolean
36 from processing.core.parameters import ParameterExtent
37
38 class pkfilter_spatial(pktoolsAlgorithm):
39
40 INPUT = "INPUT"
41 OUTPUT = "OUTPUT"
42 METHOD_OPTIONS = ["none", "median", "var", "min", "max", "sum", "mean", "dilate", "erode", "close", "open", "homog ", "heterog ", "sobelx ", "sobely ", "sobelxy ", "sobelyx" , "smooth", "countid", "smoothnodata values", "threshold local filtering", "ismin", "ismax", "order", "mode", "stdev", "mrf", "dwt", "dwti", "dwt_cut", "scramble", "shift", "savgolay", "percentile"]
43 METHOD = "METHOD"
44# RESAMPLE_OPTIONS = ['near', 'bilinear']
45# RESAMPLE = "RESAMPLE"
46 DIM = "DIM"
47 NODATA = "NODATA"
48 PADDING_OPTIONS = ["symmetric", "replicate", "circular", "zero"]
49 PADDING = "PADDING"
50 RTYPE = 'RTYPE'
51 TYPE = ['none', 'Byte','Int16','UInt16','UInt32','Int32','Float32','Float64','CInt16','CInt32','CFloat32','CFloat64']
52 EXTRA = 'EXTRA'
53
54 def cliName(self):
55 return "pkfilter"
56
57 def defineCharacteristics(self):
58 self.name = "spatial filter"
59 self.group = "[pktools] filter"
60
61 self.addParameter(ParameterRaster(self.INPUT, 'Input layer raster data set',ParameterRaster))
62 self.addParameter(ParameterSelection(self.METHOD,"filter rule",self.METHOD_OPTIONS, 0))
63 self.addOutput(OutputRaster(self.OUTPUT, "Output raster data set"))
64 self.addParameter(ParameterSelection(self.RTYPE, 'Output raster type (leave as none to keep original type)', self.TYPE, 0))
65 self.addParameter(ParameterNumber(self.DIM, "Filter kernel size (odd value)",0.0,None,3.0))
66 #for smooth nodata:
67 self.addParameter(ParameterString(self.NODATA, "invalid value(s) for input raster dataset (e.g., 0;255)","none"))
68 self.addParameter(ParameterSelection(self.PADDING,"Padding (edge effects)",self.PADDING_OPTIONS, 0))
69# self.addParameter(ParameterSelection(self.RESAMPLE,"resampling method",self.RESAMPLE_OPTIONS, 0))
70 self.addParameter(ParameterString(self.EXTRA,
71 'Additional parameters', '-of GTiff', optional=True))
72
73 def processAlgorithm(self, progress):
74 cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
75 commands = [cliPath]
76
77 input=self.getParameterValue(self.INPUT)
78 if input != "":
79 commands.append('-i')
80 commands.append('"' + input + '"')
81
82 method=self.METHOD_OPTIONS[self.getParameterValue(self.METHOD)]
83 if method != "none":
84 commands.append("-f")
85 commands.append(method)
86 commands.append("-pad")
87 commands.append(self.PADDING_OPTIONS[self.getParameterValue(self.PADDING)])
88
89 if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
90 commands.append('-ot')
91 commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
92 output=self.getOutputValue(self.OUTPUT)
93 if output != "":
94 commands.append("-o")
95 commands.append('"' + output + '"')
96
97 if self.getParameterValue(self.DIM) != 0:
98 commands.append("-dx")
99 commands.append(str(self.getParameterValue(self.DIM)))
100 commands.append("-dy")
101 commands.append(str(self.getParameterValue(self.DIM)))
102 nodata=self.getParameterValue(self.NODATA)
103 if nodata != "none":
104 nodataValues = nodata.split(';')
105 for nodataValue in nodataValues:
106 commands.append('-nodata')
107 commands.append(nodataValue)
108 extra = str(self.getParameterValue(self.EXTRA))
109 if len(extra) > 0:
110 commands.append(extra)
111
112 pktoolsUtils.runpktools(commands, progress)
qgis.pkfilter_spatial.pkfilter_spatial.OUTPUT
string OUTPUT
Definition: pkfilter_spatial.py:41
qgis.pkfilter_spatial.pkfilter_spatial.METHOD
string METHOD
Definition: pkfilter_spatial.py:43
qgis.pkfilter_spatial.pkfilter_spatial.PADDING_OPTIONS
list PADDING_OPTIONS
Definition: pkfilter_spatial.py:48
qgis.pkfilter_spatial.pkfilter_spatial.EXTRA
string EXTRA
Definition: pkfilter_spatial.py:52
qgis.pkfilter_spatial.pkfilter_spatial.NODATA
string NODATA
Definition: pkfilter_spatial.py:47
qgis.pkfilter_spatial.pkfilter_spatial.PADDING
string PADDING
Definition: pkfilter_spatial.py:49
qgis.pkfilter_spatial.pkfilter_spatial.RTYPE
string RTYPE
Definition: pkfilter_spatial.py:50
qgis.pkfilter_spatial.pkfilter_spatial.INPUT
string INPUT
Definition: pkfilter_spatial.py:40
qgis.pkfilter_spatial.pkfilter_spatial.group
group
Definition: pkfilter_spatial.py:59
qgis.pkfilter_spatial.pkfilter_spatial.name
name
Definition: pkfilter_spatial.py:58
qgis.pkfilter_spatial.pkfilter_spatial.DIM
string DIM
Definition: pkfilter_spatial.py:46
qgis.pkfilter_spatial.pkfilter_spatial.TYPE
list TYPE
Definition: pkfilter_spatial.py:51
qgis.pkfilter_spatial.pkfilter_spatial.cliName
def cliName(self)
Definition: pkfilter_spatial.py:54
qgis.pkfilter_spatial.pkfilter_spatial.METHOD_OPTIONS
list METHOD_OPTIONS
Definition: pkfilter_spatial.py:42