QGIS/python/plugins/sextante/outputs/OutputVector.py

88 lines
3.6 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
OutputVector.py
---------------------
Date : August 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. *
* *
***************************************************************************
"""
from sextante.core.QGisLayers import QGisLayers
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from qgis.core import *
2012-09-15 18:25:25 +03:00
from sextante.outputs.Output import Output
from sextante.core.SextanteVectorWriter import SextanteVectorWriter
from sextante.core.SextanteUtils import SextanteUtils
2012-09-15 18:25:25 +03:00
class OutputVector(Output):
encoding = None
compatible = None
2012-09-15 18:25:25 +03:00
2013-02-07 01:09:39 +01:00
def getFileFilter(self,alg):
2013-01-26 23:55:14 +01:00
exts = QGisLayers.getSupportedOutputVectorLayerExtensions()
2012-09-15 18:25:25 +03:00
for i in range(len(exts)):
exts[i] = exts[i].upper() + " files(*." + exts[i].lower() + ")"
return ";;".join(exts)
2013-02-07 01:09:39 +01:00
2012-09-15 18:25:25 +03:00
def getDefaultFileExtension(self, alg):
return alg.provider.getSupportedOutputVectorLayerExtensions()[0]
2013-02-07 01:09:39 +01:00
def getCompatibleFileName(self, alg):
'''Returns a filename that is compatible with the algorithm that is going to generate this output.
2013-02-07 01:09:39 +01:00
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.'''
2013-01-26 23:55:14 +01:00
ext = self.value[self.value.rfind(".") + 1:]
if ext in alg.provider.getSupportedOutputVectorLayerExtensions():
return self.value
else:
2013-02-07 01:09:39 +01:00
if self.compatible is None:
self.compatible = SextanteUtils.getTempFilename(self.getDefaultFileExtension(alg))
return self.compatible;
2013-02-07 01:09:39 +01:00
2012-09-15 18:25:25 +03:00
def getVectorWriter(self, fields, geomType, crs, options=None):
'''Returns a suitable writer to which features can be added as a
result of the algorithm. Use this to transparently handle output
values instead of creating your own method.
Executing this method might modify the object, adding additional
information to it, so the writer can be later accessed and processed
within QGIS. It should be called just once, since a new call might
result in previous data being replaced, thus rendering a previously
obtained writer useless
@param fields a list of QgsField
2012-09-15 18:25:25 +03:00
@param geomType a suitable geometry type, as it would be passed
to a QgsVectorFileWriter constructor
@param crs the crs of the layer to create
2013-01-12 12:42:00 +01:00
@return writer instance of the vector writer class
2012-09-15 18:25:25 +03:00
'''
if self.encoding is None:
settings = QSettings()
self.encoding = settings.value("/SextanteQGIS/encoding", "System").toString()
w = SextanteVectorWriter(self.value, self.encoding, fields, geomType, crs, options)
self.memoryLayer = w.memLayer
return w