Skip to content

Commit

Permalink
[processing] write table header only when necessary
Browse files Browse the repository at this point in the history
adopt Point distance alg to use OutputTable
  • Loading branch information
alexbruy committed Sep 6, 2013
1 parent 8f19acb commit 82b1ea0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 41 deletions.
50 changes: 10 additions & 40 deletions python/plugins/processing/algs/ftools/PointDistance.py
Expand Up @@ -23,10 +23,7 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import csv
import math
import codecs
import cStringIO

from qgis.core import *

Expand All @@ -38,7 +35,7 @@
from processing.parameters.ParameterSelection import ParameterSelection
from processing.parameters.ParameterTableField import ParameterTableField

from processing.outputs.OutputFile import OutputFile
from processing.outputs.OutputTable import OutputTable

from processing.algs.ftools import FToolsUtils as utils

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

self.addOutput(OutputFile(self.DISTANCE_MATRIX, "Distance matrix"))
self.addOutput(OutputTable(self.DISTANCE_MATRIX, "Distance matrix"))

def processAlgorithm(self, progress):
inLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
Expand All @@ -83,14 +80,12 @@ def processAlgorithm(self, progress):
matType = self.getParameterValue(self.MATRIX_TYPE)
nPoints = self.getParameterValue(self.NEAREST_POINTS)

outputFile = self.getOutputValue(self.DISTANCE_MATRIX)
outputFile = self.getOutputFromName(self.DISTANCE_MATRIX)

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

# prepare CSV file writer
csvFile = open(outputFile, "wb")
self.writer = UnicodeWriter(csvFile)
self.writer = outputFile.getTableWriter([])

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

csvFile.close()
del self.writer

def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoints, progress):
if matType == 0:
self.writer.writerow(["InputID", "TargetID", "Distance"])
self.writer.addRecord(["InputID", "TargetID", "Distance"])
else:
self.writer.writerow(["InputID", "MEAN", "STDDEV", "MIN", "MAX"])
self.writer.addRecord(["InputID", "MEAN", "STDDEV", "MIN", "MAX"])

index = utils.createSpatialIndex(targetLayer);

Expand Down Expand Up @@ -135,7 +127,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
outGeom = outFeat.geometry()
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
if matType == 0:
self.writer.writerow([unicode(inID), unicode(outID), unicode(dist)])
self.writer.addRecord([unicode(inID), unicode(outID), unicode(dist)])
else:
distList.append(float(dist))

Expand All @@ -144,7 +136,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
for i in distList:
vari += (i - mean) * (i - mean)
vari = math.sqrt(vari / len(distList))
self.writer.writerow([unicode(inID), unicode(mean), unicode(vari), unicode(min(distList)), unicode(max(distList))])
self.writer.addRecord([unicode(inID), unicode(mean), unicode(vari), unicode(min(distList)), unicode(max(distList))])

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

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

current += 1
progress.setPercentage(int(current * total))

class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
try:
self.writer.writerow([s.encode("utf-8") for s in row])
except:
self.writer.writerow(row)
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)

def writerows(self, rows):
for row in rows:
self.writerow(row)
3 changes: 2 additions & 1 deletion python/plugins/processing/core/TableWriter.py
Expand Up @@ -39,7 +39,8 @@ def __init__(self, fileName, encoding, fields):

with open(self.fileName, "wb") as csvFile:
self.writer = UnicodeWriter(csvFile, encoding=self.encoding)
self.writer.writerow(fields)
if len(fields) != 0:
self.writer.writerow(fields)

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

0 comments on commit 82b1ea0

Please sign in to comment.