Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ogr provider: increase width by one for decimal point if precision is…
… given

(fixes #11755, fixes #12985)

(cherry picked from commit 2fcbc8b and 513cfad)
  • Loading branch information
jef-n committed Jun 29, 2015
1 parent 38961dc commit c980332
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -344,6 +344,9 @@ QgsVectorFileWriter::QgsVectorFileWriter(
OGRFieldType ogrType = OFTString; //default to string
int ogrWidth = attrField.length();
int ogrPrecision = attrField.precision();
if ( ogrPrecision > 0 )
++ogrWidth;

switch ( attrField.type() )
{
case QVariant::LongLong:
Expand Down
19 changes: 16 additions & 3 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -82,6 +82,8 @@ bool QgsOgrProvider::convertField( QgsField &field, const QTextCodec &encoding )
OGRFieldType ogrType = OFTString; //default to string
int ogrWidth = field.length();
int ogrPrecision = field.precision();
if ( ogrPrecision > 0 )
ogrWidth += 1;
switch ( field.type() )
{
case QVariant::LongLong:
Expand Down Expand Up @@ -791,6 +793,11 @@ void QgsOgrProvider::loadFields()
}
}

int width = OGR_Fld_GetWidth( fldDef );
int prec = OGR_Fld_GetPrecision( fldDef );
if ( prec > 0 )
width -= 1;

mAttributeFields.append(
QgsField(
name,
Expand All @@ -800,8 +807,9 @@ void QgsOgrProvider::loadFields()
#else
mEncoding->toUnicode( OGR_GetFieldTypeName( ogrType ) ),
#endif
OGR_Fld_GetWidth( fldDef ),
OGR_Fld_GetPrecision( fldDef ) ) );
width, prec
)
);
}
}
}
Expand Down Expand Up @@ -1144,7 +1152,10 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
}

OGRFieldDefnH fielddefn = OGR_Fld_Create( mEncoding->fromUnicode( iter->name() ).constData(), type );
OGR_Fld_SetWidth( fielddefn, iter->length() );
int width = iter->length();
if ( iter->precision() )
width += 1;
OGR_Fld_SetWidth( fielddefn, width );
OGR_Fld_SetPrecision( fielddefn, iter->precision() );

if ( OGR_L_CreateField( ogrLayer, fielddefn, true ) != OGRERR_NONE )
Expand Down Expand Up @@ -2172,6 +2183,8 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,

int width = fields.size() > 1 ? fields[1].toInt() : -1;
int precision = fields.size() > 2 ? fields[2].toInt() : -1;
if ( precision > 0 )
width += 1;

OGRFieldDefnH field;
if ( fields[0] == "Real" )
Expand Down

0 comments on commit c980332

Please sign in to comment.