pktools  2.6.3
Processing Kernel for geospatial data
pkreclass.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pkreclass.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 pkreclass(pktoolsAlgorithm):
39 
40  INPUT = "INPUT"
41  OUTPUT = "OUTPUT"
42  CLASS = "CLASS"
43  BAND = "BAND"
44  RECLASS = "RECLASS"
45  MASK = "MASK"
46  MSKNODATA = "MSKNODATA"
47  NODATA = "NODATA"
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 "pkreclass"
54 
55  def defineCharacteristics(self):
56  self.name = "reclass raster datasets"
57  self.group = "[pktools] raster"
58  self.addParameter(ParameterMultipleInput(self.INPUT, 'Input layer raster data set',ParameterMultipleInput.TYPE_RASTER))
59  self.addOutput(OutputRaster(self.OUTPUT, "Output raster data set"))
60  self.addParameter(ParameterString(self.BAND, "Band index(es) to replace, e.g., 0;1;2 (other bands are copied to output)", '0'))
61  self.addParameter(ParameterRaster(self.MASK, "Mask raster dataset",optional=True))
62  self.addParameter(ParameterString(self.MSKNODATA, "Mask value(s) not to consider for classification (e.g., 0;255)","0"))
63  self.addParameter(ParameterString(self.CLASS, "list of classes to reclass, in combination with reclass option, e.g., 0;1;2;3",""))
64  self.addParameter(ParameterString(self.RECLASS, "list of recoded classes, in combination with class option e.g., 10;11;12;13",""))
65  self.addParameter(ParameterNumber(self.NODATA, "nodata value to put in image if not valid",0,None,0))
66 
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 
74  commands.append('-i')
75  commands.append(self.getParameterValue(self.INPUT))
76 
77  if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
78  commands.append('-ot')
79  commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
80 
81  commands.append("-o")
82  commands.append(self.getOutputValue(self.OUTPUT))
83 
84  commands.append('-nodata')
85  commands.append(str(self.getParameterValue(self.NODATA)))
86 
87  band=str(self.getParameterValue(self.BAND))
88  if band != '':
89  bandValues = band.split(';')
90  for bandValue in bandValues:
91  commands.append('-b')
92  commands.append(bandValue)
93 
94  theclass=str(self.getParameterValue(self.CLASS))
95  if theclass != '':
96  classValues = theclass.split(';')
97  for classValue in classValues:
98  commands.append('-c')
99  commands.append(classValue)
100  reclass=str(self.getParameterValue(self.RECLASS))
101  if reclass != '':
102  reclassValues = reclass.split(';')
103  for reclassValue in reclassValues:
104  commands.append('-r')
105  commands.append(reclassValue)
106 
107  mask = str(self.getParameterValue(self.MASK))
108  if mask != "None":
109  commands.append('-m')
110  commands.append(mask)
111  msknodata=str(self.getParameterValue(self.MSKNODATA))
112  msknodataValues = msknodata.split(';')
113  for msknodataValue in msknodataValues:
114  commands.append('-msknodata')
115  commands.append(msknodataValue)
116 
117  extra = str(self.getParameterValue(self.EXTRA))
118  if len(extra) > 0:
119  commands.append(extra)
120 
121  f=open('/tmp/a','w')
122  for item in commands:
123  print >> f, item
124  f.close()
125 
126  pktoolsUtils.runpktools(commands, progress)