Skip to content
Permalink
Browse files
improvements for handling tables
  • Loading branch information
volaya committed Jan 8, 2013
1 parent eab2d8a commit c67fb81d6653623926791aee49863c90507582d7
@@ -56,8 +56,7 @@ def getVectorLayers(shapetype=-1):
if layer.type() == layer.VectorLayer:
if shapetype == QGisLayers.ALL_TYPES or layer.geometryType() == shapetype:
uri = unicode(layer.source())
if not uri.endswith("csv") and not uri.endswith("dbf"):

if not uri.lower().endswith("csv") and not uri.lower().endswith("dbf"):
vector.append(layer)
return vector

@@ -72,11 +71,11 @@ def getAllLayers():
def getTables():
layers = QGisLayers.iface.legendInterface().layers()
tables = list()
for layer in layers:
for layer in layers:
if layer.type() == layer.VectorLayer :
uri = unicode(layer.source())
if uri.endswith("csv") or uri.endswith("dbf"):
tables.append(layer)
if uri.lower().endswith("csv") or uri.lower().endswith("dbf") or uri.lower().endswith("shp"):
tables.append(layer)
return tables

@staticmethod
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
SextanteVectorWriter.py
---------------------
Date : September 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'September 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import *

from PyQt4.QtCore import *

class SextanteTableWriter:

def __init__(self, fileName, encoding, fields):
self.fileName = fileName
self.writer = None

if encoding is None:
settings = QSettings()
encoding = settings.value("/SextanteQGIS/encoding", "System").toString()

if fileName.endswith("csv"):
fileName += ".csv"
file = open(fileName, "w")
file.write(";".join(field.name() for field in fields))
file.write("\n")
file.close()

def addFeature(self, values):
file = open(self.fileName, "a")
file.write(";".join([value.toString() for value in values]))
file.write("\n")
file.close()
@@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.core.SextanteTableWriter import SextanteTableWriter

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -25,6 +26,8 @@

from sextante.outputs.Output import Output

from PyQt4.QtCore import *

class OutputTable(Output):

def getFileFilter(self,alg):
@@ -34,4 +37,20 @@ def getFileFilter(self,alg):
return ";;".join(exts)

def getDefaultFileExtension(self, alg):
return alg.provider.getSupportedOutputTableExtensions()[0]
return alg.provider.getSupportedOutputTableExtensions()[0]

def getTableWriter(self, fields):
'''Returns a suitable writer to which records can be added as a
result of the algorithm. Use this to transparently handle output
values instead of creating your own method.
@param fields a dict of int-QgsField
@return writer instance of the table writer class
'''

if self.encoding is None:
settings = QSettings()
self.encoding = settings.value("/SextanteQGIS/encoding", "System").toString()

return SextanteTableWriter(self.value, self.encoding, fields)
@@ -43,9 +43,12 @@ def setValue(self, obj):
return False
if isinstance(obj, QgsVectorLayer):
source = unicode(obj.source())
if source.endswith("dbf") or source.endswith("csv"):
if source.lower().endswith("dbf") or source.lower().endswith("csv"):
self.value = source
return True
elif source.endswith("shp"):
self.value = source[:-4] + "dbf"
return True
else:
return False
else:

0 comments on commit c67fb81

Please sign in to comment.