Skip to content

Commit

Permalink
Better feedback and responsiveness when saving large shapefile layers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 16, 2017
1 parent 5f5c1dd commit 7d2d6e8
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/core/qgsvectorfilewriter.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,9 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
}
}

int lastProgressReport = 0;
long total = options.onlySelectedFeatures ? layer->selectedFeatureCount() : layer->featureCount();

if ( layer->providerType() == QLatin1String( "ogr" ) && layer->dataProvider() )
{
QStringList theURIParts = layer->dataProvider()->dataSourceUri().split( '|' );
Expand All @@ -2307,14 +2310,30 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
req.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator fit = layer->getFeatures( req );
QgsFeature fet;

long scanned = 0;
while ( fit.nextFeature( fet ) )
{
if ( options.feedback && options.feedback->isCanceled() )
{
return Canceled;
}
if ( options.feedback )
{
//dedicate first 5% of progress bar to this scan
int newProgress = ( 5.0 * scanned ) / total;
if ( newProgress != lastProgressReport )
{
lastProgressReport = newProgress;
options.feedback->setProgress( lastProgressReport );
}
}

if ( fet.hasGeometry() && QgsWkbTypes::isMultiType( fet.geometry().geometry()->wkbType() ) )
{
destWkbType = QgsWkbTypes::multiType( destWkbType );
break;
}
scanned++;
}
}
}
Expand Down Expand Up @@ -2426,8 +2445,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,

// write all features
long saved = 0;
long total = options.onlySelectedFeatures ? layer->selectedFeatureCount() : layer->featureCount();
int lastProgressReport = 0;
int initialProgress = lastProgressReport;
while ( fit.nextFeature( fet ) )
{
if ( options.feedback && options.feedback->isCanceled() )
Expand All @@ -2440,7 +2458,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
if ( options.feedback )
{
//avoid spamming progress reports
int newProgress = ( 100.0 * saved ) / total;
int newProgress = initialProgress + (( 100.0 - initialProgress ) * saved ) / total;
if ( newProgress < 100 && newProgress != lastProgressReport )
{
lastProgressReport = newProgress;
Expand Down

0 comments on commit 7d2d6e8

Please sign in to comment.