Skip to content
Permalink
Browse files
improved management of tables in SEXTANTE
  • Loading branch information
volaya committed Feb 17, 2013
1 parent d8ec492 commit 3aadcfa005113b4c6fac7fed02620dec178787c5
@@ -17,7 +17,8 @@
***************************************************************************
"""

from sextante.core.Sextante import runalg, runandload, alghelp, alglist, algoptions, load, loadFromAlg, extent, getObjectFromName, getObjectFromUri, features
from sextante.core.Sextante import runalg, runandload, alghelp, alglist, algoptions, load, loadFromAlg, \
extent, getObjectFromName, getObjectFromUri, getobject, getfeatures

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -44,7 +44,8 @@ def exportVectorLayer(layer):
a remote one or db-based (non-file based) one, or if there is a selection and it should be
used, exporting just the selected features.
Currently, the output is restricted to shapefiles, so anything that is not in a shapefile
will get exported'''
will get exported.
It also export to a new file if the original one contains non-ascii characters'''
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = SextanteUtils.getTempFilename("shp")
@@ -91,4 +92,38 @@ def exportRasterLayer(layer):
#TODO:Do the conversion here
return unicode(layer.source())


@staticmethod
def exportTable( table):
'''Takes a QgsVectorLayer and returns the filename to refer to its attributes table,
which allows external apps which support only file-based layers to use it.
It performs the necessary export in case the input layer is not in a standard format
suitable for most applications, it isa remote one or db-based (non-file based) one
Currently, the output is restricted to dbf.
It also export to a new file if the original one contains non-ascii characters'''
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = SextanteUtils.getTempFilename("dbf")
provider = table.dataProvider()
isASCII=True
try:
unicode(table.source()).decode("ascii")
except UnicodeEncodeError:
isASCII=False
isDbf = unicode(table.source()).endswith("dbf") or unicode(table.source()).endswith("shp")
if (not isDbf or not isASCII):
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), QGis.WKBNoGeometry, provider.crs() )
for feat in table.getFeatures():
writer.addFeature(feat)
del writer
return output
else:
filename = unicode(table.source())
if unicode(table.source()).endswith("shp"):
return filename[:-3] + "dbf"
else:
return filename




@@ -99,9 +99,7 @@ def getTables():
tables = list()
for layer in layers:
if layer.type() == layer.VectorLayer :
uri = unicode(layer.source())
if uri.lower().endswith("csv") or uri.lower().endswith("dbf") or uri.lower().endswith("shp"):
tables.append(layer)
tables.append(layer)
return tables

@staticmethod
@@ -416,11 +416,17 @@ def getObjectFromName(name):
def getObjectFromUri(uri):
return QGisLayers.getObjectFromUri(uri, False)

def getobject(uriorname):
ret = getObjectFromName(uriorname)
if ret is None:
ret = getObjectFromUri(uriorname)
return ret

def load(path):
'''Loads a layer into QGIS'''
return QGisLayers.load(path)

def features(layer):
def getfeatures(layer):
return QGisLayers.features(layer)

def loadFromAlg(layersdict):
@@ -40,12 +40,16 @@ def __init__(self, fileName, encoding, fields):
if not fileName.endswith("csv"):
fileName += ".csv"
file = open(fileName, "w")
file.write(";".join(field.name() for field in fields))
file.write(";".join(unicode(field.name()) for field in fields))
file.write("\n")
file.close()

def addRecord(self, values):
for i in range(len(values)):
if isinstance(values[i], QVariant):
values[i] = values[i].toString()

file = open(self.fileName, "a")
file.write(";".join([value.toString() for value in values]))
file.write(";".join([unicode(value) for value in values]))
file.write("\n")
file.close()
@@ -74,17 +74,7 @@ def updateTree(self):
Sextante.updateAlgsList()

def configureProviders(self):
webbrowser.open("http://docs.qgis.org/html/en/user_manual/sextante/3rdParty.html")
#=======================================================================
# #QDesktopServices.openUrl(QUrl(os.path.join(os.path.dirname(__file__), os.path.pardir) + "/help/3rdParty.html"))
# filename = os.path.join(os.path.dirname(__file__), "..", "help", "3rdParty.html")
# if os.name == "nt":
# os.startfile(filename)
# elif sys.platform == "darwin":
# subprocess.Popen(('open', filename))
# else:
# subprocess.call(('xdg-open', filename))
#=======================================================================
webbrowser.open("http://sextantegis.com")

def showPopupMenu(self,point):
item = self.algorithmTree.itemAt(point)
@@ -31,6 +31,7 @@

class OutputTable(Output):

encoding = None
compatible = None

def getFileFilter(self,alg):
@@ -47,7 +48,7 @@ def getCompatibleFileName(self, alg):
If the algorithm supports the file format of the current output value, it returns that value. If not,
it returns a temporary file with a supported file format, to be used to generate the output result.'''
ext = self.value[self.value.rfind(".") + 1:]
if ext in alg.provider.getSupportedOutputTableExtensions:
if ext in alg.provider.getSupportedOutputTableExtensions():
return self.value
else:
if self.compatible is None:
@@ -59,7 +60,7 @@ def getTableWriter(self, fields):
result of the algorithm. Use this to transparently handle output
values instead of creating your own method.
@param fields a dict of int-QgsField
@param fields a list of QgsField
@return writer instance of the table writer class
'''
@@ -17,6 +17,7 @@
***************************************************************************
"""
import os
from sextante.core.LayerExporter import LayerExporter

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -34,8 +35,10 @@ def __init__(self, name="", description="", optional=False):
ParameterDataObject.__init__(self, name, description)
self.optional = optional
self.value = None
self.exported = None

