Skip to content

Commit f4daa8c

Browse files
committed
Added providerUriParams to QgsVectorFileWriter::PreparedWriterDetails
Also added a test for the uri parsing
1 parent abc96ce commit f4daa8c

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/core/qgsvectorfilewriter.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgsexception.h"
3434
#include "qgssettings.h"
3535
#include "qgsgeometryengine.h"
36+
#include "qgsproviderregistry.h"
3637

3738
#include <QFile>
3839
#include <QFileInfo>
@@ -2422,6 +2423,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::prepareWriteAsVectorFormat
24222423
details.dataSourceUri = layer->dataProvider()->dataSourceUri();
24232424
details.storageType = layer->storageType();
24242425
details.selectedFeatureIds = layer->selectedFeatureIds();
2426+
details.providerUriParams = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->dataProvider()->dataSourceUri() );
24252427

24262428
if ( details.storageType == QLatin1String( "ESRI Shapefile" ) )
24272429
{
@@ -2554,26 +2556,21 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
25542556
if ( details.providerType == QLatin1String( "ogr" ) && !details.dataSourceUri.isEmpty() )
25552557
{
25562558
QStringList theURIParts = details.dataSourceUri.split( '|' );
2557-
QString srcFileName = theURIParts[0];
25582559
QgsStringMap srcUriParams;
2560+
QString srcFileName;
25592561
if ( theURIParts.length() > 0 )
25602562
{
2561-
for ( int i = 1; i < theURIParts.length(); ++i )
2562-
{
2563-
QStringList parts( theURIParts[i].split( '=' ) );
2564-
if ( parts.length() == 2 )
2565-
srcUriParams[parts[0]] = parts[1];
2566-
}
2563+
srcFileName = theURIParts[0];
25672564
}
25682565

25692566
if ( QFile::exists( srcFileName ) && QFileInfo( fileName ).canonicalFilePath() == QFileInfo( srcFileName ).canonicalFilePath() )
25702567
{
2571-
// Check the layer name too if it's a GPKG/SpatiaLite/SQLite OGR driver
2568+
// Check the layer name too if it's a GPKG/SpatiaLite/SQLite OGR driver (pay attention: camel case in layerName)
25722569
QgsDataSourceUri uri( details.dataSourceUri );
25732570
if ( !( ( options.driverName == QLatin1String( "GPKG" ) ||
25742571
options.driverName == QLatin1String( "SpatiaLite" ) ||
25752572
options.driverName == QLatin1String( "SQLite" ) ) &&
2576-
options.layerName != srcUriParams["layername"] ) )
2573+
options.layerName != details.providerUriParams.value( QLatin1String( "layerName" ) ) ) )
25772574
{
25782575
if ( errorMessage )
25792576
*errorMessage = QObject::tr( "Cannot overwrite a OGR layer in place" );
@@ -2596,7 +2593,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
25962593
if ( options.feedback )
25972594
{
25982595
//dedicate first 5% of progress bar to this scan
2599-
int newProgress = ( 5.0 * scanned ) / total;
2596+
int newProgress = static_cast<int>( ( 5.0 * scanned ) / total );
26002597
if ( newProgress != lastProgressReport )
26012598
{
26022599
lastProgressReport = newProgress;
@@ -2694,7 +2691,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
26942691
if ( options.feedback )
26952692
{
26962693
//avoid spamming progress reports
2697-
int newProgress = initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total;
2694+
int newProgress = static_cast<int>( initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total );
26982695
if ( newProgress < 100 && newProgress != lastProgressReport )
26992696
{
27002697
lastProgressReport = newProgress;

src/core/qgsvectorfilewriter.h

+2
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
760760
QgsFeatureIterator sourceFeatureIterator;
761761
QgsGeometry filterRectGeometry;
762762
std::unique_ptr< QgsGeometryEngine > filterRectEngine;
763+
QVariantMap providerUriParams;
763764
};
764765

765766
/**
@@ -814,6 +815,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
814815
static QStringList concatenateOptions( const QMap<QString, Option *> &options );
815816

816817
friend class QgsVectorFileWriterTask;
818+
friend class TestQgsVectorFileWriter;
817819
};
818820

819821
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::EditionCapabilities )

tests/src/core/testqgsvectorfilewriter.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class TestQgsVectorFileWriter: public QObject
8080
void projectedPlygonGridTest();
8181
//! This is a regression test ticket 1141 (broken Polish characters support since r8592) https://issues.qgis.org/issues/1141
8282
void regression1141();
83+
//! Test prepareWriteAsVectorFormat
84+
void prepareWriteAsVectorFormat();
8385

8486
private:
8587
// a little util fn used by all tests
@@ -106,6 +108,7 @@ void TestQgsVectorFileWriter::initTestCase()
106108
// init QGIS's paths - true means that all path will be inited from prefix
107109
QgsApplication::init();
108110
QgsApplication::showSettings();
111+
QgsApplication::initQgis();
109112
//create some objects that will be used in all tests...
110113

111114
mEncoding = QStringLiteral( "UTF-8" );
@@ -457,5 +460,36 @@ void TestQgsVectorFileWriter::regression1141()
457460
QVERIFY( QgsVectorFileWriter::deleteShapeFile( fileName ) );
458461
}
459462

463+
void TestQgsVectorFileWriter::prepareWriteAsVectorFormat()
464+
{
465+
QgsVectorFileWriter::PreparedWriterDetails details;
466+
QgsVectorFileWriter::SaveVectorOptions options;
467+
QgsVectorLayer ml( "Point?field=firstfield:int&field=secondfield:int", "test", "memory" );
468+
QgsFeature ft( ml.fields( ) );
469+
ft.setAttribute( 0, 4 );
470+
ft.setAttribute( 1, -10 );
471+
ml.dataProvider()->addFeature( ft );
472+
QVERIFY( ml.isValid() );
473+
QTemporaryFile tmpFile( QDir::tempPath() + "/test_qgsvectorfilewriter_XXXXXX.gpkg" );
474+
tmpFile.open();
475+
QString fileName( tmpFile.fileName( ) );
476+
options.driverName = "GPKG";
477+
options.layerName = "test";
478+
QString errorMessage;
479+
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormat(
480+
&ml,
481+
fileName,
482+
options,
483+
&errorMessage ) );
484+
485+
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
486+
QCOMPARE( errorMessage, fileName );
487+
QgsVectorLayer vl( QStringLiteral( "%1|layername=test" ).arg( fileName ), "src_test", "ogr" );
488+
QVERIFY( vl.isValid() );
489+
QgsVectorFileWriter::prepareWriteAsVectorFormat( &vl, options, details );
490+
QCOMPARE( details.providerUriParams.value( "layerName" ), QStringLiteral( "test" ) );
491+
QCOMPARE( details.providerUriParams.value( "path" ), fileName );
492+
}
493+
460494
QGSTEST_MAIN( TestQgsVectorFileWriter )
461495
#include "testqgsvectorfilewriter.moc"

0 commit comments

Comments
 (0)