Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add wrapper vector writer class
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@349 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
alexander.bruy@gmail.com committed Aug 17, 2012
1 parent 8c42295 commit d67298a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 54 deletions.
49 changes: 49 additions & 0 deletions src/sextante/core/SextanteVectorWriter.py
@@ -0,0 +1,49 @@
from PyQt4.QtCore import *

from qgis.core import *

class SextanteVectorWriter:

MEMORY_LAYER_PREFIX = "memory:"

TYPE_MAP = {QGis.WKBPoint : "Point",
QGis.WKBLineString : "LineString",
QGis.WKBPolygon : "Polygon",
QGis.WKBMultiPoint : "MultiPoint",
QGis.WKBMultiLineString : "MultiLineString",
QGis.WKBMultiPolygon : "MultiPolygon"
}

def __init__(self, fileName, encoding, fields, geometryType, crs, options=None):
self.fileName = fileName
self.isMemory = False
self.memLayer = None
self.writer = None

if self.fileName.startswith(self.MEMORY_LAYER_PREFIX):
self.isMemory = True

uri = self.TYPE_MAP[geometryType]
if crs.isValid():
uri += "?crs=" + crs.authid()
self.memLayer = QgsVectorLayer(uri, self.fileName, "memory")
self.writer = self.memLayer.dataProvider()
self.writer.addAttributes(fields.values())
self.memLayer.updateFieldMap()
else:
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
OGRCodes = {}
for key, value in formats.items():
extension = str(key)
extension = extension[extension.find('*.') + 2:]
extension = extension[:extension.find(" ")]
OGRCodes[extension] = value

extension = self.fileName[self.fileName.find(".") + 1:]
self.writer = QgsVectorFileWriter(self.fileName, encoding, fields, geometryType, crs, OGRCodes[extension])

def addFeature(self, feature):
if self.isMemory:
self.writer.addFeatures([feature])
else:
self.writer.addFeature(feature)
3 changes: 0 additions & 3 deletions src/sextante/gui/SextantePostprocessing.py
Expand Up @@ -22,9 +22,6 @@ def handleAlgorithmResults(alg, showResults = True):
try:
if out.value.startswith("memory:"):
layer = out.memoryLayer
layer.updateFieldMap()
layer.commitChanges()
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(layer)
else:
if SextanteConfig.getSetting(SextanteConfig.USE_FILENAME_AS_LAYER_NAME):
Expand Down
76 changes: 25 additions & 51 deletions src/sextante/outputs/OutputVector.py
@@ -1,11 +1,9 @@
from sextante.outputs.Output import Output
from qgis.core import *
from PyQt4.QtCore import *
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

class OutputVector(Output):
from sextante.outputs.Output import Output
from sextante.core.SextanteVectorWriter import SextanteVectorWriter

MEMORY_LAYER_PREFIX = "memory:"
class OutputVector(Output):

def getFileFilter(self,alg):
exts = alg.provider.getSupportedOutputVectorLayerExtensions()
Expand All @@ -17,50 +15,26 @@ def getDefaultFileExtension(self, alg):
return alg.provider.getSupportedOutputVectorLayerExtensions()[0]

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.
Parameters:
-field: an array with the fields of the attributes table or dict of int-QgsField
-geomType: A suitable geometry type, as it would be passed to a QgsVectorFileWriter constructor
-crs: the crs of the layer to create.
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'''
'''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 dict of int-QgsField
@param geomType a suitable geometry type, as it would be passed
to a QgsVectorFileWriter constructor
@param crs the crs of the layer to create
@return writer instance of the vectoe writer class
'''

if self.value.startswith(self.MEMORY_LAYER_PREFIX):
if isinstance(fields, dict):
fields = fields.values()
types = { QGis.WKBPoint : "Point", QGis.WKBLineString : "Point", QGis.WKBPolygon : "Polygon",
QGis.WKBMultiPoint : "MultiPoint", QGis.WKBMultiLineString : "MultiLineString", QGis.WKBMultiPolygon : "MultiPolygon",}
v = QgsVectorLayer(types[geomType], self.description, "memory")
pr = v.dataProvider()
pr.addAttributes(fields)
v.startEditing()
self.memoryLayer = v #keep a reference to the writer
return v
else: #outputChannel is a file path
#TODO: Add support for encodings
check = QFile(self.value)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(self.value):
raise GeoAlgorithmExecutionException("Could not delete existing output file")
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
OGRCodes = {}
for key,value in formats.items():
extension = str(key)
extension = extension[extension.find('*.') + 2:]
extension = extension[:extension.find(" ")]
OGRCodes[extension] = value
if isinstance(fields, dict):
fieldsDict = fields
else:
fieldsDict = {}
i = 0
for field in fields:
fieldsDict[i] = field
i += 1
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
extension = self.value[self.value.find(".")+1:]
return QgsVectorFileWriter(self.value, systemEncoding, fieldsDict, geomType, crs, OGRCodes[extension] )
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()
w = SextanteVectorWriter(self.value, encoding, fields, geomType, crs, options)
self.memoryLayer = w.memLayer
return w

0 comments on commit d67298a

Please sign in to comment.