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' 24 __revision__ =
'$Format:%H$' 27 from pktoolsUtils
import pktoolsUtils
28 from pktoolsAlgorithm
import pktoolsAlgorithm
29 from processing.core.parameters
import ParameterMultipleInput
30 from processing.core.parameters
import ParameterVector
31 from processing.core.parameters
import ParameterRaster
32 from processing.core.outputs
import OutputRaster
33 from processing.core.parameters
import ParameterSelection
34 from processing.core.parameters
import ParameterFile
35 from processing.core.parameters
import ParameterNumber
36 from processing.core.parameters
import ParameterString
37 from processing.core.parameters
import ParameterBoolean
38 from processing.core.parameters
import ParameterExtent
51 MSKNODATA =
"MSKNODATA" 61 def defineCharacteristics(self):
62 self.
name =
"Support vector machine" 63 self.
group =
"[pktools] supervised classification" 64 self.addParameter(ParameterRaster(self.
INPUT,
'Input layer raster data set',ParameterRaster))
65 self.addParameter(ParameterVector(self.
TRAINING,
'Training vector file.'))
66 self.addParameter(ParameterBoolean(self.
ITERATE,
"Iterate over all layers",
True))
67 self.addParameter(ParameterString(self.
LABEL,
"Attribute name for class label in training vector file",
"label"))
68 self.addParameter(ParameterNumber(self.
GAMMA,
"Gamma in kernel function",0,100,1.0))
69 self.addParameter(ParameterNumber(self.
COST,
"The parameter C of C_SVC",0,100000,1000.0))
70 self.addParameter(ParameterFile(self.
MASK,
"Mask vector/raster dataset used for classification",
"None",optional=
True))
71 self.addParameter(ParameterString(self.
MSKNODATA,
"Mask value(s) not to consider for classification (in case of raster mask, e.g., 0;255)",
"0"))
72 self.addOutput(OutputRaster(self.
OUTPUT,
"Output raster data set"))
73 self.addParameter(ParameterString(self.
EXTRA,
74 'Additional parameters',
'-of GTiff', optional=
True))
79 def processAlgorithm(self, progress):
80 cliPath =
'"' + os.path.join(pktoolsUtils.pktoolsPath(), self.
cliName()) +
'"' 83 input=self.getParameterValue(self.
INPUT)
86 commands.append(
'"' + input +
'"')
89 training=self.getParameterValue(self.
TRAINING)
91 if(str(training).find(
'|')>0):
92 if self.getParameterValue(self.
ITERATE):
93 trainingname=str(training)
94 commands.append(trainingname[:trainingname.find(
'|')])
96 trainingname=str(training).replace(
"|layername",
" -ln")
97 commands.append(trainingname)
99 commands.append(training)
101 commands.append(
'-label')
102 commands.append(str(self.getParameterValue(self.
LABEL)))
105 commands.append(
'-g')
106 commands.append(str(self.getParameterValue(self.
GAMMA)))
107 commands.append(
'-cc')
108 commands.append(str(self.getParameterValue(self.
COST)))
110 mask = str(self.getParameterValue(self.
MASK))
112 commands.append(
'-m')
113 commands.append(mask)
114 msknodata=str(self.getParameterValue(self.
MSKNODATA))
115 msknodataValues = msknodata.split(
';')
116 for msknodataValue
in msknodataValues:
117 commands.append(
'-msknodata')
118 commands.append(msknodataValue)
120 extra = str(self.getParameterValue(self.
EXTRA))
122 commands.append(extra)
124 output=self.getOutputValue(self.
OUTPUT)
126 commands.append(
'-o')
127 commands.append(
'"' + output +
'"')
129 pktoolsUtils.runpktools(commands, progress)