|
| 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) |
0 commit comments