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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jul 30 17:16:53 EDT 2009


Author: jef
Date: 2009-07-30 17:16:53 -0400 (Thu, 30 Jul 2009)
New Revision: 11218

Modified:
   trunk/qgis/src/app/legend/qgslegendlayerfile.cpp
   trunk/qgis/src/core/qgsvectorfilewriter.cpp
   trunk/qgis/src/core/qgsvectorfilewriter.h
Log:
fix #1664 and #1681

Modified: trunk/qgis/src/app/legend/qgslegendlayerfile.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgslegendlayerfile.cpp	2009-07-30 14:23:25 UTC (rev 11217)
+++ trunk/qgis/src/app/legend/qgslegendlayerfile.cpp	2009-07-30 21:16:53 UTC (rev 11218)
@@ -334,10 +334,16 @@
     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;
   }
 }
 

Modified: trunk/qgis/src/core/qgsvectorfilewriter.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorfilewriter.cpp	2009-07-30 14:23:25 UTC (rev 11217)
+++ trunk/qgis/src/core/qgsvectorfilewriter.cpp	2009-07-30 21:16:53 UTC (rev 11218)
@@ -121,13 +121,14 @@
     const QgsField& attrField = fldIt.value();
 
     OGRFieldType ogrType = OFTString; //default to string
-    int ogrWidth = -1;
-    int ogrPrecision = -1;
+    int ogrWidth = fldIt->length();
+    int ogrPrecision = fldIt->precision();
     switch ( attrField.type() )
     {
       case QVariant::LongLong:
         ogrType = OFTString;
-        ogrWidth = 21;
+        ogrWidth = ogrWidth<=21 ? ogrWidth : 21;
+        ogrPrecision = -1;
         break;
 
       case QVariant::String:
@@ -136,13 +137,14 @@
 
       case QVariant::Int:
         ogrType = OFTInteger;
-        ogrWidth = 10;
+        ogrWidth = ogrWidth<=10 ? ogrWidth : 10;
+        ogrPrecision = 0;
         break;
+
       case QVariant::Double:
         ogrType = OFTReal;
-        ogrWidth = 32;
-        ogrPrecision = 3;
         break;
+
       default:
         //assert(0 && "invalid variant type!");
         mError = ErrAttributeTypeUnsupported;
@@ -169,6 +171,8 @@
     if ( OGR_L_CreateField( mLayer, fld, TRUE ) != OGRERR_NONE )
     {
       QgsDebugMsg( "error creating field " + attrField.name() );
+      mError = ErrAttributeCreationFailed;
+      return;
     }
   }
 
@@ -201,22 +205,18 @@
   OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );
 
   // attribute handling
-  const QgsAttributeMap& attributes = feature.attributeMap();
-  for ( it = attributes.begin(); it != attributes.end(); it++ )
+  QgsFieldMap::const_iterator fldIt;
+  for ( fldIt = mFields.begin(); fldIt != mFields.end(); ++fldIt )
   {
-    QgsFieldMap::const_iterator fldIt = mFields.find( it.key() );
-    if ( fldIt == mFields.end() )
+    if( !feature.attributeMap().contains( fldIt.key() ) )
     {
-      QgsDebugMsg( "ignoring attribute that's not in field map: type=" +
-                   QString( it.value().typeName() ) + " value=" + it.value().toString() );
+      QgsDebugMsg( QString("no attribute for field %1").arg( fldIt.key() ) );
       continue;
     }
+       
+    int ogrField = fldIt.key();
+    const QVariant& attrValue = feature.attributeMap()[ ogrField ];
 
-    QString attrName = mFields[it.key()].name();
-    QByteArray encAttrName = mCodec->fromUnicode( attrName );
-    const QVariant& attrValue = it.value();
-    int ogrField = OGR_F_GetFieldIndex( poFeature, encAttrName.data() );
-
     switch ( attrValue.type() )
     {
       case QVariant::Int:

Modified: trunk/qgis/src/core/qgsvectorfilewriter.h
===================================================================
--- trunk/qgis/src/core/qgsvectorfilewriter.h	2009-07-30 14:23:25 UTC (rev 11217)
+++ trunk/qgis/src/core/qgsvectorfilewriter.h	2009-07-30 21:16:53 UTC (rev 11218)
@@ -48,7 +48,8 @@
       ErrDriverNotFound,
       ErrCreateDataSource,
       ErrCreateLayer,
-      ErrAttributeTypeUnsupported
+      ErrAttributeTypeUnsupported,
+      ErrAttributeCreationFailed
     };
 
     /** Write contents of vector layer to a shapefile */



More information about the QGIS-commit mailing list