@@ -121,13 +121,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
121
121
const QgsField& attrField = fldIt.value ();
122
122
123
123
OGRFieldType ogrType = OFTString; // default to string
124
- int ogrWidth = - 1 ;
125
- int ogrPrecision = - 1 ;
124
+ int ogrWidth = fldIt-> length () ;
125
+ int ogrPrecision = fldIt-> precision () ;
126
126
switch ( attrField.type () )
127
127
{
128
128
case QVariant::LongLong:
129
129
ogrType = OFTString;
130
- ogrWidth = 21 ;
130
+ ogrWidth = ogrWidth<=21 ? ogrWidth : 21 ;
131
+ ogrPrecision = -1 ;
131
132
break ;
132
133
133
134
case QVariant::String:
@@ -136,13 +137,14 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
136
137
137
138
case QVariant::Int:
138
139
ogrType = OFTInteger;
139
- ogrWidth = 10 ;
140
+ ogrWidth = ogrWidth<=10 ? ogrWidth : 10 ;
141
+ ogrPrecision = 0 ;
140
142
break ;
143
+
141
144
case QVariant::Double:
142
145
ogrType = OFTReal;
143
- ogrWidth = 32 ;
144
- ogrPrecision = 3 ;
145
146
break ;
147
+
146
148
default :
147
149
// assert(0 && "invalid variant type!");
148
150
mError = ErrAttributeTypeUnsupported;
@@ -169,6 +171,8 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
169
171
if ( OGR_L_CreateField ( mLayer , fld, TRUE ) != OGRERR_NONE )
170
172
{
171
173
QgsDebugMsg ( " error creating field " + attrField.name () );
174
+ mError = ErrAttributeCreationFailed;
175
+ return ;
172
176
}
173
177
}
174
178
@@ -201,21 +205,17 @@ bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
201
205
OGRFeatureH poFeature = OGR_F_Create ( OGR_L_GetLayerDefn ( mLayer ) );
202
206
203
207
// 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 )
206
210
{
207
- QgsFieldMap::const_iterator fldIt = mFields .find ( it.key () );
208
- if ( fldIt == mFields .end () )
211
+ if ( !feature.attributeMap ().contains ( fldIt.key () ) )
209
212
{
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 () ) );
212
214
continue ;
213
215
}
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 ];
219
219
220
220
switch ( attrValue.type () )
221
221
{
0 commit comments