Skip to content

Commit 1387631

Browse files
committed
Improve code readability
1 parent b00b3f6 commit 1387631

File tree

1 file changed

+148
-147
lines changed

1 file changed

+148
-147
lines changed

src/core/qgsvectorfilewriter.cpp

+148-147
Original file line numberDiff line numberDiff line change
@@ -492,190 +492,191 @@ void QgsVectorFileWriter::init( QString vectorFileName,
492492

493493
mFieldValueConverter = fieldValueConverter;
494494

495-
for ( int fldIdx = 0; ( action == CreateOrOverwriteFile ||
496-
action == CreateOrOverwriteLayer ||
497-
action == AppendToLayerAddFields ) &&
498-
fldIdx < fields.count(); ++fldIdx )
495+
if ( action == CreateOrOverwriteFile ||
496+
action == CreateOrOverwriteLayer ||
497+
action == AppendToLayerAddFields )
499498
{
500-
QgsField attrField = fields.at( fldIdx );
501-
502-
if ( fieldValueConverter )
499+
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
503500
{
504-
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
505-
}
501+
QgsField attrField = fields.at( fldIdx );
506502

507-
QString name( attrField.name() );
508-
if ( action == AppendToLayerAddFields )
509-
{
510-
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
511-
if ( ogrIdx >= 0 )
503+
if ( fieldValueConverter )
512504
{
513-
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
514-
continue;
505+
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
515506
}
516-
}
517507

518-
OGRFieldType ogrType = OFTString; //default to string
519-
int ogrWidth = attrField.length();
520-
int ogrPrecision = attrField.precision();
521-
if ( ogrPrecision > 0 )
522-
++ogrWidth;
523-
524-
switch ( attrField.type() )
525-
{
526-
case QVariant::LongLong:
508+
QString name( attrField.name() );
509+
if ( action == AppendToLayerAddFields )
527510
{
528-
const char *pszDataTypes = GDALGetMetadataItem( poDriver, GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
529-
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
530-
ogrType = OFTInteger64;
531-
else
532-
ogrType = OFTReal;
533-
ogrWidth = ogrWidth > 0 && ogrWidth <= 20 ? ogrWidth : 20;
534-
ogrPrecision = 0;
535-
break;
511+
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
512+
if ( ogrIdx >= 0 )
513+
{
514+
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
515+
continue;
516+
}
536517
}
537-
case QVariant::String:
538-
ogrType = OFTString;
539-
if ( ogrWidth <= 0 || ogrWidth > 255 )
540-
ogrWidth = 255;
541-
break;
542518

543-
case QVariant::Int:
544-
ogrType = OFTInteger;
545-
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
546-
ogrPrecision = 0;
547-
break;
548-
549-
case QVariant::Bool:
550-
ogrType = OFTInteger;
551-
ogrWidth = 1;
552-
ogrPrecision = 0;
553-
break;
554-
555-
case QVariant::Double:
556-
ogrType = OFTReal;
557-
break;
519+
OGRFieldType ogrType = OFTString; //default to string
520+
int ogrWidth = attrField.length();
521+
int ogrPrecision = attrField.precision();
522+
if ( ogrPrecision > 0 )
523+
++ogrWidth;
558524

559-
case QVariant::Date:
560-
ogrType = OFTDate;
561-
break;
562-
563-
case QVariant::Time:
564-
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
565-
{
566-
ogrType = OFTString;
567-
ogrWidth = 12; // %02d:%02d:%06.3f
568-
}
569-
else
525+
switch ( attrField.type() )
526+
{
527+
case QVariant::LongLong:
570528
{
571-
ogrType = OFTTime;
529+
const char *pszDataTypes = GDALGetMetadataItem( poDriver, GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
530+
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
531+
ogrType = OFTInteger64;
532+
else
533+
ogrType = OFTReal;
534+
ogrWidth = ogrWidth > 0 && ogrWidth <= 20 ? ogrWidth : 20;
535+
ogrPrecision = 0;
536+
break;
572537
}
573-
break;
574-
575-
case QVariant::DateTime:
576-
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
577-
{
538+
case QVariant::String:
578539
ogrType = OFTString;
579-
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
580-
}
581-
else
582-
{
583-
ogrType = OFTDateTime;
584-
}
585-
break;
540+
if ( ogrWidth <= 0 || ogrWidth > 255 )
541+
ogrWidth = 255;
542+
break;
586543

587-
default:
588-
//assert(0 && "invalid variant type!");
589-
mErrorMessage = QObject::tr( "Unsupported type for field %1" )
590-
.arg( attrField.name() );
591-
mError = ErrAttributeTypeUnsupported;
592-
return;
593-
}
544+
case QVariant::Int:
545+
ogrType = OFTInteger;
546+
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
547+
ogrPrecision = 0;
548+
break;
594549

595-
if ( mOgrDriverName == QLatin1String( "SQLite" ) && name.compare( QLatin1String( "ogc_fid" ), Qt::CaseInsensitive ) == 0 )
596-
{
597-
int i;
598-
for ( i = 0; i < 10; i++ )
599-
{
600-
name = QStringLiteral( "ogc_fid%1" ).arg( i );
550+
case QVariant::Bool:
551+
ogrType = OFTInteger;
552+
ogrWidth = 1;
553+
ogrPrecision = 0;
554+
break;
601555

602-
int j;
603-
for ( j = 0; j < fields.size() && name.compare( fields.at( j ).name(), Qt::CaseInsensitive ) != 0; j++ )
604-
;
556+
case QVariant::Double:
557+
ogrType = OFTReal;
558+
break;
559+
560+
case QVariant::Date:
561+
ogrType = OFTDate;
562+
break;
605563

606-
if ( j == fields.size() )
564+
case QVariant::Time:
565+
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
566+
{
567+
ogrType = OFTString;
568+
ogrWidth = 12; // %02d:%02d:%06.3f
569+
}
570+
else
571+
{
572+
ogrType = OFTTime;
573+
}
607574
break;
575+
576+
case QVariant::DateTime:
577+
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
578+
{
579+
ogrType = OFTString;
580+
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
581+
}
582+
else
583+
{
584+
ogrType = OFTDateTime;
585+
}
586+
break;
587+
588+
default:
589+
//assert(0 && "invalid variant type!");
590+
mErrorMessage = QObject::tr( "Unsupported type for field %1" )
591+
.arg( attrField.name() );
592+
mError = ErrAttributeTypeUnsupported;
593+
return;
608594
}
609595

610-
if ( i == 10 )
596+
if ( mOgrDriverName == QLatin1String( "SQLite" ) && name.compare( QLatin1String( "ogc_fid" ), Qt::CaseInsensitive ) == 0 )
611597
{
612-
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
613-
mError = ErrAttributeCreationFailed;
614-
return;
615-
}
598+
int i;
599+
for ( i = 0; i < 10; i++ )
600+
{
601+
name = QStringLiteral( "ogc_fid%1" ).arg( i );
616602

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

620-
// create field definition
621-
gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType ) );
622-
if ( ogrWidth > 0 )
623-
{
624-
OGR_Fld_SetWidth( fld.get(), ogrWidth );
625-
}
607+
if ( j == fields.size() )
608+
break;
609+
}
626610

627-
if ( ogrPrecision >= 0 )
628-
{
629-
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
630-
}
611+
if ( i == 10 )
612+
{
613+
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
614+
mError = ErrAttributeCreationFailed;
615+
return;
616+
}
631617

632-
switch ( attrField.type() )
633-
{
634-
case QVariant::Bool:
635-
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
636-
break;
637-
default:
638-
break;
639-
}
618+
QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
619+
}
640620

641-
// create the field
642-
QgsDebugMsg( "creating field " + attrField.name() +
643-
" type " + QString( QVariant::typeToName( attrField.type() ) ) +
644-
" width " + QString::number( ogrWidth ) +
645-
" precision " + QString::number( ogrPrecision ) );
646-
if ( OGR_L_CreateField( mLayer, fld.get(), true ) != OGRERR_NONE )
647-
{
648-
QgsDebugMsg( "error creating field " + attrField.name() );
649-
mErrorMessage = QObject::tr( "Creation of field %1 failed (OGR error: %2)" )
650-
.arg( attrField.name(),
651-
QString::fromUtf8( CPLGetLastErrorMsg() ) );
652-
mError = ErrAttributeCreationFailed;
653-
return;
654-
}
621+
// create field definition
622+
gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType ) );
623+
if ( ogrWidth > 0 )
624+
{
625+
OGR_Fld_SetWidth( fld.get(), ogrWidth );
626+
}
655627

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

663-
if ( ogrIdx < 0 )
633+
switch ( attrField.type() )
634+
{
635+
case QVariant::Bool:
636+
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
637+
break;
638+
default:
639+
break;
640+
}
641+
642+
// create the field
643+
QgsDebugMsg( "creating field " + attrField.name() +
644+
" type " + QString( QVariant::typeToName( attrField.type() ) ) +
645+
" width " + QString::number( ogrWidth ) +
646+
" precision " + QString::number( ogrPrecision ) );
647+
if ( OGR_L_CreateField( mLayer, fld.get(), true ) != OGRERR_NONE )
664648
{
665649
QgsDebugMsg( "error creating field " + attrField.name() );
666-
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
650+
mErrorMessage = QObject::tr( "Creation of field %1 failed (OGR error: %2)" )
667651
.arg( attrField.name(),
668652
QString::fromUtf8( CPLGetLastErrorMsg() ) );
669653
mError = ErrAttributeCreationFailed;
670654
return;
671655
}
672-
}
673656

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

678-
if ( action == AppendToLayerNoNewFields )
664+
if ( ogrIdx < 0 )
665+
{
666+
QgsDebugMsg( "error creating field " + attrField.name() );
667+
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
668+
.arg( attrField.name(),
669+
QString::fromUtf8( CPLGetLastErrorMsg() ) );
670+
mError = ErrAttributeCreationFailed;
671+
return;
672+
}
673+
}
674+
675+
existingIdxs.insert( ogrIdx );
676+
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
677+
}
678+
}
679+
else if ( action == AppendToLayerNoNewFields )
679680
{
680681
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
681682
{

0 commit comments

Comments
 (0)