Skip to content
Permalink
Browse files

convert field names to lowercase when importing from shapefile,

fix the number of written features,
cleanup the code
  • Loading branch information
brushtyler committed Jan 13, 2012
1 parent 710aa4c commit 59418069289525dda1104a1df76044bc17630d05
Showing with 53 additions and 34 deletions.
  1. +49 −34 src/core/qgsvectorlayerimport.cpp
  2. +4 −0 src/core/qgsvectorlayerimport.h
@@ -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(
@@ -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 );
@@ -170,6 +157,7 @@ bool QgsVectorLayerImport::flushBuffer()
.arg( mFeatureBuffer.first().id() )
.arg( mFeatureBuffer.last().id() );
mError = ErrFeatureWriteFailed;
mErrorCount += mFeatureBuffer.count();

mFeatureBuffer.clear();
QgsDebugMsg( mErrorMessage );
@@ -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();
@@ -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 ) )
@@ -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;
}
@@ -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;
@@ -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;
@@ -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 );

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

int mErrorCount;

QgsVectorDataProvider *mProvider;

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

0 comments on commit 5941806

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