Skip to content

Commit 82b1ea0

Browse files
committed
[processing] write table header only when necessary
adopt Point distance alg to use OutputTable
1 parent 8f19acb commit 82b1ea0

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

python/plugins/processing/algs/ftools/PointDistance.py

+10-40
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26-
import csv
2726
import math
28-
import codecs
29-
import cStringIO
3027

3128
from qgis.core import *
3229

@@ -38,7 +35,7 @@
3835
from processing.parameters.ParameterSelection import ParameterSelection
3936
from processing.parameters.ParameterTableField import ParameterTableField
4037

41-
from processing.outputs.OutputFile import OutputFile
38+
from processing.outputs.OutputTable import OutputTable
4239

4340
from processing.algs.ftools import FToolsUtils as utils
4441

@@ -73,7 +70,7 @@ def defineCharacteristics(self):
7370
self.addParameter(ParameterSelection(self.MATRIX_TYPE, "Output matrix type", self.MAT_TYPES, 0))
7471
self.addParameter(ParameterNumber(self.NEAREST_POINTS, "Use only the nearest (k) target points", 0, 9999, 0))
7572

76-
self.addOutput(OutputFile(self.DISTANCE_MATRIX, "Distance matrix"))
73+
self.addOutput(OutputTable(self.DISTANCE_MATRIX, "Distance matrix"))
7774

7875
def processAlgorithm(self, progress):
7976
inLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
@@ -83,14 +80,12 @@ def processAlgorithm(self, progress):
8380
matType = self.getParameterValue(self.MATRIX_TYPE)
8481
nPoints = self.getParameterValue(self.NEAREST_POINTS)
8582

86-
outputFile = self.getOutputValue(self.DISTANCE_MATRIX)
83+
outputFile = self.getOutputFromName(self.DISTANCE_MATRIX)
8784

8885
if nPoints < 1:
8986
nPoints = len(QGisLayers.features(targetLayer))
9087

91-
# prepare CSV file writer
92-
csvFile = open(outputFile, "wb")
93-
self.writer = UnicodeWriter(csvFile)
88+
self.writer = outputFile.getTableWriter([])
9489

9590
if matType == 0: # Linear distance matrix
9691
self.linearMatrix(inLayer, inField, targetLayer, targetField, matType, nPoints, progress)
@@ -99,14 +94,11 @@ def processAlgorithm(self, progress):
9994
elif matType == 2: # Summary distance matrix
10095
self.linearMatrix(inLayer, inField, targetLayer, targetField, matType, nPoints, progress)
10196

102-
csvFile.close()
103-
del self.writer
104-
10597
def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoints, progress):
10698
if matType == 0:
107-
self.writer.writerow(["InputID", "TargetID", "Distance"])
99+
self.writer.addRecord(["InputID", "TargetID", "Distance"])
108100
else:
109-
self.writer.writerow(["InputID", "MEAN", "STDDEV", "MIN", "MAX"])
101+
self.writer.addRecord(["InputID", "MEAN", "STDDEV", "MIN", "MAX"])
110102

111103
index = utils.createSpatialIndex(targetLayer);
112104

@@ -135,7 +127,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
135127
outGeom = outFeat.geometry()
136128
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
137129
if matType == 0:
138-
self.writer.writerow([unicode(inID), unicode(outID), unicode(dist)])
130+
self.writer.addRecord([unicode(inID), unicode(outID), unicode(dist)])
139131
else:
140132
distList.append(float(dist))
141133

@@ -144,7 +136,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
144136
for i in distList:
145137
vari += (i - mean) * (i - mean)
146138
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))])
148140

149141
current += 1
150142
progress.setPercentage(int(current * total))
@@ -176,7 +168,7 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro
176168
request = QgsFeatureRequest().setFilterFid(i)
177169
outFeat = targetLayer.getFeatures(request).next()
178170
data.append(unicode(outFeat.attributes[outIdx]))
179-
self.writer.writerow(data)
171+
self.writer.addRecord(data)
180172

181173
data = [unicode(inID)]
182174
for i in featList:
@@ -185,29 +177,7 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro
185177
outGeom = outFeat.geometry()
186178
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
187179
data.append(unicode(float(dist)))
188-
self.writer.writerow(data)
180+
self.writer.addRecord(data)
189181

190182
current += 1
191183
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)

python/plugins/processing/core/TableWriter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(self, fileName, encoding, fields):
3939

4040
with open(self.fileName, "wb") as csvFile:
4141
self.writer = UnicodeWriter(csvFile, encoding=self.encoding)
42-
self.writer.writerow(fields)
42+
if len(fields) != 0:
43+
self.writer.writerow(fields)
4344

4445
def addRecord(self, values):
4546
with open(self.fileName, "ab") as csvFile:

0 commit comments

Comments
 (0)