pktools  2.6.7
Processing Kernel for geospatial data
pkfilter_spectral.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pkfilter_spectral.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 
39 
40  INPUT = "INPUT"
41  OUTPUT = "OUTPUT"
42  METHOD_OPTIONS = ["none", "median", "var", "min", "max", "sum", "mean", "dilate", "erode", "close", "open", "smooth", "density", "smoothnodata values", "threshold local filtering", "stdev", "dwt", "dwti", "dwt_cut", "dwt_cut_from", "savgolay", "percentile"]
43  METHOD = "METHOD"
44  DZ = "DZ"
45  NODATA = "NODATA"
46  PADDING_OPTIONS = ["symmetric", "replicate", "circular", "zero"]
47  PADDING = "PADDING"
48  RTYPE = 'RTYPE'
49  TYPE = ['none', 'Byte','Int16','UInt16','UInt32','Int32','Float32','Float64','CInt16','CInt32','CFloat32','CFloat64']
50  EXTRA = 'EXTRA'
51 
52  def cliName(self):
53  return "pkfilter"
54 
55  def defineCharacteristics(self):
56  self.name = "spectral/temporal filter"
57  self.group = "[pktools] filter"
58  self.addParameter(ParameterRaster(self.INPUT, 'Input layer raster data set',ParameterRaster))
59  self.addParameter(ParameterSelection(self.METHOD,"filter rule",self.METHOD_OPTIONS, 0))
60  self.addOutput(OutputRaster(self.OUTPUT, "Output raster data set"))
61  self.addParameter(ParameterSelection(self.RTYPE, 'Output raster type (leave as none to keep original type)', self.TYPE, 0))
62  self.addParameter(ParameterNumber(self.DZ, "Filter kernel size",0.0,None,1.0))
63  #for smooth nodata:
64  self.addParameter(ParameterString(self.NODATA, "nodata value to smooth(e.g., 0;255)","none"))
65  self.addParameter(ParameterSelection(self.PADDING,"Padding (edge effects)",self.PADDING_OPTIONS, 0))
66  self.addParameter(ParameterString(self.EXTRA,
67  'Additional parameters', '-of GTiff', optional=True))
68 
69  def processAlgorithm(self, progress):
70  cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
71  commands = [cliPath]
72 
73  input=self.getParameterValue(self.INPUT)
74  if input != "":
75  commands.append('-i')
76  commands.append('"' + input + '"')
77 
78  method=self.METHOD_OPTIONS[self.getParameterValue(self.METHOD)]
79  if method != "none":
80  commands.append("-f")
81  commands.append(method)
82  commands.append("-pad")
83  commands.append(self.PADDING_OPTIONS[self.getParameterValue(self.PADDING)])
84 
85  if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
86  commands.append('-ot')
87  commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
88  output=self.getOutputValue(self.OUTPUT)
89  if output != "":
90  commands.append("-o")
91  commands.append('"' + output + '"')
92 
93  if self.getParameterValue(self.DZ) != 0:
94  commands.append("-dz")
95  commands.append(str(self.getParameterValue(self.DZ)))
96  nodata=self.getParameterValue(self.NODATA)
97  if nodata != "none":
98  nodataValues = nodata.split(';')
99  for nodataValue in nodataValues:
100  commands.append('-nodata')
101  commands.append(nodataValue)
102  extra = str(self.getParameterValue(self.EXTRA))
103  if len(extra) > 0:
104  commands.append(extra)
105 
106  pktoolsUtils.runpktools(commands, progress)