[QGIS Commit] r12890 - in trunk/qgis: python/core src/app/legend src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Feb 6 14:27:38 EST 2010


Author: jef
Date: 2010-02-06 14:27:36 -0500 (Sat, 06 Feb 2010)
New Revision: 12890

Modified:
   trunk/qgis/python/core/qgsvectorfilewriter.sip
   trunk/qgis/src/app/legend/qgslegendlayer.cpp
   trunk/qgis/src/core/qgsvectorfilewriter.cpp
   trunk/qgis/src/core/qgsvectorfilewriter.h
Log:
improve vector file writers error reporting

Modified: trunk/qgis/python/core/qgsvectorfilewriter.sip
===================================================================
--- trunk/qgis/python/core/qgsvectorfilewriter.sip	2010-02-06 05:41:44 UTC (rev 12889)
+++ trunk/qgis/python/core/qgsvectorfilewriter.sip	2010-02-06 19:27:36 UTC (rev 12890)
@@ -31,7 +31,6 @@
                                         const QgsCoordinateReferenceSystem*,
                                         bool onlySelected = FALSE);
 
-
     /** create shapefile and initialize it */
     QgsVectorFileWriter(const QString& shapefileName,
                         const QString& fileEncoding,
@@ -42,7 +41,12 @@
     
     /** 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);
     

Modified: trunk/qgis/src/app/legend/qgslegendlayer.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgslegendlayer.cpp	2010-02-06 05:41:44 UTC (rev 12889)
+++ trunk/qgis/src/app/legend/qgslegendlayer.cpp	2010-02-06 19:27:36 UTC (rev 12890)
@@ -574,44 +574,19 @@
   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 );
+  }
 }
 
 

Modified: trunk/qgis/src/core/qgsvectorfilewriter.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorfilewriter.cpp	2010-02-06 05:41:44 UTC (rev 12889)
+++ trunk/qgis/src/core/qgsvectorfilewriter.cpp	2010-02-06 19:27:36 UTC (rev 12890)
@@ -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 @@
 
   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 @@
     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 @@
   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 @@
 
   if ( mLayer == NULL )
   {
+    mErrorMessage = QObject::tr( "creation of layer failed (OGR error:%1)" )
+                    .arg( QString::fromUtf8( CPLGetLastErrorMsg() ) );
     mError = ErrCreateLayer;
     return;
   }
@@ -187,6 +196,8 @@
 
       default:
         //assert(0 && "invalid variant type!");
+        mErrorMessage = QObject::tr( "unsupported type for field %1" )
+                        .arg( attrField.name() );
         mError = ErrAttributeTypeUnsupported;
         return;
     }
@@ -211,6 +222,9 @@
     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 @@
   return mError;
 }
 
+QString QgsVectorFileWriter::errorMessage()
+{
+  return mErrorMessage;
+}
+
 bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
 {
   if ( hasError() != NoError )
@@ -364,13 +383,12 @@
                                        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 @@
     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 @@
   }
 
   // write all features
-  while ( provider->nextFeature( fet ) )
+  while ( layer->nextFeature( fet ) )
   {
     if ( onlySelected && !ids.contains( fet.id() ) )
       continue;
@@ -431,10 +451,11 @@
         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;
       }

Modified: trunk/qgis/src/core/qgsvectorfilewriter.h
===================================================================
--- trunk/qgis/src/core/qgsvectorfilewriter.h	2010-02-06 05:41:44 UTC (rev 12889)
+++ trunk/qgis/src/core/qgsvectorfilewriter.h	2010-02-06 19:27:36 UTC (rev 12890)
@@ -58,9 +58,9 @@
                                          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,
                          const QString& fileEncoding,
@@ -72,6 +72,11 @@
     /** 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 @@
 
     /** 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;



More information about the QGIS-commit mailing list