Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsVectorFileWriter::writeAsVectorFormatV3
Unfortunately QgsVectorFileWriter::writeAsVectorFormatV2 is missing
SIP_OUT arguments necessary to get the desired output arguments,
and it's not possible to add these now without breaking existing PyQGIS
code.

Solution = v3!
  • Loading branch information
nyalldawson committed Mar 29, 2021
1 parent bb7b009 commit 62ad6e3
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 19 deletions.
26 changes: 25 additions & 1 deletion python/core/auto_generated/qgsvectorfilewriter.sip.in
Expand Up @@ -475,7 +475,31 @@ Writes a layer out to a vector file.
:return: - Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
- errorMessage: will be set to the error message text, if an error occurs while writing the layer

.. versionadded:: 3.10.3
.. deprecated:: QGIS 3.20
use writeAsVectorFormatV3 instead
%End

static QgsVectorFileWriter::WriterError writeAsVectorFormatV3( QgsVectorLayer *layer,
const QString &fileName,
const QgsCoordinateTransformContext &transformContext,
const QgsVectorFileWriter::SaveVectorOptions &options,
QString *errorMessage /Out/ = 0,
QString *newFilename /Out/ = 0,
QString *newLayer /Out/ = 0 );
%Docstring
Writes a layer out to a vector file.

:param layer: source layer to write
:param fileName: file name to write to
:param transformContext: coordinate transform context
:param options: save options
:param newFilename: potentially modified file name (output parameter)
:param errorMessage: will be set to the error message text, if an error occurs while writing the layer

:return: - Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
- newLayer: potentially modified layer name (output parameter)

.. versionadded:: 3.20
%End

