Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve code readability
  • Loading branch information
m-kuhn committed Oct 29, 2018
1 parent b00b3f6 commit 1387631
Showing 1 changed file with 148 additions and 147 deletions.
295 changes: 148 additions & 147 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -492,190 +492,191 @@ void QgsVectorFileWriter::init( QString vectorFileName,

mFieldValueConverter = fieldValueConverter;

for ( int fldIdx = 0; ( action == CreateOrOverwriteFile ||
action == CreateOrOverwriteLayer ||
action == AppendToLayerAddFields ) &&
fldIdx < fields.count(); ++fldIdx )
if ( action == CreateOrOverwriteFile ||
action == CreateOrOverwriteLayer ||
action == AppendToLayerAddFields )
{
QgsField attrField = fields.at( fldIdx );

if ( fieldValueConverter )
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
{
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
}
QgsField attrField = fields.at( fldIdx );

QString name( attrField.name() );
if ( action == AppendToLayerAddFields )
{
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
if ( ogrIdx >= 0 )
if ( fieldValueConverter )
{
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
continue;
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
}
}

OGRFieldType ogrType = OFTString; //default to string
int ogrWidth = attrField.length();
int ogrPrecision = attrField.precision();
if ( ogrPrecision > 0 )
++ogrWidth;

switch ( attrField.type() )
{
case QVariant::LongLong:
QString name( attrField.name() );
if ( action == AppendToLayerAddFields )
{
const char *pszDataTypes = GDALGetMetadataItem( poDriver, GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
ogrType = OFTInteger64;
else
ogrType = OFTReal;
ogrWidth = ogrWidth > 0 && ogrWidth <= 20 ? ogrWidth : 20;
ogrPrecision = 0;
break;
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
if ( ogrIdx >= 0 )
{
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
continue;
}
}
case QVariant::String:
ogrType = OFTString;
if ( ogrWidth <= 0 || ogrWidth > 255 )
ogrWidth = 255;
break;

case QVariant::Int:
ogrType = OFTInteger;
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
ogrPrecision = 0;
break;

case QVariant::Bool:
ogrType = OFTInteger;
ogrWidth = 1;
ogrPrecision = 0;
break;

case QVariant::Double:
ogrType = OFTReal;
break;
OGRFieldType ogrType = OFTString; //default to string
int ogrWidth = attrField.length();
int ogrPrecision = attrField.precision();
if ( ogrPrecision > 0 )
++ogrWidth;

case QVariant::Date:
ogrType = OFTDate;
break;

case QVariant::Time:
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
{
ogrType = OFTString;
ogrWidth = 12; // %02d:%02d:%06.3f
}
else
switch ( attrField.type() )
{
case QVariant::LongLong:
{
ogrType = OFTTime;
const char *pszDataTypes = GDALGetMetadataItem( poDriver, GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
ogrType = OFTInteger64;
else
ogrType = OFTReal;
ogrWidth = ogrWidth > 0 && ogrWidth <= 20 ? ogrWidth : 20;
ogrPrecision = 0;
break;
}
break;

case QVariant::DateTime:
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
{
case QVariant::String:
ogrType = OFTString;
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
}
else
{
ogrType = OFTDateTime;
}
break;
if ( ogrWidth <= 0 || ogrWidth > 255 )
ogrWidth = 255;
break;

default:
//assert(0 && "invalid variant type!");
mErrorMessage = QObject::tr( "Unsupported type for field %1" )
.arg( attrField.name() );
mError = ErrAttributeTypeUnsupported;
return;
}
case QVariant::Int:
ogrType = OFTInteger;
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
ogrPrecision = 0;
break;

if ( mOgrDriverName == QLatin1String( "SQLite" ) && name.compare( QLatin1String( "ogc_fid" ), Qt::CaseInsensitive ) == 0 )
{
int i;
for ( i = 0; i < 10; i++ )
{
name = QStringLiteral( "ogc_fid%1" ).arg( i );
case QVariant::Bool:
ogrType = OFTInteger;
ogrWidth = 1;
ogrPrecision = 0;
break;

int j;
for ( j = 0; j < fields.size() && name.compare( fields.at( j ).name(), Qt::CaseInsensitive ) != 0; j++ )
;
case QVariant::Double:
ogrType = OFTReal;
break;

case QVariant::Date:
ogrType = OFTDate;
break;

if ( j == fields.size() )
case QVariant::Time:
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
{
ogrType = OFTString;
ogrWidth = 12; // %02d:%02d:%06.3f
}
else
{
ogrType = OFTTime;
}
break;

case QVariant::DateTime:
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
{
ogrType = OFTString;
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
}
else
{
ogrType = OFTDateTime;
}
break;

default:
//assert(0 && "invalid variant type!");
mErrorMessage = QObject::tr( "Unsupported type for field %1" )
.arg( attrField.name() );
mError = ErrAttributeTypeUnsupported;
return;
}

if ( i == 10 )
if ( mOgrDriverName == QLatin1String( "SQLite" ) && name.compare( QLatin1String( "ogc_fid" ), Qt::CaseInsensitive ) == 0 )
{
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
mError = ErrAttributeCreationFailed;
return;
}
int i;
for ( i = 0; i < 10; i++ )
{
name = QStringLiteral( "ogc_fid%1" ).arg( i );

QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
}
int j;
for ( j = 0; j < fields.size() && name.compare( fields.at( j ).name(), Qt::CaseInsensitive ) != 0; j++ )
;

// create field definition
gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType ) );
if ( ogrWidth > 0 )
{
OGR_Fld_SetWidth( fld.get(), ogrWidth );
}
if ( j == fields.size() )
break;
}

if ( ogrPrecision >= 0 )
{
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
}
if ( i == 10 )
{
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
mError = ErrAttributeCreationFailed;
return;
}

switch ( attrField.type() )
{
case QVariant::Bool:
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
break;
default:
break;
}
QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
}

// create the field
QgsDebugMsg( "creating field " + attrField.name() +
" type " + QString( QVariant::typeToName( attrField.type() ) ) +
" width " + QString::number( ogrWidth ) +
" precision " + QString::number( ogrPrecision ) );
if ( OGR_L_CreateField( mLayer, fld.get(), true ) != OGRERR_NONE )
{
QgsDebugMsg( "error creating field " + attrField.name() );
mErrorMessage = QObject::tr( "Creation of field %1 failed (OGR error: %2)" )
.arg( attrField.name(),
QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrAttributeCreationFailed;
return;
}
// create field definition
gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType ) );
if ( ogrWidth > 0 )
{
OGR_Fld_SetWidth( fld.get(), ogrWidth );
}

int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
QgsDebugMsg( QStringLiteral( "returned field index for %1: %2" ).arg( name ).arg( ogrIdx ) );
if ( ogrIdx < 0 || existingIdxs.contains( ogrIdx ) )
{
// GDAL 1.7 not just truncates, but launders more aggressivly.
ogrIdx = OGR_FD_GetFieldCount( defn ) - 1;
if ( ogrPrecision >= 0 )
{
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
}

if ( ogrIdx < 0 )
switch ( attrField.type() )
{
case QVariant::Bool:
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
break;
default:
break;
}

// create the field
QgsDebugMsg( "creating field " + attrField.name() +
" type " + QString( QVariant::typeToName( attrField.type() ) ) +
" width " + QString::number( ogrWidth ) +
" precision " + QString::number( ogrPrecision ) );
if ( OGR_L_CreateField( mLayer, fld.get(), true ) != OGRERR_NONE )
{
QgsDebugMsg( "error creating field " + attrField.name() );
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
mErrorMessage = QObject::tr( "Creation of field %1 failed (OGR error: %2)" )
.arg( attrField.name(),
QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrAttributeCreationFailed;
return;
}
}

existingIdxs.insert( ogrIdx );
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
}
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
QgsDebugMsg( QStringLiteral( "returned field index for %1: %2" ).arg( name ).arg( ogrIdx ) );
if ( ogrIdx < 0 || existingIdxs.contains( ogrIdx ) )
{
// GDAL 1.7 not just truncates, but launders more aggressivly.
ogrIdx = OGR_FD_GetFieldCount( defn ) - 1;

if ( action == AppendToLayerNoNewFields )
if ( ogrIdx < 0 )
{
QgsDebugMsg( "error creating field " + attrField.name() );
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
.arg( attrField.name(),
QString::fromUtf8( CPLGetLastErrorMsg() ) );
mError = ErrAttributeCreationFailed;
return;
}
}

existingIdxs.insert( ogrIdx );
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
}
}
else if ( action == AppendToLayerNoNewFields )
{
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
{
Expand Down

0 comments on commit 1387631

Please sign in to comment.