From 3043e85ea10d00b4c88021912fd8273d5d50c41d Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Mon, 18 Sep 2017 18:31:46 +0200 Subject: [PATCH 1/4] Return error message from QgsVectorFileWriter --- python/core/qgsvectorfilewriter.sip | 6 +++--- src/core/qgsvectorfilewriter.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/core/qgsvectorfilewriter.sip b/python/core/qgsvectorfilewriter.sip index 1610b46a75d..0dea1612be0 100644 --- a/python/core/qgsvectorfilewriter.sip +++ b/python/core/qgsvectorfilewriter.sip @@ -226,7 +226,7 @@ Constructor const QgsCoordinateReferenceSystem &destCRS = QgsCoordinateReferenceSystem(), const QString &driverName = "ESRI Shapefile", bool onlySelected = false, - QString *errorMessage = 0, + QString *errorMessage /Out/ = 0, const QStringList &datasourceOptions = QStringList(), const QStringList &layerOptions = QStringList(), bool skipAttributeCreation = false, @@ -271,7 +271,7 @@ Constructor const QgsCoordinateTransform &ct, const QString &driverName = "ESRI Shapefile", bool onlySelected = false, - QString *errorMessage = 0, + QString *errorMessage /Out/ = 0, const QStringList &datasourceOptions = QStringList(), const QStringList &layerOptions = QStringList(), bool skipAttributeCreation = false, @@ -431,7 +431,7 @@ Optional feedback object allowing cancelation of layer save const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename = 0, - QString *errorMessage = 0 ); + QString *errorMessage /Out/ = 0 ); %Docstring Writes a layer out to a vector file. \param layer source layer to write diff --git a/src/core/qgsvectorfilewriter.h b/src/core/qgsvectorfilewriter.h index 7e29b6893ca..a4a6f34a75d 100644 --- a/src/core/qgsvectorfilewriter.h +++ b/src/core/qgsvectorfilewriter.h @@ -276,7 +276,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink const QgsCoordinateReferenceSystem &destCRS = QgsCoordinateReferenceSystem(), const QString &driverName = "ESRI Shapefile", bool onlySelected = false, - QString *errorMessage = nullptr, + QString *errorMessage SIP_OUT = nullptr, const QStringList &datasourceOptions = QStringList(), const QStringList &layerOptions = QStringList(), bool skipAttributeCreation = false, @@ -321,7 +321,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink const QgsCoordinateTransform &ct, const QString &driverName = "ESRI Shapefile", bool onlySelected = false, - QString *errorMessage = nullptr, + QString *errorMessage SIP_OUT = nullptr, const QStringList &datasourceOptions = QStringList(), const QStringList &layerOptions = QStringList(), bool skipAttributeCreation = false, @@ -423,7 +423,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename = nullptr, - QString *errorMessage = nullptr ); + QString *errorMessage SIP_OUT = nullptr ); //! Create a new vector file writer QgsVectorFileWriter( const QString &vectorFileName, From 37e522e77e3f0378215537b1ba9b89aced448d00 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Mon, 18 Sep 2017 18:34:22 +0200 Subject: [PATCH 2/4] Document API breaks --- doc/api_break.dox | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/api_break.dox b/doc/api_break.dox index 887b39addb0..ea7cde03e5a 100644 --- a/doc/api_break.dox +++ b/doc/api_break.dox @@ -2550,6 +2550,8 @@ in code which previously passed a null pointer to QgsVectorFileWriter. - setSymbologyScaleDenominator() and symbologyScaleDenominator() were renamed to setSymbologyScale() and symbologyScale() for consistency with other parts of the QGIS API. - The addFeature which takes a renderer argument was renamed to addFeatureWithStyle. +- static `writeAsVectorFormat` calls no longer take a errorMessage argument in + python and instead return a `(errorCode, errorMessage)` tuple. QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode} From 44e32e18c0fc5079455dc7b59c9e5c70d41fc018 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 19 Sep 2017 11:00:49 +0200 Subject: [PATCH 3/4] Fix tests for new QgsVectorFileWriter python API --- tests/src/python/test_qgsvectorfilewriter.py | 68 ++++++++++---------- tests/src/python/utilities.py | 6 +- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/tests/src/python/test_qgsvectorfilewriter.py b/tests/src/python/test_qgsvectorfilewriter.py index 7f081f1a7df..d5315e359e3 100755 --- a/tests/src/python/test_qgsvectorfilewriter.py +++ b/tests/src/python/test_qgsvectorfilewriter.py @@ -115,13 +115,13 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'datetime.shp') crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile') - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -159,11 +159,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.filterExtent = QgsRectangle(-111, 26, -96, 38) dest_file_name = os.path.join(str(QDir.tempPath()), 'extent_no_transform.shp') - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( source_layer, dest_file_name, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -184,11 +184,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.ct = QgsCoordinateTransform(source_layer.crs(), QgsCoordinateReferenceSystem.fromEpsgId(3785)) dest_file_name = os.path.join(str(QDir.tempPath()), 'extent_transform.shp') - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( source_layer, dest_file_name, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -220,13 +220,13 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'datetime.tab') crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'MapInfo File') - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -274,14 +274,14 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'point_{}.shp'.format(QgsWkbTypes.displayString(t))) crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile', overrideGeometryType=t) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -299,13 +299,13 @@ class TestQgsVectorFileWriter(unittest.TestCase): 'point_{}_copy.shp'.format(QgsWkbTypes.displayString(t))) crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( created_layer, dest_file_name, 'utf-8', crs, 'ESRI Shapefile') - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer_from_shp = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -337,14 +337,14 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'to_multi.shp') crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile', forceMulti=True) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -379,14 +379,14 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'all_attributes.shp') crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile', attributes=[]) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -399,14 +399,14 @@ class TestQgsVectorFileWriter(unittest.TestCase): # now test writing out only a subset of attributes dest_file_name = os.path.join(str(QDir.tempPath()), 'subset_attributes.shp') - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile', attributes=[1, 3]) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -417,14 +417,14 @@ class TestQgsVectorFileWriter(unittest.TestCase): # finally test writing no attributes dest_file_name = os.path.join(str(QDir.tempPath()), 'no_attributes.shp') - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'ESRI Shapefile', skipAttributeCreation=True) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -463,7 +463,7 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'value_converter.shp') converter = TestFieldValueConverter(ml) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', @@ -471,7 +471,7 @@ class TestQgsVectorFileWriter(unittest.TestCase): 'ESRI Shapefile', attributes=[0, 2], fieldValueConverter=converter) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -501,13 +501,13 @@ class TestQgsVectorFileWriter(unittest.TestCase): dest_file_name = os.path.join(str(QDir.tempPath()), 'integer64.tab') crs = QgsCoordinateReferenceSystem() crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, dest_file_name, 'utf-8', crs, 'MapInfo File') - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) # Open result and check created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr') @@ -558,11 +558,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.driverName = 'GPKG' options.layerName = 'test' filename = '/vsimem/out.gpkg' - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, filename, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) ds = ogr.Open(filename, update=1) lyr = ds.GetLayerByName('test') @@ -597,11 +597,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.layerName = 'test' options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer filename = '/vsimem/out.gpkg' - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, filename, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) ds = ogr.Open(filename) lyr = ds.GetLayerByName('test') @@ -626,11 +626,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.driverName = 'GPKG' options.layerName = 'test' filename = '/vsimem/out.gpkg' - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, filename, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) ds = ogr.Open(filename) lyr = ds.GetLayerByName('test') @@ -658,11 +658,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.layerName = 'test' options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields filename = '/vsimem/out.gpkg' - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, filename, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) ds = ogr.Open(filename) lyr = ds.GetLayerByName('test') @@ -691,11 +691,11 @@ class TestQgsVectorFileWriter(unittest.TestCase): options.layerName = 'test' options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerAddFields filename = '/vsimem/out.gpkg' - write_result = QgsVectorFileWriter.writeAsVectorFormat( + write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( ml, filename, options) - self.assertEqual(write_result, QgsVectorFileWriter.NoError) + self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message) ds = ogr.Open(filename) lyr = ds.GetLayerByName('test') diff --git a/tests/src/python/utilities.py b/tests/src/python/utilities.py index a8360c5859f..69ca56f7fe0 100644 --- a/tests/src/python/utilities.py +++ b/tests/src/python/utilities.py @@ -105,25 +105,23 @@ def writeShape(theMemoryLayer, theFileName): myFileName = os.path.join(str(QDir.tempPath()), theFileName) print(myFileName) # Explicitly giving all options, not really needed but nice for clarity - myErrorMessage = '' myOptions = [] myLayerOptions = [] mySelectedOnlyFlag = False mySkipAttributesFlag = False myGeoCrs = QgsCoordinateReferenceSystem() myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - myResult = QgsVectorFileWriter.writeAsVectorFormat( + myResult, myErrorMessage = QgsVectorFileWriter.writeAsVectorFormat( theMemoryLayer, myFileName, 'utf-8', myGeoCrs, 'ESRI Shapefile', mySelectedOnlyFlag, - myErrorMessage, myOptions, myLayerOptions, mySkipAttributesFlag) - assert myResult == QgsVectorFileWriter.NoError + self.assertEqual(myResult, QgsVectorFileWriter.NoError, myErrorMessage) def doubleNear(a, b, tol=0.0000000001): From d1d26e96b0c672769af03d81b5f3fec416978778 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 19 Sep 2017 12:24:01 +0200 Subject: [PATCH 4/4] Fix test --- tests/src/python/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/python/utilities.py b/tests/src/python/utilities.py index 69ca56f7fe0..b437df87bd2 100644 --- a/tests/src/python/utilities.py +++ b/tests/src/python/utilities.py @@ -121,7 +121,7 @@ def writeShape(theMemoryLayer, theFileName): myOptions, myLayerOptions, mySkipAttributesFlag) - self.assertEqual(myResult, QgsVectorFileWriter.NoError, myErrorMessage) + assert myResult == QgsVectorFileWriter.NoError, 'Writing shape failed, Error {} ({})'.format(myResult, myErrorMessage) def doubleNear(a, b, tol=0.0000000001):