Skip to content
Permalink
Browse files

[vector writer] Return actual layer name used

This is needed as some OGR drivers will modify the provided layer name
(e.g. the KML driver replaces dashes with underscores).
  • Loading branch information
nirvn committed Aug 23, 2018
1 parent 64e8174 commit 6196fd3753697b321f2cbc293fa9db3529753c0f
@@ -229,6 +229,7 @@ Creates a clone of the FieldValueConverter.
AppendToLayerAddFields
};


static QgsVectorFileWriter::WriterError writeAsVectorFormat( QgsVectorLayer *layer,
const QString &fileName,
const QString &fileEncoding,
@@ -250,7 +251,7 @@ Creates a clone of the FieldValueConverter.
QgsVectorFileWriter::FieldValueConverter *fieldValueConverter = 0
);
%Docstring
Write contents of vector layer to an (OGR supported) vector formt
Write contents of vector layer to an (OGR supported) vector format

:param layer: layer to write
:param fileName: file name to write to
@@ -274,6 +275,7 @@ Write contents of vector layer to an (OGR supported) vector formt
:param fieldValueConverter: field value converter (added in QGIS 2.16)
%End


static QgsVectorFileWriter::WriterError writeAsVectorFormat( QgsVectorLayer *layer,
const QString &fileName,
const QString &fileEncoding,
@@ -322,7 +324,6 @@ Writes a layer out to a vector file.
.. versionadded:: 2.2
%End


class SaveVectorOptions
{
%Docstring
@@ -379,11 +380,13 @@ Constructor
QgsFeedback *feedback;
};


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

@@ -407,9 +410,6 @@ Writes a layer out to a vector file.
QString *newFilename = 0,
QgsVectorFileWriter::SymbologyExport symbologyExport = QgsVectorFileWriter::NoSymbology
);
%Docstring
Create a new vector file writer
%End



@@ -45,8 +45,13 @@ and save ``options``.
%Docstring
Emitted when writing the layer is successfully completed. The ``newFilename``
parameter indicates the file path for the written file.

.. note::

this signal is deprecated in favor of completed().
%End


void errorOccurred( int error, const QString &errorMessage );
%Docstring
Emitted when an error occurs which prevented the file being written (or if
@@ -7446,13 +7446,12 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbologyOpt
options.fieldValueConverter = converterPtr;

bool addToCanvas = dialog->addToCanvas();
QString layerName = dialog->layername();
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask( vlayer, vectorFilename, options );

// when writer is successful:
connect( writerTask, &QgsVectorFileWriterTask::writeComplete, this, [onSuccess, addToCanvas, layerName, encoding, vectorFilename]( const QString & newFilename )
connect( writerTask, &QgsVectorFileWriterTask::completed, this, [onSuccess, addToCanvas, encoding, vectorFilename]( const QString & newFilename, const QString & newLayer )
{
onSuccess( newFilename, addToCanvas, layerName, encoding, vectorFilename );
onSuccess( newFilename, addToCanvas, newLayer, encoding, vectorFilename );
} );

// when an error occurs:
@@ -101,7 +101,8 @@ QgsVectorFileWriter::QgsVectorFileWriter(
const QStringList &datasourceOptions,
const QStringList &layerOptions,
QString *newFilename,
SymbologyExport symbologyExport
SymbologyExport symbologyExport,
QString *newLayer
)
: mError( NoError )
, mWkbType( geometryType )
@@ -110,7 +111,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
{
init( vectorFileName, fileEncoding, fields, geometryType,
srs, driverName, datasourceOptions, layerOptions, newFilename, nullptr,
QString(), CreateOrOverwriteFile );
QString(), CreateOrOverwriteFile, newLayer );
}

QgsVectorFileWriter::QgsVectorFileWriter( const QString &vectorFileName,
@@ -125,15 +126,16 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString &vectorFileName,
QgsVectorFileWriter::SymbologyExport symbologyExport,
FieldValueConverter *fieldValueConverter,
const QString &layerName,
ActionOnExistingFile action )
ActionOnExistingFile action,
QString *newLayer )
: mError( NoError )
, mWkbType( geometryType )
, mSymbologyExport( symbologyExport )
, mSymbologyScale( 1.0 )
{
init( vectorFileName, fileEncoding, fields, geometryType, srs, driverName,
datasourceOptions, layerOptions, newFilename, fieldValueConverter,
layerName, action );
layerName, action, newLayer );
}

bool QgsVectorFileWriter::supportsFeatureStyles( const QString &driverName )
@@ -164,7 +166,8 @@ void QgsVectorFileWriter::init( QString vectorFileName,
QString *newFilename,
FieldValueConverter *fieldValueConverter,
const QString &layerNameIn,
ActionOnExistingFile action )
ActionOnExistingFile action,
QString *newLayer )
{
mRenderContext.setRendererScale( mSymbologyScale );

@@ -419,6 +422,8 @@ void QgsVectorFileWriter::init( QString vectorFileName,
else if ( action == CreateOrOverwriteFile || action == CreateOrOverwriteLayer )
{
mLayer = OGR_DS_CreateLayer( mDS.get(), layerName.toUtf8().constData(), mOgrRef, wkbType, options );
if ( newLayer )
*newLayer = OGR_L_GetName( mLayer );
}
else
{
@@ -2338,7 +2343,8 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
bool forceMulti,
bool includeZ,
const QgsAttributeList &attributes,
FieldValueConverter *fieldValueConverter )
FieldValueConverter *fieldValueConverter,
QString *newLayer )
{
QgsCoordinateTransform ct;
if ( destCRS.isValid() && layer )
@@ -2352,7 +2358,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
errorMessage, datasourceOptions, layerOptions, skipAttributeCreation,
newFilename, symbologyExport, symbologyScale, filterExtent,
overrideGeometryType, forceMulti, includeZ, attributes,
fieldValueConverter );
fieldValueConverter, newLayer );
return error;
}

@@ -2374,7 +2380,8 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
bool forceMulti,
bool includeZ,
const QgsAttributeList &attributes,
FieldValueConverter *fieldValueConverter )
FieldValueConverter *fieldValueConverter,
QString *newLayer )
{
SaveVectorOptions options;
options.fileEncoding = fileEncoding;
@@ -2393,7 +2400,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
options.includeZ = includeZ;
options.attributes = attributes;
options.fieldValueConverter = fieldValueConverter;
return writeAsVectorFormat( layer, fileName, options, newFilename, errorMessage );
return writeAsVectorFormat( layer, fileName, options, newFilename, errorMessage, newLayer );
}


@@ -2544,7 +2551,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::prepareWriteAsVectorFormat
return NoError;
}

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( PreparedWriterDetails &details, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename, QString *errorMessage )
QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( PreparedWriterDetails &details, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename, QString *errorMessage, QString *newLayer )
{

QgsWkbTypes::Type destWkbType = details.destWkbType;
@@ -2614,7 +2621,8 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
options.symbologyExport,
options.fieldValueConverter,
options.layerName,
options.actionOnExistingFile );
options.actionOnExistingFile,
newLayer );
writer->setSymbologyScale( options.symbologyScale );

if ( newFilename )
@@ -2765,14 +2773,15 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
const QString &fileName,
const SaveVectorOptions &options,
QString *newFilename,
QString *errorMessage )
QString *errorMessage,
QString *newLayer )
{
QgsVectorFileWriter::PreparedWriterDetails details;
WriterError err = prepareWriteAsVectorFormat( layer, options, details );
if ( err != NoError )
return err;

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


0 comments on commit 6196fd3

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