23
23
# This will get replaced with a git SHA1 when you do a git archive
24
24
__revision__ = '$Format:%H$'
25
25
26
- import csv
27
26
import math
28
- import codecs
29
- import cStringIO
30
27
31
28
from qgis .core import *
32
29
38
35
from processing .parameters .ParameterSelection import ParameterSelection
39
36
from processing .parameters .ParameterTableField import ParameterTableField
40
37
41
- from processing .outputs .OutputFile import OutputFile
38
+ from processing .outputs .OutputTable import OutputTable
42
39
43
40
from processing .algs .ftools import FToolsUtils as utils
44
41
@@ -73,7 +70,7 @@ def defineCharacteristics(self):
73
70
self .addParameter (ParameterSelection (self .MATRIX_TYPE , "Output matrix type" , self .MAT_TYPES , 0 ))
74
71
self .addParameter (ParameterNumber (self .NEAREST_POINTS , "Use only the nearest (k) target points" , 0 , 9999 , 0 ))
75
72
76
- self .addOutput (OutputFile (self .DISTANCE_MATRIX , "Distance matrix" ))
73
+ self .addOutput (OutputTable (self .DISTANCE_MATRIX , "Distance matrix" ))
77
74
78
75
def processAlgorithm (self , progress ):
79
76
inLayer = QGisLayers .getObjectFromUri (self .getParameterValue (self .INPUT_LAYER ))
@@ -83,14 +80,12 @@ def processAlgorithm(self, progress):
83
80
matType = self .getParameterValue (self .MATRIX_TYPE )
84
81
nPoints = self .getParameterValue (self .NEAREST_POINTS )
85
82
86
- outputFile = self .getOutputValue (self .DISTANCE_MATRIX )
83
+ outputFile = self .getOutputFromName (self .DISTANCE_MATRIX )
87
84
88
85
if nPoints < 1 :
89
86
nPoints = len (QGisLayers .features (targetLayer ))
90
87
91
- # prepare CSV file writer
92
- csvFile = open (outputFile , "wb" )
93
- self .writer = UnicodeWriter (csvFile )
88
+ self .writer = outputFile .getTableWriter ([])
94
89
95
90
if matType == 0 : # Linear distance matrix
96
91
self .linearMatrix (inLayer , inField , targetLayer , targetField , matType , nPoints , progress )
@@ -99,14 +94,11 @@ def processAlgorithm(self, progress):
99
94
elif matType == 2 : # Summary distance matrix
100
95
self .linearMatrix (inLayer , inField , targetLayer , targetField , matType , nPoints , progress )
101
96
102
- csvFile .close ()
103
- del self .writer
104
-
105
97
def linearMatrix (self , inLayer , inField , targetLayer , targetField , matType , nPoints , progress ):
106
98
if matType == 0 :
107
- self .writer .writerow (["InputID" , "TargetID" , "Distance" ])
99
+ self .writer .addRecord (["InputID" , "TargetID" , "Distance" ])
108
100
else :
109
- self .writer .writerow (["InputID" , "MEAN" , "STDDEV" , "MIN" , "MAX" ])
101
+ self .writer .addRecord (["InputID" , "MEAN" , "STDDEV" , "MIN" , "MAX" ])
110
102
111
103
index = utils .createSpatialIndex (targetLayer );
112
104
@@ -135,7 +127,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
135
127
outGeom = outFeat .geometry ()
136
128
dist = distArea .measureLine (inGeom .asPoint (), outGeom .asPoint ())
137
129
if matType == 0 :
138
- self .writer .writerow ([unicode (inID ), unicode (outID ), unicode (dist )])
130
+ self .writer .addRecord ([unicode (inID ), unicode (outID ), unicode (dist )])
139
131
else :
140
132
distList .append (float (dist ))
141
133
@@ -144,7 +136,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
144
136
for i in distList :
145
137
vari += (i - mean ) * (i - mean )
146
138
vari = math .sqrt (vari / len (distList ))
147
- self .writer .writerow ([unicode (inID ), unicode (mean ), unicode (vari ), unicode (min (distList )), unicode (max (distList ))])
139
+ self .writer .addRecord ([unicode (inID ), unicode (mean ), unicode (vari ), unicode (min (distList )), unicode (max (distList ))])
148
140
149
141
current += 1
150
142
progress .setPercentage (int (current * total ))
@@ -176,7 +168,7 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro
176
168
request = QgsFeatureRequest ().setFilterFid (i )
177
169
outFeat = targetLayer .getFeatures (request ).next ()
178
170
data .append (unicode (outFeat .attributes [outIdx ]))
179
- self .writer .writerow (data )
171
+ self .writer .addRecord (data )
180
172
181
173
data = [unicode (inID )]
182
174
for i in featList :
@@ -185,29 +177,7 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro
185
177
outGeom = outFeat .geometry ()
186
178
dist = distArea .measureLine (inGeom .asPoint (), outGeom .asPoint ())
187
179
data .append (unicode (float (dist )))
188
- self .writer .writerow (data )
180
+ self .writer .addRecord (data )
189
181
190
182
current += 1
191
183
progress .setPercentage (int (current * total ))
192
-
193
- class UnicodeWriter :
194
- def __init__ (self , f , dialect = csv .excel , encoding = "utf-8" , ** kwds ):
195
- self .queue = cStringIO .StringIO ()
196
- self .writer = csv .writer (self .queue , dialect = dialect , ** kwds )
197
- self .stream = f
198
- self .encoder = codecs .getincrementalencoder (encoding )()
199
-
200
- def writerow (self , row ):
201
- try :
202
- self .writer .writerow ([s .encode ("utf-8" ) for s in row ])
203
- except :
204
- self .writer .writerow (row )
205
- data = self .queue .getvalue ()
206
- data = data .decode ("utf-8" )
207
- data = self .encoder .encode (data )
208
- self .stream .write (data )
209
- self .queue .truncate (0 )
210
-
211
- def writerows (self , rows ):
212
- for row in rows :
213
- self .writerow (row )
0 commit comments