Skip to content

Commit d67298a

Browse files
author
alexander.bruy@gmail.com
committed
add wrapper vector writer class
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@349 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent 8c42295 commit d67298a

File tree

3 files changed

+74
-54
lines changed

3 files changed

+74
-54
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from PyQt4.QtCore import *
2+
3+
from qgis.core import *
4+
5+
class SextanteVectorWriter:
6+
7+
MEMORY_LAYER_PREFIX = "memory:"
8+
9+
TYPE_MAP = {QGis.WKBPoint : "Point",
10+
QGis.WKBLineString : "LineString",
11+
QGis.WKBPolygon : "Polygon",
12+
QGis.WKBMultiPoint : "MultiPoint",
13+
QGis.WKBMultiLineString : "MultiLineString",
14+
QGis.WKBMultiPolygon : "MultiPolygon"
15+
}
16+
17+
def __init__(self, fileName, encoding, fields, geometryType, crs, options=None):
18+
self.fileName = fileName
19+
self.isMemory = False
20+
self.memLayer = None
21+
self.writer = None
22+
23+
if self.fileName.startswith(self.MEMORY_LAYER_PREFIX):
24+
self.isMemory = True
25+
26+
uri = self.TYPE_MAP[geometryType]
27+
if crs.isValid():
28+
uri += "?crs=" + crs.authid()
29+
self.memLayer = QgsVectorLayer(uri, self.fileName, "memory")
30+
self.writer = self.memLayer.dataProvider()
31+
self.writer.addAttributes(fields.values())
32+
self.memLayer.updateFieldMap()
33+
else:
34+
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
35+
OGRCodes = {}
36+
for key, value in formats.items():
37+
extension = str(key)
38+
extension = extension[extension.find('*.') + 2:]
39+
extension = extension[:extension.find(" ")]
40+
OGRCodes[extension] = value
41+
42+
extension = self.fileName[self.fileName.find(".") + 1:]
43+
self.writer = QgsVectorFileWriter(self.fileName, encoding, fields, geometryType, crs, OGRCodes[extension])
44+
45+
def addFeature(self, feature):
46+
if self.isMemory:
47+
self.writer.addFeatures([feature])
48+
else:
49+
self.writer.addFeature(feature)

src/sextante/gui/SextantePostprocessing.py

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def handleAlgorithmResults(alg, showResults = True):
2222
try:
2323
if out.value.startswith("memory:"):
2424
layer = out.memoryLayer
25-
layer.updateFieldMap()
26-
layer.commitChanges()
27-
layer.updateExtents()
2825
QgsMapLayerRegistry.instance().addMapLayer(layer)
2926
else:
3027
if SextanteConfig.getSetting(SextanteConfig.USE_FILENAME_AS_LAYER_NAME):

src/sextante/outputs/OutputVector.py

+25-51
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from sextante.outputs.Output import Output
2-
from qgis.core import *
31
from PyQt4.QtCore import *
4-
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
52

6-
class OutputVector(Output):
3+
from sextante.outputs.Output import Output
4+
from sextante.core.SextanteVectorWriter import SextanteVectorWriter
75

8-
MEMORY_LAYER_PREFIX = "memory:"
6+
class OutputVector(Output):
97

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

1917
def getVectorWriter(self, fields, geomType, crs, options=None):
20-
'''Returns a suitable writer to which features can be added as a result of the algorithm.
21-
Use this to transparently handle output values instead of creating your own method.
22-
Parameters:
23-
-field: an array with the fields of the attributes table or dict of int-QgsField
24-
-geomType: A suitable geometry type, as it would be passed to a QgsVectorFileWriter constructor
25-
-crs: the crs of the layer to create.
26-
Executing this method might modify the object, adding additional information to it, so the writer
27-
can be later accessed and processed within QGIS.
28-
It should be called just once, since a new call might result in previous data being replaced,
29-
thus rendering a previously obtained writer useless'''
18+
'''Returns a suitable writer to which features can be added as a
19+
result of the algorithm. Use this to transparently handle output
20+
values instead of creating your own method.
21+
22+
Executing this method might modify the object, adding additional
23+
information to it, so the writer can be later accessed and processed
24+
within QGIS. It should be called just once, since a new call might
25+
result in previous data being replaced, thus rendering a previously
26+
obtained writer useless
27+
28+
@param fields a dict of int-QgsField
29+
@param geomType a suitable geometry type, as it would be passed
30+
to a QgsVectorFileWriter constructor
31+
@param crs the crs of the layer to create
32+
33+
@return writer instance of the vectoe writer class
34+
'''
3035

31-
if self.value.startswith(self.MEMORY_LAYER_PREFIX):
32-
if isinstance(fields, dict):
33-
fields = fields.values()
34-
types = { QGis.WKBPoint : "Point", QGis.WKBLineString : "Point", QGis.WKBPolygon : "Polygon",
35-
QGis.WKBMultiPoint : "MultiPoint", QGis.WKBMultiLineString : "MultiLineString", QGis.WKBMultiPolygon : "MultiPolygon",}
36-
v = QgsVectorLayer(types[geomType], self.description, "memory")
37-
pr = v.dataProvider()
38-
pr.addAttributes(fields)
39-
v.startEditing()
40-
self.memoryLayer = v #keep a reference to the writer
41-
return v
42-
else: #outputChannel is a file path
43-
#TODO: Add support for encodings
44-
check = QFile(self.value)
45-
if check.exists():
46-
if not QgsVectorFileWriter.deleteShapeFile(self.value):
47-
raise GeoAlgorithmExecutionException("Could not delete existing output file")
48-
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
49-
OGRCodes = {}
50-
for key,value in formats.items():
51-
extension = str(key)
52-
extension = extension[extension.find('*.') + 2:]
53-
extension = extension[:extension.find(" ")]
54-
OGRCodes[extension] = value
55-
if isinstance(fields, dict):
56-
fieldsDict = fields
57-
else:
58-
fieldsDict = {}
59-
i = 0
60-
for field in fields:
61-
fieldsDict[i] = field
62-
i += 1
63-
settings = QSettings()
64-
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
65-
extension = self.value[self.value.find(".")+1:]
66-
return QgsVectorFileWriter(self.value, systemEncoding, fieldsDict, geomType, crs, OGRCodes[extension] )
36+
settings = QSettings()
37+
encoding = settings.value( "/UI/encoding", "System" ).toString()
38+
w = SextanteVectorWriter(self.value, encoding, fields, geomType, crs, options)
39+
self.memoryLayer = w.memLayer
40+
return w

0 commit comments

Comments
 (0)