Skip to content

Commit

Permalink
use a buffer when importing features
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Sep 14, 2011
1 parent 6eb406b commit 37b2c04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/core/qgsvectorlayerimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include <cstdlib> // size_t
#include <limits> // std::numeric_limits


#define FEATURE_BUFFER_SIZE 200

typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
const QString &uri,
const QgsFieldMap &fields,
Expand Down Expand Up @@ -108,6 +111,8 @@ QgsVectorLayerImport::QgsVectorLayerImport(

QgsVectorLayerImport::~QgsVectorLayerImport()
{
flushBuffer();

if ( mProvider )
delete mProvider;
}
Expand Down Expand Up @@ -142,15 +147,34 @@ bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
}
feat.setAttributeMap( newAttrs );

if ( !mProvider->addFeatures( QgsFeatureList() << feat ) )
mFeatureBuffer.append( feat );

if ( mFeatureBuffer.count() >= FEATURE_BUFFER_SIZE )
{
return flushBuffer();
}

return true;
}

bool QgsVectorLayerImport::flushBuffer()
{
if ( mFeatureBuffer.count() <= 0 )
return true;

if ( !mProvider->addFeatures( mFeatureBuffer ) )
{
mErrorMessage = QObject::tr( "Feature #%1 creation error" ).arg( feat.id() );
mErrorMessage = QObject::tr( "Creation error for features from #%1 to #%2" )
.arg( mFeatureBuffer.first().id() )
.arg( mFeatureBuffer.last().id() );
mError = ErrFeatureWriteFailed;

mFeatureBuffer.clear();
QgsDebugMsg( mErrorMessage );
return false;
}

mFeatureBuffer.clear();
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayerimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class CORE_EXPORT QgsVectorLayerImport
~QgsVectorLayerImport();

protected:
/** flush the buffer writing the features to the new layer */
bool flushBuffer();

/** contains error value */
ImportError mError;
QString mErrorMessage;
Expand All @@ -94,6 +97,8 @@ class CORE_EXPORT QgsVectorLayerImport

/** map attribute indexes to new field indexes */
QMap<int, int> mOldToNewAttrIdx;

QgsFeatureList mFeatureBuffer;
};

#endif

0 comments on commit 37b2c04

Please sign in to comment.