Skip to content
Permalink
Browse files

Merge pull request #5216 from m-kuhn/vectorFileWriter

Make error message accessible for python in vector file writer
  • Loading branch information
m-kuhn committed Sep 19, 2017
2 parents b089248 + d1d26e9 commit 8d34023d6e51dc613fb94d2645b745689f99d912
@@ -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}
@@ -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
@@ -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,
@@ -115,13 +115,13 @@ def testDateTimeWriteShapefile(self):
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 @@ def testWriterWithExtent(self):
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 @@ def testWriterWithExtentAndReprojection(self):
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 @@ def testDateTimeWriteTabfile(self):
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 @@ def testWriteShapefileWithZ(self):
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 @@ def testWriteShapefileWithZ(self):
'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 @@ def testWriteShapefileWithMultiConversion(self):
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 @@ def testWriteShapefileWithAttributeSubsets(self):
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 @@ def testWriteShapefileWithAttributeSubsets(self):

# 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 @@ def testWriteShapefileWithAttributeSubsets(self):

# 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,15 +463,15 @@ def testValueConverter(self):

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',
QgsCoordinateReferenceSystem(),
'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 @@ def testInteger64WriteTabfile(self):
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 @@ def testOverwriteLayer(self):
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 @@ def testOverwriteLayer(self):
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 @@ def testOverwriteLayer(self):
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 @@ def testOverwriteLayer(self):
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 @@ def testOverwriteLayer(self):
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')
@@ -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
assert myResult == QgsVectorFileWriter.NoError, 'Writing shape failed, Error {} ({})'.format(myResult, myErrorMessage)


def doubleNear(a, b, tol=0.0000000001):

0 comments on commit 8d34023

Please sign in to comment.
You can’t perform that action at this time.