Skip to content

Commit

Permalink
convert field names to lowercase when importing from shapefile,
Browse files Browse the repository at this point in the history
fix the number of written features,
cleanup the code
  • Loading branch information
brushtyler committed Jan 13, 2012
1 parent 710aa4c commit 5941806
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
83 changes: 49 additions & 34 deletions src/core/qgsvectorlayerimport.cpp
Expand Up @@ -25,20 +25,6 @@
#include "qgsvectorlayerimport.h"
#include "qgsproviderregistry.h"

#include <QFile>
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <QTextCodec>
#include <QTextStream>
#include <QSet>
#include <QMetaType>

#include <cassert>
#include <cstdlib> // size_t
#include <limits> // std::numeric_limits


#define FEATURE_BUFFER_SIZE 200

typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
Expand All @@ -53,16 +39,17 @@ typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
);


QgsVectorLayerImport::QgsVectorLayerImport(
const QString &uri,
const QString &providerKey,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
bool overwrite,
const QMap<QString, QVariant> *options )
QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
const QString &providerKey,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
bool overwrite,
const QMap<QString, QVariant> *options )
: mErrorCount( 0 )
{
mProvider = NULL;

QgsProviderRegistry * pReg = QgsProviderRegistry::instance();

QLibrary *myLib = pReg->providerLibrary( providerKey );
Expand Down Expand Up @@ -170,6 +157,7 @@ bool QgsVectorLayerImport::flushBuffer()
.arg( mFeatureBuffer.first().id() )
.arg( mFeatureBuffer.last().id() );
mError = ErrFeatureWriteFailed;
mErrorCount += mFeatureBuffer.count();

mFeatureBuffer.clear();
QgsDebugMsg( mErrorMessage );
Expand Down Expand Up @@ -212,8 +200,18 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
outputCRS = &layer->crs();
}

QgsFieldMap fields = skipAttributeCreation ? QgsFieldMap() : layer->pendingFields();
if ( layer->providerType() == "ogr" && layer->storageType() == "ESRI Shapefile" )
{
// convert field names to lowercase
for ( QgsFieldMap::iterator fldIt = fields.begin(); fldIt != fields.end(); ++fldIt )
{
fldIt.value().setName( fldIt.value().name().toLower() );
}
}

QgsVectorLayerImport * writer =
new QgsVectorLayerImport( uri, providerKey, skipAttributeCreation ? QgsFieldMap() : layer->pendingFields(), layer->wkbType(), outputCRS, false, options );
new QgsVectorLayerImport( uri, providerKey, fields, layer->wkbType(), outputCRS, false, options );

// check whether file creation was successful
ImportError err = writer->hasError();
Expand Down Expand Up @@ -249,7 +247,12 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
shallTransform = false;
}

int n = 0, errors = 0;
int n = 0;

if ( errorMessage )
{
*errorMessage = QObject::tr( "Feature write errors:" );
}

// write all features
while ( layer->nextFeature( fet ) )
Expand All @@ -275,7 +278,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
.arg( fet.typeName() ).arg( e.what() );
QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
if ( errorMessage )
*errorMessage = msg;
*errorMessage += "\n" + msg;

return ErrProjection;
}
Expand All @@ -288,19 +291,14 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
{
if ( writer->hasError() && errorMessage )
{
if ( errorMessage->isEmpty() )
{
*errorMessage = QObject::tr( "Feature write errors:" );
}
*errorMessage += "\n" + writer->errorMessage();
}
errors++;

if ( errors > 1000 )
if ( writer->errorCount() > 1000 )
{
if ( errorMessage )
{
*errorMessage += QObject::tr( "Stopping after %1 errors" ).arg( errors );
*errorMessage += "\n" + QObject::tr( "Stopping after %1 errors" ).arg( writer->errorCount() );
}

n = -1;
Expand All @@ -310,16 +308,33 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
n++;
}

// flush the buffer to be sure that all features are written
if ( !writer->flushBuffer() )
{
if ( writer->hasError() && errorMessage )
{
*errorMessage += "\n" + writer->errorMessage();
}
}
int errors = writer->errorCount();

delete writer;

if ( shallTransform )
{
delete ct;
}

if ( errors > 0 && errorMessage && n > 0 )
if ( errorMessage )
{
*errorMessage += QObject::tr( "\nOnly %1 of %2 features written." ).arg( n - errors ).arg( n );
if ( n > 0 && errors > 0 )
{
*errorMessage += "\n" + QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n );
}
else
{
errorMessage->clear();
}
}

return errors == 0 ? NoError : ErrFeatureWriteFailed;
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsvectorlayerimport.h
Expand Up @@ -79,6 +79,8 @@ class CORE_EXPORT QgsVectorLayerImport
/** retrieves error message */
QString errorMessage();

int errorCount() const { return mErrorCount; }

/** add feature to the new created layer */
bool addFeature( QgsFeature& feature );

Expand All @@ -93,6 +95,8 @@ class CORE_EXPORT QgsVectorLayerImport
ImportError mError;
QString mErrorMessage;

int mErrorCount;

QgsVectorDataProvider *mProvider;

/** map attribute indexes to new field indexes */
Expand Down

0 comments on commit 5941806

Please sign in to comment.