Skip to content

Commit

Permalink
fix #1664 and #1681
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11218 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jul 30, 2009
1 parent a483e57 commit 829f074
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/app/legend/qgslegendlayerfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,16 @@ void QgsLegendLayerFile::saveAsShapefileGeneral( bool saveOnlySelection )
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;
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
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:
Expand All @@ -136,13 +137,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,

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;
Expand All @@ -169,6 +171,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
if ( OGR_L_CreateField( mLayer, fld, TRUE ) != OGRERR_NONE )
{
QgsDebugMsg( "error creating field " + attrField.name() );
mError = ErrAttributeCreationFailed;
return;
}
}

Expand Down Expand Up @@ -201,21 +205,17 @@ bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
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;
}

QString attrName = mFields[it.key()].name();
QByteArray encAttrName = mCodec->fromUnicode( attrName );
const QVariant& attrValue = it.value();
int ogrField = OGR_F_GetFieldIndex( poFeature, encAttrName.data() );

int ogrField = fldIt.key();
const QVariant& attrValue = feature.attributeMap()[ ogrField ];

switch ( attrValue.type() )
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsvectorfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CORE_EXPORT QgsVectorFileWriter
ErrDriverNotFound,
ErrCreateDataSource,
ErrCreateLayer,
ErrAttributeTypeUnsupported
ErrAttributeTypeUnsupported,
ErrAttributeCreationFailed
};

/** Write contents of vector layer to a shapefile */
Expand Down

0 comments on commit 829f074

Please sign in to comment.