pktools  2.6.7
Processing Kernel for geospatial data
pksvm.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pksvm.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 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
39 
40 class pksvm(pktoolsAlgorithm):
41 
42  INPUT = "INPUT"
43  TRAINING = "TRAINING"
44  ITERATE = "ITERATE"
45  LABEL = "LABEL"
46 # CV = "CV"
47  GAMMA = "GAMMA"
48  COST = "COST"
49  OUTPUT = "OUTPUT"
50  MASK = "MASK"
51  MSKNODATA = "MSKNODATA"
52 # NODATA = "NODATA"
53 
54 # SVM_TYPE_OPTIONS = ["C_SVC", "nu_SVC,one_class", "epsilon_SVR", "nu_SVR"]
55 # KERNEL_TYPE_OPTIONS = ["linear", "polynomial", "radial", "sigmoid"]
56  EXTRA = 'EXTRA'
57 
58  def cliName(self):
59  return "pksvm"
60 
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))
75 
76 # self.addParameter(ParameterSelection(self.KERNEL_TYPE,"Type of kernel function (linear,polynomial,radial,sigmoid)",self.KERNEL_TYPE_OPTIONS, 2))
77 # self.addParameter(ParameterSelection(self.SVM_TYPE,"Type of SVM (C_SVC, nu_SVC,one_class, epsilon_SVR, nu_SVR)",self.SVM_TYPE_OPTIONS, 0))
78 
79  def processAlgorithm(self, progress):
80  cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
81  commands = [cliPath]
82 
83  input=self.getParameterValue(self.INPUT)
84  if input != "":
85  commands.append('-i')
86  commands.append('"' + input + '"')
87 
88  commands.append('-t')
89  training=self.getParameterValue(self.TRAINING)
90 
91  if(str(training).find('|')>0):
92  if self.getParameterValue(self.ITERATE):
93  trainingname=str(training)
94  commands.append(trainingname[:trainingname.find('|')])
95  else:
96  trainingname=str(training).replace("|layername"," -ln")
97  commands.append(trainingname)
98  else:
99  commands.append(training)
100 
101  commands.append('-label')
102  commands.append(str(self.getParameterValue(self.LABEL)))
103  # if self.getParameterValue(self.CV):
104  # commands.append("-cv 2")
105  commands.append('-g')
106  commands.append(str(self.getParameterValue(self.GAMMA)))
107  commands.append('-cc')
108  commands.append(str(self.getParameterValue(self.COST)))
109 
110  mask = str(self.getParameterValue(self.MASK))
111  if 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)
119 
120  extra = str(self.getParameterValue(self.EXTRA))
121  if len(extra) > 0:
122  commands.append(extra)
123 
124  output=self.getOutputValue(self.OUTPUT)
125  if output != "":
126  commands.append('-o')
127  commands.append('"' + output + '"')
128 
129  pktoolsUtils.runpktools(commands, progress)
string TRAINING
Definition: pksvm.py:43
string EXTRA
Definition: pksvm.py:56
string MASK
Definition: pksvm.py:50
string GAMMA
Definition: pksvm.py:47
string ITERATE
Definition: pksvm.py:44
string MSKNODATA
Definition: pksvm.py:51
string COST
Definition: pksvm.py:48
string LABEL
Definition: pksvm.py:45
string INPUT
Definition: pksvm.py:42
string OUTPUT
Definition: pksvm.py:49