pktools  2.6.3
Processing Kernel for geospatial data
pkcomposite.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pkcomposite.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 pkcomposite(pktoolsAlgorithm):
39 
40  INPUT = "INPUT"
41  OUTPUT = "OUTPUT"
42  CRULE_OPTIONS = ["overwrite", "maxndvi", "maxband", "minband", "validband", "mean", "mode", "median", "sum", "minallbands", "maxallbands","stdev"]
43  CRULE = "CRULE"
44  DX = "DX"
45  DY = "DY"
46  PROJWIN = 'PROJWIN'
47  CB = "CB"
48  SRCNODATA = "SRCNODATA"
49  BNDNODATA = "BNDNODATA"
50  DSTNODATA = "DSTNODATA"
51  MINGUI = "MINGUI"
52  MAXGUI = "MAXGUI"
53  RESAMPLE_OPTIONS = ['near', 'bilinear']
54  RESAMPLE = "RESAMPLE"
55  RTYPE = 'RTYPE'
56  TYPE = ['none', 'Byte','Int16','UInt16','UInt32','Int32','Float32','Float64','CInt16','CInt32','CFloat32','CFloat64']
57  EXTRA = 'EXTRA'
58 
59  def cliName(self):
60  return "pkcomposite"
61 
62  def defineCharacteristics(self):
63  self.name = "composite/mosaic raster datasets"
64  self.group = "[pktools] raster"
65  self.addParameter(ParameterMultipleInput(self.INPUT, 'Input layer raster data set',ParameterMultipleInput.TYPE_RASTER))
66  self.addParameter(ParameterSelection(self.CRULE,"composite rule",self.CRULE_OPTIONS, 0))
67  self.addOutput(OutputRaster(self.OUTPUT, "Output raster data set"))
68  self.addParameter(ParameterSelection(self.RTYPE, 'Output raster type (leave as none to keep original type)', self.TYPE, 0))
69  self.addParameter(ParameterNumber(self.DX, "Output resolution in x (leave 0 for no change)",0.0,None,0.0))
70  self.addParameter(ParameterNumber(self.DY, "Output resolution in y (leave 0 for no change)",0.0,None,0.0))
71  self.addParameter(ParameterExtent(self.PROJWIN,
72  'Georeferenced boundingbox'))
73  self.addParameter(ParameterString(self.CB, "band index(es) used for the composite rule (0 based)","0"))
74  self.addParameter(ParameterString(self.SRCNODATA, "invalid value(s) for input raster dataset (e.g., 0;255)","none"))
75  self.addParameter(ParameterString(self.BNDNODATA, "Band(s) in input image to check if pixel is valid (e.g., 0;1)","0"))
76  self.addParameter(ParameterString(self.DSTNODATA, "nodata value to put in output raster dataset if not valid or out of bounds","0"))
77  self.addParameter(ParameterString(self.MINGUI, "flag values smaller or equal to this value as invalid","none"))
78  self.addParameter(ParameterString(self.MAXGUI, "flag values smaller or equal to this value as invalid","none"))
79  self.addParameter(ParameterSelection(self.RESAMPLE,"resampling method",self.RESAMPLE_OPTIONS, 0))
80  self.addParameter(ParameterString(self.EXTRA,
81  'Additional parameters', '-of GTiff', optional=True))
82 
83  def processAlgorithm(self, progress):
84  commands = [os.path.join(pktoolsUtils.pktoolsPath(), self.cliName())]
85 
86  input=self.getParameterValue(self.INPUT)
87  inputFiles = input.split(';')
88  for inputFile in inputFiles:
89  commands.append('-i')
90  commands.append(inputFile)
91 
92  if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
93  commands.append('-ot')
94  commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
95  output=self.getParameterValue(self.OUTPUT)
96  if output != "":
97  commands.append("-o")
98  commands.append(self.getOutputValue(self.OUTPUT))
99  commands.append("-cr")
100  commands.append(self.CRULE_OPTIONS[self.getParameterValue(self.CRULE)])
101  if self.getParameterValue(self.DX) != 0:
102  commands.append("-dx")
103  commands.append(str(self.getParameterValue(self.DX)))
104  if self.getParameterValue(self.DY) != 0:
105  commands.append("-dy")
106  commands.append(str(self.getParameterValue(self.DY)))
107  projwin = str(self.getParameterValue(self.PROJWIN))
108  regionCoords = projwin.split(',')
109  commands.append('-ulx')
110  commands.append(regionCoords[0])
111  commands.append('-uly')
112  commands.append(regionCoords[3])
113  commands.append('-lrx')
114  commands.append(regionCoords[1])
115  commands.append('-lry')
116  commands.append(regionCoords[2])
117  cb=self.getParameterValue(self.CB)
118  cbValues = cb.split(';')
119  for cbValue in cbValues:
120  commands.append('-cb')
121  commands.append(cbValue)
122  srcnodata=self.getParameterValue(self.SRCNODATA)
123  if srcnodata != "none":
124  srcnodataValues = srcnodata.split(';')
125  for srcnodataValue in srcnodataValues:
126  commands.append('-srcnodata')
127  commands.append(srcnodataValue)
128  bndnodata=self.getParameterValue(self.BNDNODATA)
129  bndnodataValues = bndnodata.split(';')
130  for bndnodataValue in bndnodataValues:
131  commands.append('-bndnodata')
132  commands.append(bndnodataValue)
133  commands.append('-dstnodata')
134  commands.append(self.getParameterValue(self.DSTNODATA))
135 
136  minGUI=self.getParameterValue(self.MINGUI)
137  if minGUI != "none":
138  minValues = minGUI.split(';')
139  for minValue in minValues:
140  commands.append('-min')
141  commands.append(minValue)
142  maxGUI=self.getParameterValue(self.MAXGUI)
143  if maxGUI != "none":
144  maxValues = maxGUI.split(';')
145  for maxValue in maxValues:
146  commands.append('-max')
147  commands.append(maxValue)
148  commands.append("-r")
149  commands.append(self.RESAMPLE_OPTIONS[self.getParameterValue(self.RESAMPLE)])
150  extra = str(self.getParameterValue(self.EXTRA))
151  if len(extra) > 0:
152  commands.append(extra)
153 
154  f=open('/tmp/a','w')
155  for item in commands:
156  print >> f, item
157  f.close()
158 
159  pktoolsUtils.runpktools(commands, progress)