33
33
#include " qgsexception.h"
34
34
#include " qgssettings.h"
35
35
#include " qgsgeometryengine.h"
36
+ #include " qgsproviderregistry.h"
36
37
37
38
#include < QFile>
38
39
#include < QFileInfo>
@@ -2422,6 +2423,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::prepareWriteAsVectorFormat
2422
2423
details.dataSourceUri = layer->dataProvider ()->dataSourceUri ();
2423
2424
details.storageType = layer->storageType ();
2424
2425
details.selectedFeatureIds = layer->selectedFeatureIds ();
2426
+ details.providerUriParams = QgsProviderRegistry::instance ()->decodeUri ( layer->providerType (), layer->dataProvider ()->dataSourceUri () );
2425
2427
2426
2428
if ( details.storageType == QLatin1String ( " ESRI Shapefile" ) )
2427
2429
{
@@ -2550,16 +2552,23 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
2550
2552
int lastProgressReport = 0 ;
2551
2553
long total = details.featureCount ;
2552
2554
2555
+ // Special rules for OGR layers
2553
2556
if ( details.providerType == QLatin1String ( " ogr" ) && !details.dataSourceUri .isEmpty () )
2554
2557
{
2555
- QStringList theURIParts = details.dataSourceUri .split ( ' |' );
2556
- QString srcFileName = theURIParts[0 ];
2557
-
2558
+ QString srcFileName ( details.providerUriParams .value ( QLatin1String ( " path" ) ).toString () );
2558
2559
if ( QFile::exists ( srcFileName ) && QFileInfo ( fileName ).canonicalFilePath () == QFileInfo ( srcFileName ).canonicalFilePath () )
2559
2560
{
2560
- if ( errorMessage )
2561
- *errorMessage = QObject::tr ( " Cannot overwrite a OGR layer in place" );
2562
- return ErrCreateDataSource;
2561
+ // Check the layer name too if it's a GPKG/SpatiaLite/SQLite OGR driver (pay attention: camel case in layerName)
2562
+ QgsDataSourceUri uri ( details.dataSourceUri );
2563
+ if ( !( ( options.driverName == QLatin1String ( " GPKG" ) ||
2564
+ options.driverName == QLatin1String ( " SpatiaLite" ) ||
2565
+ options.driverName == QLatin1String ( " SQLite" ) ) &&
2566
+ options.layerName != details.providerUriParams .value ( QLatin1String ( " layerName" ) ) ) )
2567
+ {
2568
+ if ( errorMessage )
2569
+ *errorMessage = QObject::tr ( " Cannot overwrite a OGR layer in place" );
2570
+ return ErrCreateDataSource;
2571
+ }
2563
2572
}
2564
2573
2565
2574
// Shapefiles might contain multi types although wkbType() only reports singles
@@ -2577,7 +2586,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
2577
2586
if ( options.feedback )
2578
2587
{
2579
2588
// dedicate first 5% of progress bar to this scan
2580
- int newProgress = ( 5.0 * scanned ) / total;
2589
+ int newProgress = static_cast < int >( ( 5.0 * scanned ) / total ) ;
2581
2590
if ( newProgress != lastProgressReport )
2582
2591
{
2583
2592
lastProgressReport = newProgress;
@@ -2675,7 +2684,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
2675
2684
if ( options.feedback )
2676
2685
{
2677
2686
// avoid spamming progress reports
2678
- int newProgress = initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total;
2687
+ int newProgress = static_cast < int >( initialProgress + ( ( 100.0 - initialProgress ) * saved ) / total ) ;
2679
2688
if ( newProgress < 100 && newProgress != lastProgressReport )
2680
2689
{
2681
2690
lastProgressReport = newProgress;
0 commit comments