Skip to content

Commit 16b91c6

Browse files
author
jef
committedJul 30, 2009
fix #1664 and #1681
git-svn-id: http://svn.osgeo.org/qgis/trunk@11218 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ad7937e commit 16b91c6

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed
 

‎src/app/legend/qgslegendlayerfile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,16 @@ void QgsLegendLayerFile::saveAsShapefileGeneral( bool saveOnlySelection )
334334
case QgsVectorFileWriter::ErrCreateLayer:
335335
QMessageBox::warning( 0, tr( "Error" ), tr( "Layer creation failed" ) );
336336
break;
337+
337338
case QgsVectorFileWriter::ErrAttributeTypeUnsupported:
338339
QMessageBox::warning( 0, tr( "Error" ),
339340
tr( "Layer attribute table contains unsupported datatype(s)" ) );
340341
break;
342+
343+
case QgsVectorFileWriter::ErrAttributeCreationFailed:
344+
QMessageBox::warning( 0, tr( "Error" ),
345+
tr( "Creation of an attribute failed" ) );
346+
break;
341347
}
342348
}
343349

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
121121
const QgsField& attrField = fldIt.value();
122122

123123
OGRFieldType ogrType = OFTString; //default to string
124-
int ogrWidth = -1;
125-
int ogrPrecision = -1;
124+
int ogrWidth = fldIt->length();
125+
int ogrPrecision = fldIt->precision();
126126
switch ( attrField.type() )
127127
{
128128
case QVariant::LongLong:
129129
ogrType = OFTString;
130-
ogrWidth = 21;
130+
ogrWidth = ogrWidth<=21 ? ogrWidth : 21;
131+
ogrPrecision = -1;
131132
break;
132133

133134
case QVariant::String:
@@ -136,13 +137,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
136137

137138
case QVariant::Int:
138139
ogrType = OFTInteger;
139-
ogrWidth = 10;
140+
ogrWidth = ogrWidth<=10 ? ogrWidth : 10;
141+
ogrPrecision = 0;
140142
break;
143+
141144
case QVariant::Double:
142145
ogrType = OFTReal;
143-
ogrWidth = 32;
144-
ogrPrecision = 3;
145146
break;
147+
146148
default:
147149
//assert(0 && "invalid variant type!");
148150
mError = ErrAttributeTypeUnsupported;
@@ -169,6 +171,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
169171
if ( OGR_L_CreateField( mLayer, fld, TRUE ) != OGRERR_NONE )
170172
{
171173
QgsDebugMsg( "error creating field " + attrField.name() );
174+
mError = ErrAttributeCreationFailed;
175+
return;
172176
}
173177
}
174178

@@ -201,21 +205,17 @@ bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
201205
OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );
202206

203207
// attribute handling
204-
const QgsAttributeMap& attributes = feature.attributeMap();
205-
for ( it = attributes.begin(); it != attributes.end(); it++ )
208+
QgsFieldMap::const_iterator fldIt;
209+
for ( fldIt = mFields.begin(); fldIt != mFields.end(); ++fldIt )
206210
{
207-
QgsFieldMap::const_iterator fldIt = mFields.find( it.key() );
208-
if ( fldIt == mFields.end() )
211+
if( !feature.attributeMap().contains( fldIt.key() ) )
209212
{
210-
QgsDebugMsg( "ignoring attribute that's not in field map: type=" +
211-
QString( it.value().typeName() ) + " value=" + it.value().toString() );
213+
QgsDebugMsg( QString("no attribute for field %1").arg( fldIt.key() ) );
212214
continue;
213215
}
214-
215-
QString attrName = mFields[it.key()].name();
216-
QByteArray encAttrName = mCodec->fromUnicode( attrName );
217-
const QVariant& attrValue = it.value();
218-
int ogrField = OGR_F_GetFieldIndex( poFeature, encAttrName.data() );
216+
217+
int ogrField = fldIt.key();
218+
const QVariant& attrValue = feature.attributeMap()[ ogrField ];
219219

220220
switch ( attrValue.type() )
221221
{

‎src/core/qgsvectorfilewriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class CORE_EXPORT QgsVectorFileWriter
4848
ErrDriverNotFound,
4949
ErrCreateDataSource,
5050
ErrCreateLayer,
51-
ErrAttributeTypeUnsupported
51+
ErrAttributeTypeUnsupported,
52+
ErrAttributeCreationFailed
5253
};
5354

5455
/** Write contents of vector layer to a shapefile */

0 commit comments

Comments
 (0)
Please sign in to comment.