Skip to content
Permalink
Browse files

improve vector file writers error reporting

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12890 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Feb 6, 2010
1 parent de3e9d1 commit e3066b0495a58f4bc03adc5d4dd65b023bbddcb5
@@ -31,7 +31,6 @@ public:
const QgsCoordinateReferenceSystem*,
bool onlySelected = FALSE);


/** create shapefile and initialize it */
QgsVectorFileWriter(const QString& shapefileName,
const QString& fileEncoding,
@@ -42,7 +41,12 @@ public:

/** checks whether there were any errors in constructor */
WriterError hasError();


/** retrieves error message
* @note added in 1.5
*/
QString errorMessage();

/** add feature to the currently opened shapefile */
bool addFeature(QgsFeature& feature);

@@ -574,43 +574,18 @@ void QgsLegendLayer::saveAsShapefileGeneral( bool saveOnlySelection )
QApplication::setOverrideCursor( Qt::WaitCursor );

QgsVectorFileWriter::WriterError error;
error = QgsVectorFileWriter::writeAsShapefile( vlayer, shapefileName, encoding, &destCRS, saveOnlySelection );
QString errorMessage;
error = QgsVectorFileWriter::writeAsShapefile( vlayer, shapefileName, encoding, &destCRS, saveOnlySelection, &errorMessage );

QApplication::restoreOverrideCursor();

switch ( error )
if ( error == QgsVectorFileWriter::NoError )
{
case QgsVectorFileWriter::NoError:
QMessageBox::information( 0, tr( "Saving done" ), tr( "Export to Shapefile has been completed" ) );
break;

case QgsVectorFileWriter::ErrDriverNotFound:
QMessageBox::warning( 0, tr( "Driver not found" ), tr( "ESRI Shapefile driver is not available" ) );
break;

case QgsVectorFileWriter::ErrCreateDataSource:
QMessageBox::warning( 0, tr( "Error creating shapefile" ),
tr( "The shapefile could not be created (%1)" ).arg( shapefileName ) );
break;

case QgsVectorFileWriter::ErrCreateLayer:
QMessageBox::warning( 0, tr( "Error" ), tr( "Layer creation failed" ) );
break;

case QgsVectorFileWriter::ErrAttributeTypeUnsupported:
QMessageBox::warning( 0, tr( "Error" ),
tr( "Layer attribute table contains unsupported datatype(s)" ) );
break;

case QgsVectorFileWriter::ErrAttributeCreationFailed:
QMessageBox::warning( 0, tr( "Error" ),
tr( "Creation of an attribute failed" ) );
break;

case QgsVectorFileWriter::ErrProjection:
QMessageBox::warning( 0, tr( "Error" ),
tr( "Reprojection failed" ) );
break;
QMessageBox::information( 0, tr( "Saving done" ), tr( "Export to Shapefile has been completed" ) );
}
else
{
QMessageBox::warning( 0, tr( "Save error" ), errorMessage );
}
}

@@ -24,7 +24,6 @@
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectordataprovider.h"

#include <QFile>
#include <QSettings>
@@ -39,6 +38,7 @@

#include <ogr_api.h>
#include <ogr_srs_api.h>
#include <cpl_error.h>


QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
@@ -58,6 +58,9 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,

