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 ParameterRaster
30from processing.core.parameters import ParameterVector
31from processing.core.outputs import OutputVector
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
94 RULE_OPTIONS = [
'centroid',
'point',
'mean',
'proportion',
'count',
'min',
'max',
'mode',
'sum',
'median',
'stdev',
'percentile']
99 SRCNODATA =
"SRCNODATA"
100 BNDNODATA =
"BNDNODATA"
107 return "pkextractogr"
109 def defineCharacteristics(self):
110 self.
name =
"extract vector sample from raster"
111 self.
group =
"[pktools] raster/vector"
112 self.addParameter(ParameterRaster(self.
INPUT,
'Input raster data set'))
113 self.addParameter(ParameterVector(self.
SAMPLE,
'Sample vector data set'))
114 self.addParameter(ParameterBoolean(self.
ITERATE,
"Iterate over all layers",
True))
115 self.addParameter(ParameterSelection(self.
RULE,
"extraction rule",self.
RULE_OPTIONS, 0))
117 self.addOutput(OutputVector(self.
OUTPUT,
'Output vector data set'))
118 self.addParameter(ParameterSelection(self.
FORMAT,
119 'Destination Format', FORMATS))
121 self.addParameter(ParameterBoolean(self.
POLYGON,
"Create OGRPolygon as geometry instead of OGRPoint",
False))
122 self.addParameter(ParameterNumber(self.
BUFFER,
"Buffer for calculating statistics for point features",0,19,0))
123 self.addParameter(ParameterString(self.
SRCNODATA,
"invalid value(s) for input raster dataset (e.g., 0;255)",
"none"))
124 self.addParameter(ParameterString(self.
BNDNODATA,
"Band(s) in input image to check if pixel is valid (e.g., 0;1)",
"0"))
126 self.addParameter(ParameterString(self.
EXTRA,
127 'Additional parameters',
'', optional=
True))
129 def processAlgorithm(self, progress):
130 cliPath =
'"' + os.path.join(pktoolsUtils.pktoolsPath(), self.
cliName()) +
'"'
133 input=self.getParameterValue(self.
INPUT)
134 commands.append(
'-i')
135 commands.append(
'"' + input +
'"')
137 sample=self.getParameterValue(self.
SAMPLE)
138 if self.getParameterValue(self.
ITERATE):
139 if str(sample).find(
'|')>0:
140 samplename=str(sample)[:str(sample).find(
'|')]
142 samplename=str(sample)
144 samplename=str(sample).replace(
"|layername",
" -ln")
145 commands.append(
'-s')
146 commands.append(samplename)
148 if self.getParameterValue(self.
POLYGON):
149 commands.append(
"-polygon")
151 commands.append(
"-r")
154 output = self.getOutputFromName(self.
OUTPUT)
155 outFile = output.value
156 formatIdx = self.getParameterValue(self.
FORMAT)
157 outFormat =
'"' + FORMATS[formatIdx] +
'"'
158 commands.append(
'-f')
159 commands.append(outFormat)
160 ext = EXTS[formatIdx]
161 if not outFile.endswith(ext):
163 output.value = outFile
164 commands.append(
'-o')
165 commands.append(
'"' + outFile +
'"')
167 buffer=self.getParameterValue(self.
BUFFER)
169 commands.append(
"-buf")
170 commands.append(str(buffer))
172 srcnodata=self.getParameterValue(self.
SRCNODATA)
173 if srcnodata !=
"none":
174 srcnodataValues = srcnodata.split(
';')
175 for srcnodataValue
in srcnodataValues:
176 commands.append(
'-srcnodata')
177 commands.append(srcnodataValue)
178 bndnodata=self.getParameterValue(self.
BNDNODATA)
179 bndnodataValues = bndnodata.split(
';')
180 for bndnodataValue
in bndnodataValues:
181 commands.append(
'-bndnodata')
182 commands.append(bndnodataValue)
184 extra = str(self.getParameterValue(self.
EXTRA))
186 commands.append(extra)
188 pktoolsUtils.runpktools(commands, progress)