| pktools 2.6.7 Processing Kernel for geospatial data

pkcrop.py
1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5 pkcrop.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 pkcrop(pktoolsAlgorithm):
39
40 INPUT = "INPUT"
41 OUTPUT = "OUTPUT"
42 DX = "DX"
43 DY = "DY"
44 PROJWIN = 'PROJWIN'
45 BAND = "BAND"
46 NODATA = "NODATA"
47 RESAMPLE_OPTIONS = ['near', 'bilinear']
48 RESAMPLE = "RESAMPLE"
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 "pkcrop"
55
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))
68 self.addParameter(ParameterSelection(self.RESAMPLE,"resampling method",self.RESAMPLE_OPTIONS, 0))
69 self.addParameter(ParameterString(self.EXTRA,
70 'Additional parameters', '-of GTiff', optional=True))
71
72 def processAlgorithm(self, progress):
73 cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
74 commands = [cliPath]
75
76 input=self.getParameterValue(self.INPUT)
77 inputFiles = input.split(';')
78 for inputFile in inputFiles:
79 commands.append('-i')
80 commands.append('"' + inputFile + '"')
81
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)
86 if output != "":
87 commands.append("-o")
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)))
95
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])
107
108 nodata=self.getParameterValue(self.NODATA)
109 if nodata != "none":
110 nodataValues = nodata.split(';')
111 for nodataValue in nodataValues:
112 commands.append('-nodata')
113 commands.append(nodataValue)
114
115 band=self.getParameterValue(self.BAND)
116 if band != '':
117 bandValues = band.split(';')
118 for bandValue in bandValues:
119 commands.append('-b')
120 commands.append(bandValue)
121 commands.append("-r")
122 commands.append(self.RESAMPLE_OPTIONS[self.getParameterValue(self.RESAMPLE)])
123
124 extra = str(self.getParameterValue(self.EXTRA))
125 if len(extra) > 0:
126 commands.append(extra)
127
128 pktoolsUtils.runpktools(commands, progress)
qgis.pkcrop.pkcrop.RESAMPLE
string RESAMPLE
Definition: pkcrop.py:48
qgis.pkcrop.pkcrop.group
group
Definition: pkcrop.py:58
qgis.pkcrop.pkcrop.name
name
Definition: pkcrop.py:57
qgis.pkcrop.pkcrop.RESAMPLE_OPTIONS
list RESAMPLE_OPTIONS
Definition: pkcrop.py:47
qgis.pkcrop.pkcrop.BAND
string BAND
Definition: pkcrop.py:45
qgis.pkcrop.pkcrop.INPUT
string INPUT
Definition: pkcrop.py:40
qgis.pkcrop.pkcrop.RTYPE
string RTYPE
Definition: pkcrop.py:49
qgis.pkcrop.pkcrop.PROJWIN
string PROJWIN
Definition: pkcrop.py:44
qgis.pkcrop.pkcrop.EXTRA
string EXTRA
Definition: pkcrop.py:51
qgis.pkcrop.pkcrop.cliName
def cliName(self)
Definition: pkcrop.py:53
qgis.pkcrop.pkcrop.NODATA
string NODATA
Definition: pkcrop.py:46
qgis.pkcrop.pkcrop.DX
string DX
Definition: pkcrop.py:42
qgis.pkcrop.pkcrop.TYPE
list TYPE
Definition: pkcrop.py:50
qgis.pkcrop.pkcrop.DY
string DY
Definition: pkcrop.py:43
qgis.pkcrop.pkcrop.OUTPUT
string OUTPUT
Definition: pkcrop.py:41