|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsVectorFileWriter. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Tim Sutton' |
| 10 | +__date__ = '20/08/2012' |
| 11 | +__copyright__ = 'Copyright 2012, The Quantum GIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from PyQt4.QtCore import QVariant, QDir, QString, QStringList |
| 18 | + |
| 19 | +from qgis.core import (QgsVectorLayer, |
| 20 | + QgsFeature, |
| 21 | + QgsField, |
| 22 | + QgsGeometry, |
| 23 | + QgsPoint, |
| 24 | + QgsVectorFileWriter, |
| 25 | + QgsCoordinateReferenceSystem) |
| 26 | + |
| 27 | +from utilities import (unitTestDataPath, |
| 28 | + getQgisTestApp, |
| 29 | + TestCase, |
| 30 | + unittest, |
| 31 | + #expectedFailure |
| 32 | + ) |
| 33 | +QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() |
| 34 | + |
| 35 | + |
| 36 | +class TestQgsVectorLayer(TestCase): |
| 37 | + |
| 38 | + mMemoryLayer = None |
| 39 | + |
| 40 | + def setUp(self): |
| 41 | + self.mMemoryLayer = QgsVectorLayer("Point", "test", "memory") |
| 42 | + myProvider = self.mMemoryLayer.dataProvider() |
| 43 | + |
| 44 | + myProvider.addAttributes([ |
| 45 | + QgsField("name", QVariant.String), |
| 46 | + QgsField("age", QVariant.Int), |
| 47 | + QgsField("size", QVariant.Double)]) |
| 48 | + |
| 49 | + ft = QgsFeature() |
| 50 | + ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10))) |
| 51 | + ft.setAttributeMap({0 : QVariant("Johny"), |
| 52 | + 1 : QVariant(20), |
| 53 | + 2 : QVariant(0.3)}) |
| 54 | + myProvider.addFeatures([ft]) |
| 55 | + |
| 56 | + |
| 57 | + def testWrite(self): |
| 58 | + """Check we can write a vector file.""" |
| 59 | + |
| 60 | + myFileName = os.path.join(str(QDir.tempPath()), 'writetest.shp') |
| 61 | + print myFileName |
| 62 | + # Explicitly giving all options, not really needed but nice for clarity |
| 63 | + myErrorMessage = QString() |
| 64 | + myOptions = QStringList() |
| 65 | + myLayerOptions = QStringList() |
| 66 | + mySelectedOnlyFlag = False |
| 67 | + mySkipAttributesFlag = False |
| 68 | + myGeoCrs = QgsCoordinateReferenceSystem() |
| 69 | + myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) |
| 70 | + myResult = QgsVectorFileWriter.writeAsVectorFormat( |
| 71 | + self.mMemoryLayer, |
| 72 | + myFileName, |
| 73 | + 'utf-8', |
| 74 | + myGeoCrs, |
| 75 | + "ESRI Shapefile", |
| 76 | + mySelectedOnlyFlag, |
| 77 | + myErrorMessage, |
| 78 | + myOptions, |
| 79 | + myLayerOptions, |
| 80 | + mySkipAttributesFlag) |
| 81 | + assert myResult==True |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + unittest.main() |
0 commit comments