4***************************************************************************
8 Copyright : (C) 2015 by Pieter Kempeneers
9 Email : kempenep at gmail dot com
10***************************************************************************
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. *
17***************************************************************************
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$'
27from pktoolsUtils import pktoolsUtils
28from pktoolsAlgorithm import pktoolsAlgorithm
29from processing.core.parameters import ParameterMultipleInput
30from processing.core.parameters import ParameterRaster
31from processing.core.outputs import OutputRaster
32from processing.core.parameters import ParameterSelection
33from processing.core.parameters import ParameterNumber
34from processing.core.parameters import ParameterString
35from processing.core.parameters import ParameterBoolean
36from processing.core.parameters import ParameterExtent
38class pkcrop(pktoolsAlgorithm):
47 RESAMPLE_OPTIONS = [
'near',
'bilinear']
50 TYPE = [
'none',
'Byte',
'Int16',
'UInt16',
'UInt32',
'Int32',
'Float32',
'Float64',
'CInt16',
'CInt32',
'CFloat32',
'CFloat64']
56 def defineCharacteristics(self):
57 self.
name =
"crop raster datasets"
58 self.
group =
"[pktools] raster"
59 self.addParameter(ParameterMultipleInput(self.
INPUT,
'Input layer raster data set',ParameterMultipleInput.TYPE_RASTER))
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.
DX,
"Output resolution in x (leave 0 for no change)",0.0,
None,0.0))
63 self.addParameter(ParameterNumber(self.
DY,
"Output resolution in y (leave 0 for no change)",0.0,
None,0.0))
64 self.addParameter(ParameterExtent(self.
PROJWIN,
65 'Georeferenced boundingbox'))
66 self.addParameter(ParameterString(self.
NODATA,
"invalid value(s) for input raster dataset (e.g., 0;255)",
"none"))
67 self.addParameter(ParameterString(self.
BAND,
"Band(s) in input image to crop, e.g., 0;1;2 (leave empty to retain all bands)",
'', optional=
True))
69 self.addParameter(ParameterString(self.
EXTRA,
70 'Additional parameters',
'-of GTiff', optional=
True))
72 def processAlgorithm(self, progress):
73 cliPath =
'"' + os.path.join(pktoolsUtils.pktoolsPath(), self.
cliName()) +
'"'
76 input=self.getParameterValue(self.
INPUT)
77 inputFiles = input.split(
';')
78 for inputFile
in inputFiles:
80 commands.append(
'"' + inputFile +
'"')
82 if self.
TYPE[self.getParameterValue(self.
RTYPE)] !=
"none":
83 commands.append(
'-ot')
84 commands.append(self.
TYPE[self.getParameterValue(self.
RTYPE)])
85 output=self.getOutputValue(self.
OUTPUT)
88 commands.append(
'"' + output +
'"')
89 if self.getParameterValue(self.
DX) != 0:
90 commands.append(
"-dx")
91 commands.append(str(self.getParameterValue(self.
DX)))
92 if self.getParameterValue(self.
DY) != 0:
93 commands.append(
"-dy")
94 commands.append(str(self.getParameterValue(self.
DY)))
96 projwin = str(self.getParameterValue(self.
PROJWIN))
97 if(str(projwin).find(
',')>0):
98 regionCoords = projwin.split(
',')
99 commands.append(
'-ulx')
100 commands.append(regionCoords[0])
101 commands.append(
'-uly')
102 commands.append(regionCoords[3])
103 commands.append(
'-lrx')
104 commands.append(regionCoords[1])
105 commands.append(
'-lry')
106 commands.append(regionCoords[2])
108 nodata=self.getParameterValue(self.
NODATA)
110 nodataValues = nodata.split(
';')
111 for nodataValue
in nodataValues:
112 commands.append(
'-nodata')
113 commands.append(nodataValue)
115 band=self.getParameterValue(self.
BAND)
117 bandValues = band.split(
';')
118 for bandValue
in bandValues:
119 commands.append(
'-b')
120 commands.append(bandValue)
121 commands.append(
"-r")
124 extra = str(self.getParameterValue(self.
EXTRA))
126 commands.append(extra)
128 pktoolsUtils.runpktools(commands, progress)