if ( poDriver == NULL )
{
mErrorMessage = QObject::tr( "OGR driver for '%1' not found (OGR error: %2)" )
.arg( driverName )
.arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrDriverNotFound;
return;
}
@@ -69,9 +72,11 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
QgsFieldMap::const_iterator fldIt;
for ( fldIt = fields.begin(); fldIt != fields.end(); ++fldIt )
{
QString name = fldIt.value().name().left( 10 );
QString name = fldIt.value().name().left(10);
if ( fieldNames.contains( name ) )
{
mErrorMessage = QObject::tr( "trimming attribute name '%1' to ten significant characters produces duplicate column name." )
.arg( fldIt.value().name() );
mError = ErrAttributeCreationFailed;
return;
}
@@ -84,6 +89,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
if ( mDS == NULL )
{
mError = ErrCreateDataSource;
mErrorMessage = QObject::tr( "creation of data source failed (OGR error:%1)" )
.arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
return;
}

@@ -140,6 +147,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,

if ( mLayer == NULL )
{
mErrorMessage = QObject::tr( "creation of layer failed (OGR error:%1)" )
.arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrCreateLayer;
return;
}
@@ -187,6 +196,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,

default:
//assert(0 && "invalid variant type!");
mErrorMessage = QObject::tr( "unsupported type for field %1" )
.arg( attrField.name() );
mError = ErrAttributeTypeUnsupported;
return;
}
@@ -211,6 +222,9 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
if ( OGR_L_CreateField( mLayer, fld, TRUE ) != OGRERR_NONE )
{
QgsDebugMsg( "error creating field " + attrField.name() );
mErrorMessage = QObject::tr( "creation of field %1 failed (OGR error: %2)" )
.arg( attrField.name() )
.arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrAttributeCreationFailed;
return;
}
@@ -236,6 +250,11 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::hasError()
return mError;
}

QString QgsVectorFileWriter::errorMessage()
{
return mErrorMessage;
}

bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
{
if ( hasError() != NoError )
@@ -364,13 +383,12 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
const QString& shapefileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem* destCRS,
bool onlySelected )
bool onlySelected,
QString *errorMessage )
{

const QgsCoordinateReferenceSystem* outputCRS;
QgsCoordinateTransform* ct = 0;

QgsVectorDataProvider* provider = layer->dataProvider();
int shallTransform = false;

if ( destCRS && destCRS->isValid() )
@@ -385,20 +403,22 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
outputCRS = &layer->srs();
}
QgsVectorFileWriter* writer =
new QgsVectorFileWriter( shapefileName, fileEncoding, provider->fields(), provider->geometryType(), outputCRS );
new QgsVectorFileWriter( shapefileName, fileEncoding, layer->pendingFields(), layer->wkbType(), outputCRS );

// check whether file creation was successful
WriterError err = writer->hasError();
if ( err != NoError )
{
if ( errorMessage )
*errorMessage = writer->errorMessage();
delete writer;
return err;
}

QgsAttributeList allAttr = provider->attributeIndexes();
QgsAttributeList allAttr = layer->pendingAllAttributesList();
QgsFeature fet;

provider->select( allAttr, QgsRectangle(), true );
layer->select( allAttr, QgsRectangle(), true );

const QgsFeatureIds& ids = layer->selectedFeaturesIds();

@@ -415,7 +435,7 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
}

// write all features
while ( provider->nextFeature( fet ) )
while ( layer->nextFeature( fet ) )
{
if ( onlySelected && !ids.contains( fet.id() ) )
continue;
@@ -431,10 +451,11 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
delete ct;
delete writer;

QString msg( "Failed to transform a point while drawing a feature of type '"
+ fet.typeName() + "'. Writing stopped." );
msg += e.what();
QString msg = QObject::tr("Failed to transform a point while drawing a feature of type '%1'. Writing stopped.")
.arg( fet.typeName() );
QgsLogger::warning( msg );
if( errorMessage )
*errorMessage = msg;

return ErrProjection;
}
@@ -58,8 +58,8 @@ class CORE_EXPORT QgsVectorFileWriter
const QString& shapefileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = FALSE );

bool onlySelected = FALSE,
QString *errorMessage = 0 );

/** create shapefile and initialize it */
QgsVectorFileWriter( const QString& shapefileName,
@@ -72,6 +72,11 @@ class CORE_EXPORT QgsVectorFileWriter
/** checks whether there were any errors in constructor */
WriterError hasError();

/** retrieves error message
* @note added in 1.5
*/
QString errorMessage();

/** add feature to the currently opened shapefile */
bool addFeature( QgsFeature& feature );

@@ -95,8 +100,9 @@ class CORE_EXPORT QgsVectorFileWriter

/** contains error value if construction was not successful */
WriterError mError;
QString mErrorMessage;

QTextCodec* mCodec;
QTextCodec *mCodec;

/** geometry type which is being used */
QGis::WkbType mWkbType;

0 comments on commit e3066b0

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