def setValue(self, obj):
self.exported = None
if obj == None:
if self.optional:
self.value = None
@@ -56,6 +59,25 @@ def setValue(self, obj):
val = unicode(obj)
self.value = val
return os.path.exists(self.value)

def getSafeExportedTable(self):
'''Returns not the value entered by the user, but a string with a filename which
contains the data of this table, but saved in a standard format (currently always
a dbf file) so that it can be opened by most external applications.
Works only if the table represented by the parameter value is currently loaded in QGIS.
Otherwise, it will not perform any export and return the current value string.
If the current value represents a table in a suitable format, it does not export at all
and returns that value.
The table is exported just the first time the method is called. The method can be called
several times and it will always return the same file, performing the export only the first time.'''
if self.exported:
return self.exported
table = QGisLayers.getObjectFromUri(self.value, False)
if table:
self.exported = LayerExporter.exportTable(table)
else:
self.exported = self.value
return self.exported

def serialize(self):
return self.__module__.split(".")[-1] + "|" + self.name + "|" + self.description +\
@@ -204,9 +204,12 @@ def processAlgorithm(self, progress):
if isinstance(param, ParameterTable):
if param.value == None:
continue
value = param.value
if not value.endswith("dbf"):
raise GeoAlgorithmExecutionException("Unsupported file format")
table = QGisLayers.getObjectFromUri(param.value, False)
if table:
filename = LayerExporter.exportTable(table)
self.exportedLayers[param.value]=filename
elif not param.value.endswith("shp"):
raise GeoAlgorithmExecutionException("Unsupported file format")
if isinstance(param, ParameterMultipleInput):
if param.value == None:
continue
@@ -237,7 +240,7 @@ def processAlgorithm(self, progress):
for param in self.parameters:
if param.value is None:
continue
if isinstance(param, (ParameterRaster, ParameterVector)):
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)):
value = param.value
if value in self.exportedLayers.keys():
command+=(" -" + param.name + " \"" + self.exportedLayers[value] + "\"")

0 comments on commit 3aadcfa

Please sign in to comment.