struct FilterFormatDetails
Expand Down
Expand Up @@ -261,7 +261,7 @@ bool QgsExportToSpreadsheetAlgorithm::exportVectorLayer( QgsVectorLayer *layer,
QString error;
QString newFilename;
QString newLayer;
if ( QgsVectorFileWriter::writeAsVectorFormatV2( layer, path, context.transformContext(), options, &newFilename, &newLayer, &error ) != QgsVectorFileWriter::NoError )
if ( QgsVectorFileWriter::writeAsVectorFormatV3( layer, path, context.transformContext(), options, &error, &newFilename, &newLayer ) != QgsVectorFileWriter::NoError )
{
feedback->reportError( QObject::tr( "Exporting layer failed: %1" ).arg( error ) );
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmpackage.cpp
Expand Up @@ -240,7 +240,7 @@ bool QgsPackageAlgorithm::packageVectorLayer( QgsVectorLayer *layer, const QStri
QString error;
QString newFilename;
QString newLayer;
if ( QgsVectorFileWriter::writeAsVectorFormatV2( layer, path, context.transformContext(), options, &newFilename, &newLayer, &error ) != QgsVectorFileWriter::NoError )
if ( QgsVectorFileWriter::writeAsVectorFormatV3( layer, path, context.transformContext(), options, &error, &newFilename, &newLayer ) != QgsVectorFileWriter::NoError )
{
feedback->reportError( QObject::tr( "Packaging layer failed: %1" ).arg( error ) );
return false;
Expand Down
14 changes: 12 additions & 2 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2755,7 +2755,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
options.includeZ = includeZ;
options.attributes = attributes;
options.fieldValueConverter = fieldValueConverter;
return writeAsVectorFormatV2( layer, fileName, layer->transformContext(), options, newFilename, newLayer, errorMessage );
return writeAsVectorFormatV3( layer, fileName, layer->transformContext(), options, errorMessage, newFilename, newLayer );
}

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
Expand Down Expand Up @@ -2796,7 +2796,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
options.includeZ = includeZ;
options.attributes = attributes;
options.fieldValueConverter = fieldValueConverter;
return writeAsVectorFormatV2( layer, fileName, layer->transformContext(), options, newFilename, newLayer, errorMessage );
return writeAsVectorFormatV3( layer, fileName, layer->transformContext(), options, errorMessage, newFilename, newLayer );
}


Expand Down Expand Up @@ -3179,6 +3179,16 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2( Qgs
QString *newFilename,
QString *newLayer,
QString *errorMessage )
{
QgsVectorFileWriter::PreparedWriterDetails details;
WriterError err = prepareWriteAsVectorFormat( layer, options, details );
if ( err != NoError )
return err;

return writeAsVectorFormatV2( details, fileName, transformContext, options, errorMessage, newFilename, newLayer );
}

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV3( QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage, QString *newFilename, QString *newLayer )
{
QgsVectorFileWriter::PreparedWriterDetails details;
WriterError err = prepareWriteAsVectorFormat( layer, options, details );
Expand Down
26 changes: 23 additions & 3 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -671,15 +671,35 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
* \param newLayer potentially modified layer name (output parameter)
* \param errorMessage will be set to the error message text, if an error occurs while writing the layer
* \returns Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
* \since QGIS 3.10.3
* \deprecated since QGIS 3.20, use writeAsVectorFormatV3 instead
*/
static QgsVectorFileWriter::WriterError writeAsVectorFormatV2( QgsVectorLayer *layer,
Q_DECL_DEPRECATED static QgsVectorFileWriter::WriterError writeAsVectorFormatV2( QgsVectorLayer *layer,
const QString &fileName,
const QgsCoordinateTransformContext &transformContext,
const QgsVectorFileWriter::SaveVectorOptions &options,
QString *newFilename = nullptr,
QString *newLayer = nullptr,
QString *errorMessage SIP_OUT = nullptr );
QString *errorMessage SIP_OUT = nullptr ) SIP_DEPRECATED;

/**
* Writes a layer out to a vector file.
* \param layer source layer to write
* \param fileName file name to write to
* \param transformContext coordinate transform context
* \param options save options
* \param newFilename potentially modified file name (output parameter)
* \param newLayer potentially modified layer name (output parameter)
* \param errorMessage will be set to the error message text, if an error occurs while writing the layer
* \returns Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
* \since QGIS 3.20
*/
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3( QgsVectorLayer *layer,
const QString &fileName,
const QgsCoordinateTransformContext &transformContext,
const QgsVectorFileWriter::SaveVectorOptions &options,
QString *errorMessage SIP_OUT = nullptr,
QString *newFilename SIP_OUT = nullptr,
QString *newLayer SIP_OUT = nullptr );

/**
* Details of available filters and formats.
Expand Down
Expand Up @@ -342,7 +342,7 @@ void QgsGeometryCheckerSetupTab::runChecks()
saveOptions.fileEncoding = layer->dataProvider()->encoding();
saveOptions.driverName = outputDriverName;
saveOptions.onlySelectedFeatures = selectedOnly;
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormatV2( layer, outputPath, layer->transformContext(), saveOptions, nullptr, nullptr, &errMsg );
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormatV3( layer, outputPath, layer->transformContext(), saveOptions, &errMsg, nullptr, nullptr );
if ( err != QgsVectorFileWriter::NoError )
{
createErrors.append( errMsg );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/app/testqgsattributetable.cpp
Expand Up @@ -378,7 +378,7 @@ void TestQgsAttributeTable::testRegression15974()
QgsVectorFileWriter::SaveVectorOptions saveOptions;
saveOptions.fileEncoding = QStringLiteral( "system" );
saveOptions.driverName = QStringLiteral( "ESRI Shapefile" );
QgsVectorFileWriter::writeAsVectorFormatV2( tempLayer.get(), path, tempLayer->transformContext(), saveOptions );
QgsVectorFileWriter::writeAsVectorFormatV3( tempLayer.get(), path, tempLayer->transformContext(), saveOptions );
std::unique_ptr< QgsVectorLayer> shpLayer( new QgsVectorLayer( path, QStringLiteral( "test" ), QStringLiteral( "ogr" ) ) );
QgsFeature f1( shpLayer->dataProvider()->fields(), 1 );
QgsGeometry geom;
Expand Down
12 changes: 6 additions & 6 deletions tests/src/core/testqgsvectorfilewriter.cpp
Expand Up @@ -487,11 +487,11 @@ void TestQgsVectorFileWriter::prepareWriteAsVectorFormat()
options.driverName = "GPKG";
options.layerName = "test";
QString newFilename;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV2(
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV3(
&ml,
fileName,
ml.transformContext(),
options,
options, nullptr,
&newFilename ) );

QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
Expand Down Expand Up @@ -519,11 +519,11 @@ void TestQgsVectorFileWriter::testTextFieldLength()
options.driverName = "GPKG";
options.layerName = "test";
QString newFilename;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV2(
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV3(
&vl,
fileName,
vl.transformContext(),
options,
options, nullptr,
&newFilename ) );
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
QCOMPARE( newFilename, fileName );
Expand Down Expand Up @@ -572,11 +572,11 @@ void TestQgsVectorFileWriter::_testExportToGpx( const QString &geomTypeName,
options.layerName = inputLayerName;
options.layerOptions = layerOptions;
QString outLayerName;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV2(
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV3(
&vl,
fileName,
vl.transformContext(),
options,
options, nullptr,
nullptr, // newFilename
&outLayerName ) );
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
Expand Down
7 changes: 4 additions & 3 deletions tests/src/gui/testqgsrasterlayersaveasdialog.cpp
Expand Up @@ -132,14 +132,15 @@ QString TestQgsRasterLayerSaveAsDialog::prepareDb()
options.driverName = QStringLiteral( "GPKG" );
options.layerName = QStringLiteral( "test_vector_layer" );
QString errorMessage;
QgsVectorFileWriter::writeAsVectorFormatV2(
QgsVectorFileWriter::writeAsVectorFormatV3(
&vl,
fileName,
vl.transformContext(),
options,
&errorMessage,
nullptr,
nullptr,
&errorMessage );
nullptr
);
QgsVectorLayer vl2( QStringLiteral( "%1|layername=test_vector_layer" ).arg( fileName ), "test_vector_layer", "ogr" );
Q_ASSERT( vl2.isValid() );
return tmpFile.fileName( );
Expand Down

0 comments on commit 62ad6e3

Please sign in to comment.