pktools  2.6.7
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 
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), e.g., 0;1 in case of maxndvi","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 larger 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  cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
85  commands = [cliPath]
86 
87  input=self.getParameterValue(self.INPUT)
88  inputFiles = input.split(';')
89  for inputFile in inputFiles:
90  commands.append('-i')
91  commands.append('"' + inputFile + '"')
92 
93  if self.TYPE[self.getParameterValue(self.RTYPE)] != "none":
94  commands.append('-ot')
95  commands.append(self.TYPE[self.getParameterValue(self.RTYPE)])
96  output=self.getOutputValue(self.OUTPUT)
97  if output != "":
98  commands.append("-o")
99  commands.append('"' + output + '"')
100  commands.append("-cr")
101  commands.append(self.CRULE_OPTIONS[self.getParameterValue(self.CRULE)])
102  if self.getParameterValue(self.DX) != 0:
103  commands.append("-dx")
104  commands.append(str(self.getParameterValue(self.DX)))
105  if self.getParameterValue(self.DY) != 0:
106  commands.append("-dy")
107  commands.append(str(self.getParameterValue(self.DY)))
108  projwin = str(self.getParameterValue(self.PROJWIN))
109  regionCoords = projwin.split(',')
110  commands.append('-ulx')
111  commands.append(regionCoords[0])
112  commands.append('-uly')
113  commands.append(regionCoords[3])
114  commands.append('-lrx')
115  commands.append(regionCoords[1])
116  commands.append('-lry')
117  commands.append(regionCoords[2])
118  cb=self.getParameterValue(self.CB)
119  cbValues = cb.split(';')
120  for cbValue in cbValues:
121  commands.append('-cb')
122  commands.append(cbValue)
123  srcnodata=self.getParameterValue(self.SRCNODATA)
124  if srcnodata != "none":
125  srcnodataValues = srcnodata.split(';')
126  for srcnodataValue in srcnodataValues:
127  commands.append('-srcnodata')
128  commands.append(srcnodataValue)
129  bndnodata=self.getParameterValue(self.BNDNODATA)
130  bndnodataValues = bndnodata.split(';')
131  for bndnodataValue in bndnodataValues:
132  commands.append('-bndnodata')
133  commands.append(bndnodataValue)
134  commands.append('-dstnodata')
135  commands.append(self.getParameterValue(self.DSTNODATA))
136 
137  minGUI=self.getParameterValue(self.MINGUI)
138  if minGUI != "none":
139  minValues = minGUI.split(';')
140  for minValue in minValues:
141  commands.append('-min')
142  commands.append(minValue)
143  maxGUI=self.getParameterValue(self.MAXGUI)
144  if maxGUI != "none":
145  maxValues = maxGUI.split(';')
146  for maxValue in maxValues:
147  commands.append('-max')
148  commands.append(maxValue)
149  commands.append("-r")
150  commands.append(self.RESAMPLE_OPTIONS[self.getParameterValue(self.RESAMPLE)])
151  extra = str(self.getParameterValue(self.EXTRA))
152  if len(extra) > 0:
153  commands.append(extra)
154 
155  pktoolsUtils.runpktools(commands, progress)