pktools  2.6.3
Processing Kernel for geospatial data
pkgetmask.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pkgetmask.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 pkgetmask(pktoolsAlgorithm):
39 
40  INPUT = "INPUT"
41  BAND = "BAND"
42  MIN = "MIN"
43  MAX = "MAX"
44  OPERATOR_OPTIONS = ["OR", "AND"]
45  OPERATOR = "OPERATOR"
46  DATA = "DATA"
47  NODATA = "NODATA"
48  OUTPUT = "OUTPUT"
49  RTYPE = 'RTYPE'
50  TYPE = ['none', 'Byte','Int16','UInt16','UInt32','Int32','Float32','Float64','CInt16','CInt32','CFloat32','CFloat64']
51  EXTRA = 'EXTRA'
52 
53  def cliName(self):
54  return "pkgetmask"
55 
56  def defineCharacteristics(self):
57  self.name = "create mask from raster dataset"
58  self.group = "[pktools] raster"
59  self.addParameter(ParameterRaster(self.INPUT, 'Input layer raster data set',ParameterRaster))
60  self.addParameter(ParameterString(self.BAND, "Band(s) used for mask (e.g., 0;1)","0"))
61  self.addParameter(ParameterString(self.MIN, "Minimum valid value (one value per band)","none"))
62  self.addParameter(ParameterString(self.MAX, "Maximum valid value (one value per band)","none"))
63  self.addParameter(ParameterSelection(self.OPERATOR,"getmask rule",self.OPERATOR_OPTIONS, 0))
64  self.addParameter(ParameterString(self.DATA, "write value(s) for valid pixels (e.g., 0;255)","1"))
65  self.addParameter(ParameterString(self.NODATA, "write value(s) for invalid pixels","0"))
66  self.addOutput(OutputRaster(self.OUTPUT, "Output raster data set"))
67  self.addParameter(ParameterSelection(self.RTYPE, 'Output raster type (leave as none to keep original type)', self.TYPE, 0))
68  self.addParameter(ParameterString(self.EXTRA,
69  'Additional parameters', '-of GTiff', optional=True))
70 
71  def processAlgorithm(self, progress):
72  commands = [os.path.join(pktoolsUtils.pktoolsPath(), self.cliName())]
73  input=self.getParameterValue(self.INPUT)
74  commands.append('-i')
75  commands.append(input)
76 
77  band=self.getParameterValue(self.BAND)
78  bandValues = band.split(';')
79  for bandValue in bandValues:
80  commands.append('-band')
81  commands.append(bandValue)
82  min=self.getParameterValue(self.MIN)
83  if min != "none":
84  minValues = min.split(';')
85  for minValue in minValues:
86  commands.append('-min')
87  commands.append(minValue)
88  max=self.getParameterValue(self.MAX)
89  if max != "none":
90  maxValues = max.split(';')
91  for maxValue in maxValues:
92  commands.append('-max')
93  commands.append(maxValue)
94  commands.append("-p")
95  commands.append(self.OPERATOR_OPTIONS[self.getParameterValue(self.OPERATOR)])
96  if data != "none":
97  dataValues = data.split(';')
98  for dataValue in dataValues:
99  commands.append('-data')
100  commands.append(dataValue)
101  nodata=self.getParameterValue(self.NODATA)
102  if nodata != "none":
103  nodataValues = nodata.split(';')
104  for nodataValue in nodataValues:
105  commands.append('-nodata')
106  commands.append(nodataValue)
107  if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
108  commands.append('-ot')
109  commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
110  output=self.getParameterValue(self.OUTPUT)
111  if output != "":
112  commands.append("-o")
113  commands.append(self.getOutputValue(self.OUTPUT))
114  data=self.getParameterValue(self.DATA)
115 
116  extra = str(self.getParameterValue(self.EXTRA))
117  if len(extra) > 0:
118  commands.append(extra)
119 
120 # commands.append(" |tee /tmp/a")
121  pktoolsUtils.runpktools(commands, progress)