Skip to content
Permalink
Browse files

Added providerUriParams to QgsVectorFileWriter::PreparedWriterDetails

Also added a test for the uri parsing
  • Loading branch information
elpaso committed Aug 22, 2018
1 parent abc96ce commit f4daa8cb60244c6d8f808371d48bc17db0acf4c6
Showing with 44 additions and 11 deletions.
  1. +8 −11 src/core/qgsvectorfilewriter.cpp
  2. +2 −0 src/core/qgsvectorfilewriter.h
  3. +34 −0 tests/src/core/testqgsvectorfilewriter.cpp
@@ -33,6 +33,7 @@
#include "qgsexception.h"
#include "qgssettings.h"
#include "qgsgeometryengine.h"
#include "qgsproviderregistry.h"

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

if ( details.storageType == QLatin1String( "ESRI Shapefile" ) )
{
@@ -2554,26 +2556,21 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
if ( details.providerType == QLatin1String( "ogr" ) && !details.dataSourceUri.isEmpty() )
{
QStringList theURIParts = details.dataSourceUri.split( '|' );
QString srcFileName = theURIParts[0];
QgsStringMap srcUriParams;
QString srcFileName;
if ( theURIParts.length() > 0 )
{
for ( int i = 1; i < theURIParts.length(); ++i )
{
QStringList parts( theURIParts[i].split( '=' ) );
if ( parts.length() == 2 )
srcUriParams[parts[0]] = parts[1];
}
srcFileName = theURIParts[0];
}

if ( QFile::exists( srcFileName ) && QFileInfo( fileName ).canonicalFilePath() == QFileInfo( srcFileName ).canonicalFilePath() )
{
// Check the layer name too if it's a GPKG/SpatiaLite/SQLite OGR driver
// Check the layer name too if it's a GPKG/SpatiaLite/SQLite OGR driver (pay attention: camel case in layerName)
QgsDataSourceUri uri( details.dataSourceUri );
if ( !( ( options.driverName == QLatin1String( "GPKG" ) ||
options.driverName == QLatin1String( "SpatiaLite" ) ||
options.driverName == QLatin1String( "SQLite" ) ) &&
options.layerName != srcUriParams["layername"] ) )
options.layerName != details.providerUriParams.value( QLatin1String( "layerName" ) ) ) )
{
if ( errorMessage )
*errorMessage = QObject::tr( "Cannot overwrite a OGR layer in place" );
@@ -2596,7 +2593,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
if ( options.feedback )
{
//dedicate first 5% of progress bar to this scan
int newProgress = ( 5.0 * scanned ) / total;
int newProgress = static_cast<int>( ( 5.0 * scanned ) / total );
if ( newProgress != lastProgressReport )
{
lastProgressReport = newProgress;
@@ -2694,7 +2691,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
if ( options.feedback )
{
//avoid spamming progress reports
int newProgress = initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total;
int newProgress = static_cast<int>( initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total );
if ( newProgress < 100 && newProgress != lastProgressReport )
{
lastProgressReport = newProgress;
@@ -760,6 +760,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
QgsFeatureIterator sourceFeatureIterator;
QgsGeometry filterRectGeometry;
std::unique_ptr< QgsGeometryEngine > filterRectEngine;
QVariantMap providerUriParams;
};

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

friend class QgsVectorFileWriterTask;
friend class TestQgsVectorFileWriter;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::EditionCapabilities )
@@ -80,6 +80,8 @@ class TestQgsVectorFileWriter: public QObject
void projectedPlygonGridTest();
//! This is a regression test ticket 1141 (broken Polish characters support since r8592) https://issues.qgis.org/issues/1141
void regression1141();
//! Test prepareWriteAsVectorFormat
void prepareWriteAsVectorFormat();

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

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

void TestQgsVectorFileWriter::prepareWriteAsVectorFormat()
{
QgsVectorFileWriter::PreparedWriterDetails details;
QgsVectorFileWriter::SaveVectorOptions options;
QgsVectorLayer ml( "Point?field=firstfield:int&field=secondfield:int", "test", "memory" );
QgsFeature ft( ml.fields( ) );
ft.setAttribute( 0, 4 );
ft.setAttribute( 1, -10 );
ml.dataProvider()->addFeature( ft );
QVERIFY( ml.isValid() );
QTemporaryFile tmpFile( QDir::tempPath() + "/test_qgsvectorfilewriter_XXXXXX.gpkg" );
tmpFile.open();
QString fileName( tmpFile.fileName( ) );
options.driverName = "GPKG";
options.layerName = "test";
QString errorMessage;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormat(
&ml,
fileName,
options,
&errorMessage ) );

QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
QCOMPARE( errorMessage, fileName );
QgsVectorLayer vl( QStringLiteral( "%1|layername=test" ).arg( fileName ), "src_test", "ogr" );
QVERIFY( vl.isValid() );
QgsVectorFileWriter::prepareWriteAsVectorFormat( &vl, options, details );
QCOMPARE( details.providerUriParams.value( "layerName" ), QStringLiteral( "test" ) );
QCOMPARE( details.providerUriParams.value( "path" ), fileName );
}

QGSTEST_MAIN( TestQgsVectorFileWriter )
#include "testqgsvectorfilewriter.moc"

0 comments on commit f4daa8c

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