| pktools 2.6.7 Processing Kernel for geospatial data

pkdiff_accuracy.py
1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5 pkdiff_accuracy.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
28 from PyQt4.QtCore import QVariant
29 from qgis.core import QgsField
30
31 from pktoolsUtils import pktoolsUtils
32 from pktoolsAlgorithm import pktoolsAlgorithm
33 from processing.core.parameters import ParameterRaster
34 from processing.core.parameters import ParameterVector
35 from processing.core.outputs import OutputVector
36 from processing.core.outputs import OutputFile
37#from processing.core.outputs import OutputTable
38 from processing.core.parameters import ParameterSelection
39 from processing.core.parameters import ParameterNumber
40 from processing.core.parameters import ParameterString
41 from processing.core.parameters import ParameterBoolean
42 from processing.core.parameters import ParameterExtent
43
44 FORMATS = [
45 'ESRI Shapefile',
46 'GeoJSON',
47 'GeoRSS',
48 'SQLite',
49 'GMT',
50 'MapInfo File',
51 'INTERLIS 1',
52 'INTERLIS 2',
53 'GML',
54 'Geoconcept',
55 'DXF',
56 'DGN',
57 'CSV',
58 'BNA',
59 'S57',
60 'KML',
61 'GPX',
62 'PGDump',
63 'GPSTrackMaker',
64 'ODS',
65 'XLSX',
66 'PDF',
67]
68EXTS = [
69 '.shp',
70 '.geojson',
71 '.xml',
72 '.sqlite',
73 '.gmt',
74 '.tab',
75 '.ili',
76 '.ili',
77 '.gml',
78 '.txt',
79 '.dxf',
80 '.dgn',
81 '.csv',
82 '.bna',
83 '.000',
84 '.kml',
85 '.gpx',
86 '.pgdump',
87 '.gtm',
88 '.ods',
89 '.xlsx',
90 '.pdf',
91]
92
93 class pkdiff_accuracy(pktoolsAlgorithm):
94
95 INPUT = "INPUT"
96 REFERENCE = "REFERENCE"
97 ITERATE = "ITERATE"
98 LABELREF = "LABELREF"
99 NODATA = "NODATA"
100# TABLE = 'TABLE'
101 OUTPUT = "OUTPUT"
102 CMOUTPUT = "CMOUTPUT"
103 CMFORMAT_OPTIONS = ["ascii", "latex"]
104 CMFORMAT = "CMFORMAT"
105
106 FORMAT = "FORMAT"
107 LABELCLASS = "LABELCLASS"
108 EXTRA = 'EXTRA'
109
110 def cliName(self):
111 return "pkdiff"
112
113 def defineCharacteristics(self):
114 self.name = "Accuracy assessment with ground reference"
115 self.group = "[pktools] supervised classification"
116 self.addParameter(ParameterRaster(self.INPUT, 'Classification result (raster map)'))
117 self.addParameter(ParameterVector(self.REFERENCE, 'Labeled reference vector data set'))
118 self.addParameter(ParameterBoolean(self.ITERATE, "Iterate over all layers",True))
119 self.addParameter(ParameterString(self.LABELREF, "Attribute name of the reference label","label"))
120 self.addParameter(ParameterString(self.NODATA, "No data value(s) in input or reference dataset to ignore (e.g., 0;255)","0"))
121 self.addOutput(OutputFile(self.CMOUTPUT, self.tr("Confusion matrix output file ")))
122 self.addParameter(ParameterSelection(self.CMFORMAT,"Format for confusion matrix output",self.CMFORMAT_OPTIONS, 0))
123
124# self.addOutput(OutputTable(self.TABLE, self.tr('Confusion matrix table')))
125 self.addOutput(OutputVector(self.OUTPUT, 'Assessment output vector data set'))
126 self.addParameter(ParameterSelection(self.FORMAT,
127 'Assessment output vector Format', FORMATS))
128 self.addParameter(ParameterString(self.LABELCLASS, "Attribute name of classified (map) label","class"))
129 self.addParameter(ParameterString(self.EXTRA,
130 'Additional parameters', '', optional=True))
131
132 def processAlgorithm(self, progress):
133 cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
134 commands = [cliPath]
135 #outputtable = self.getOutputFromName(self.TABLE)
136
137 input=self.getParameterValue(self.INPUT)
138 commands.append('-i')
139 commands.append('"' + input + '"')
140
141 reference=self.getParameterValue(self.REFERENCE)
142 if self.getParameterValue(self.ITERATE):
143 if str(reference).find('|')>0:
144 referencename=str(reference)[:str(reference).find('|')]
145 else:
146 referencename=str(reference)
147 else:
148 referencename=str(reference).replace("|layername"," -ln")
149 commands.append('-ref')
150 commands.append(referencename)
151
152 commands.append('-lr');
153 commands.append(self.getParameterValue(self.LABELREF))
154
155 nodata=self.getParameterValue(self.NODATA)
156 if nodata != "none":
157 nodataValues = nodata.split(';')
158 for nodataValue in nodataValues:
159 commands.append('-nodata')
160 commands.append(nodataValue)
161
162 commands.append("-cm")
163 commands.append("-cmf")
164 commands.append(self.CMFORMAT_OPTIONS[self.getParameterValue(self.CMFORMAT)])
165 commands.append("-cmo")
166 commands.append(self.getOutputValue(self.CMOUTPUT))
167
168 output = self.getOutputFromName(self.OUTPUT)
169 outFile = output.value
170 formatIdx = self.getParameterValue(self.FORMAT)
171 outFormat = '"' + FORMATS[formatIdx] + '"'
172 commands.append('-f')
173 commands.append(outFormat)
174 ext = EXTS[formatIdx]
175 if not outFile.endswith(ext):
176 outFile += ext
177 output.value = outFile
178 commands.append('-o')
179 commands.append('"' + outFile + '"')
180 commands.append('-lc');
181 commands.append(self.getParameterValue(self.LABELCLASS))
182
183 extra = str(self.getParameterValue(self.EXTRA))
184 if len(extra) > 0:
185 commands.append(extra)
186
187 pktoolsUtils.runpktools(commands, progress)
qgis.pkdiff_accuracy.pkdiff_accuracy
Definition: pkdiff_accuracy.py:93
qgis.pkdiff_accuracy.pkdiff_accuracy.NODATA
string NODATA
Definition: pkdiff_accuracy.py:99
qgis.pkdiff_accuracy.pkdiff_accuracy.CMFORMAT
string CMFORMAT
Definition: pkdiff_accuracy.py:104
qgis.pkdiff_accuracy.pkdiff_accuracy.OUTPUT
string OUTPUT
Definition: pkdiff_accuracy.py:101
qgis.pkdiff_accuracy.pkdiff_accuracy.ITERATE
string ITERATE
Definition: pkdiff_accuracy.py:97
qgis.pkdiff_accuracy.pkdiff_accuracy.CMFORMAT_OPTIONS
list CMFORMAT_OPTIONS
Definition: pkdiff_accuracy.py:103
qgis.pkdiff_accuracy.pkdiff_accuracy.INPUT
string INPUT
Definition: pkdiff_accuracy.py:95
qgis.pkdiff_accuracy.pkdiff_accuracy.group
group
Definition: pkdiff_accuracy.py:115
qgis.pkdiff_accuracy.pkdiff_accuracy.LABELCLASS
string LABELCLASS
Definition: pkdiff_accuracy.py:107
qgis.pkdiff_accuracy.pkdiff_accuracy.CMOUTPUT
string CMOUTPUT
Definition: pkdiff_accuracy.py:102
qgis.pkdiff_accuracy.pkdiff_accuracy.EXTRA
string EXTRA
Definition: pkdiff_accuracy.py:108
qgis.pkdiff_accuracy.pkdiff_accuracy.cliName
def cliName(self)
Definition: pkdiff_accuracy.py:110
qgis.pkdiff_accuracy.pkdiff_accuracy.LABELREF
string LABELREF
Definition: pkdiff_accuracy.py:98
qgis.pkdiff_accuracy.pkdiff_accuracy.name
name
Definition: pkdiff_accuracy.py:114
qgis.pkdiff_accuracy.pkdiff_accuracy.FORMAT
string FORMAT
Definition: pkdiff_accuracy.py:106
qgis.pkdiff_accuracy.pkdiff_accuracy.REFERENCE
string REFERENCE
Definition: pkdiff_accuracy.py:96
qgis.pktoolsAlgorithm.pktoolsAlgorithm
Definition: pktoolsAlgorithm.py:41