Skip to content

Commit 2f1dec9

Browse files
authored
Merge pull request #8362 from m-kuhn/codeReadability
Improve code readability
2 parents 3a74c87 + f432684 commit 2f1dec9

File tree

1 file changed

+171
-164
lines changed

1 file changed

+171
-164
lines changed

src/core/qgsvectorfilewriter.cpp

+171-164
Original file line numberDiff line numberDiff line change
@@ -492,199 +492,206 @@ 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+
switch ( action )
499496
{
500-
QgsField attrField = fields.at( fldIdx );
501-
502-
if ( fieldValueConverter )
503-
{
504-
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
505-
}
506-
507-
QString name( attrField.name() );
508-
if ( action == AppendToLayerAddFields )
497+
case CreateOrOverwriteFile:
498+
case CreateOrOverwriteLayer:
499+
case AppendToLayerAddFields:
509500
{
510-
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
511-
if ( ogrIdx >= 0 )
501+
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
512502
{
513-
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
514-
continue;
515-
}
516-
}
517-
518-
OGRFieldType ogrType = OFTString; //default to string
519-
int ogrWidth = attrField.length();
520-
int ogrPrecision = attrField.precision();
521-
if ( ogrPrecision > 0 )
522-
++ogrWidth;
503+
QgsField attrField = fields.at( fldIdx );
523504

524-
switch ( attrField.type() )
525-
{
526-
case QVariant::LongLong:
527-
{
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;
536-
}
537-
case QVariant::String:
538-
ogrType = OFTString;
539-
if ( ogrWidth <= 0 || ogrWidth > 255 )
540-
ogrWidth = 255;
541-
break;
505+
if ( fieldValueConverter )
506+
{
507+
attrField = fieldValueConverter->fieldDefinition( fields.at( fldIdx ) );
508+
}
542509

543-
case QVariant::Int:
544-
ogrType = OFTInteger;
545-
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
546-
ogrPrecision = 0;
547-
break;
510+
QString name( attrField.name() );
511+
if ( action == AppendToLayerAddFields )
512+
{
513+
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
514+
if ( ogrIdx >= 0 )
515+
{
516+
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
517+
continue;
518+
}
519+
}
548520

549-
case QVariant::Bool:
550-
ogrType = OFTInteger;
551-
ogrWidth = 1;
552-
ogrPrecision = 0;
553-
break;
521+
OGRFieldType ogrType = OFTString; //default to string
522+
int ogrWidth = attrField.length();
523+
int ogrPrecision = attrField.precision();
524+
if ( ogrPrecision > 0 )
525+
++ogrWidth;
554526

555-
case QVariant::Double:
556-
ogrType = OFTReal;
557-
break;
527+
switch ( attrField.type() )
528+
{
529+
case QVariant::LongLong:
530+
{
531+
const char *pszDataTypes = GDALGetMetadataItem( poDriver, GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
532+
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
533+
ogrType = OFTInteger64;
534+
else
535+
ogrType = OFTReal;
536+
ogrWidth = ogrWidth > 0 && ogrWidth <= 20 ? ogrWidth : 20;
537+
ogrPrecision = 0;
538+
break;
539+
}
540+
case QVariant::String:
541+
ogrType = OFTString;
542+
if ( ogrWidth <= 0 || ogrWidth > 255 )
543+
ogrWidth = 255;
544+
break;
545+
546+
case QVariant::Int:
547+
ogrType = OFTInteger;
548+
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
549+
ogrPrecision = 0;
550+
break;
551+
552+
case QVariant::Bool:
553+
ogrType = OFTInteger;
554+
ogrWidth = 1;
555+
ogrPrecision = 0;
556+
break;
557+
558+
case QVariant::Double:
559+
ogrType = OFTReal;
560+
break;
561+
562+
case QVariant::Date:
563+
ogrType = OFTDate;
564+
break;
565+
566+
case QVariant::Time:
567+
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
568+
{
569+
ogrType = OFTString;
570+
ogrWidth = 12; // %02d:%02d:%06.3f
571+
}
572+
else
573+
{
574+
ogrType = OFTTime;
575+
}
576+
break;
558577

559-
case QVariant::Date:
560-
ogrType = OFTDate;
561-
break;
578+
case QVariant::DateTime:
579+
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
580+
{
581+
ogrType = OFTString;
582+
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
583+
}
584+
else
585+
{
586+
ogrType = OFTDateTime;
587+
}
588+
break;
589+
590+
default:
591+
//assert(0 && "invalid variant type!");
592+
mErrorMessage = QObject::tr( "Unsupported type for field %1" )
593+
.arg( attrField.name() );
594+
mError = ErrAttributeTypeUnsupported;
595+
return;
596+
}
562597

563-
case QVariant::Time:
564-
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
598+
if ( mOgrDriverName == QLatin1String( "SQLite" ) && name.compare( QLatin1String( "ogc_fid" ), Qt::CaseInsensitive ) == 0 )
565599
{
566-
ogrType = OFTString;
567-
ogrWidth = 12; // %02d:%02d:%06.3f
600+
int i;
601+
for ( i = 0; i < 10; i++ )
602+
{
603+
name = QStringLiteral( "ogc_fid%1" ).arg( i );
604+
605+
int j;
606+
for ( j = 0; j < fields.size() && name.compare( fields.at( j ).name(), Qt::CaseInsensitive ) != 0; j++ )
607+
;
608+
609+
if ( j == fields.size() )
610+
break;
611+
}
612+
613+
if ( i == 10 )
614+
{
615+
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
616+
mError = ErrAttributeCreationFailed;
617+
return;
618+
}
619+
620+
QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
568621
}
569-
else
622+
623+
// create field definition
624+
gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType ) );
625+
if ( ogrWidth > 0 )
570626
{
571-
ogrType = OFTTime;
627+
OGR_Fld_SetWidth( fld.get(), ogrWidth );
572628
}
573-
break;
574629

575-
case QVariant::DateTime:
576-
if ( mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
630+
if ( ogrPrecision >= 0 )
577631
{
578-
ogrType = OFTString;
579-
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
632+
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
580633
}
581-
else
634+
635+
switch ( attrField.type() )
582636
{
583-
ogrType = OFTDateTime;
637+
case QVariant::Bool:
638+
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
639+
break;
640+
default:
641+
break;
584642
}
585-
break;
586-
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-
}
594643

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 );
644+
// create the field
645+
QgsDebugMsg( "creating field " + attrField.name() +
646+
" type " + QString( QVariant::typeToName( attrField.type() ) ) +
647+
" width " + QString::number( ogrWidth ) +
648+
" precision " + QString::number( ogrPrecision ) );
649+
if ( OGR_L_CreateField( mLayer, fld.get(), true ) != OGRERR_NONE )
650+
{
651+
QgsDebugMsg( "error creating field " + attrField.name() );
652+
mErrorMessage = QObject::tr( "Creation of field %1 failed (OGR error: %2)" )
653+
.arg( attrField.name(),
654+
QString::fromUtf8( CPLGetLastErrorMsg() ) );
655+
mError = ErrAttributeCreationFailed;
656+
return;
657+
}
601658

602-
int j;
603-
for ( j = 0; j < fields.size() && name.compare( fields.at( j ).name(), Qt::CaseInsensitive ) != 0; j++ )
604-
;
659+
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
660+
QgsDebugMsg( QStringLiteral( "returned field index for %1: %2" ).arg( name ).arg( ogrIdx ) );
661+
if ( ogrIdx < 0 || existingIdxs.contains( ogrIdx ) )
662+
{
663+
// GDAL 1.7 not just truncates, but launders more aggressivly.
664+
ogrIdx = OGR_FD_GetFieldCount( defn ) - 1;
605665

606-
if ( j == fields.size() )
607-
break;
608-
}
666+
if ( ogrIdx < 0 )
667+
{
668+
QgsDebugMsg( "error creating field " + attrField.name() );
669+
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
670+
.arg( attrField.name(),
671+
QString::fromUtf8( CPLGetLastErrorMsg() ) );
672+
mError = ErrAttributeCreationFailed;
673+
return;
674+
}
675+
}
609676

610-
if ( i == 10 )
611-
{
612-
mErrorMessage = QObject::tr( "No available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
613-
mError = ErrAttributeCreationFailed;
614-
return;
677+
existingIdxs.insert( ogrIdx );
678+
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
615679
}
616-
617-
QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
618-
}
619-
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-
}
626-
627-
if ( ogrPrecision >= 0 )
628-
{
629-
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
630680
}
681+
break;
631682

632-
switch ( attrField.type() )
683+
case AppendToLayerNoNewFields:
633684
{
634-
case QVariant::Bool:
635-
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
636-
break;
637-
default:
638-
break;
639-
}
640-
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-
}
655-
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;
662-
663-
if ( ogrIdx < 0 )
685+
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
664686
{
665-
QgsDebugMsg( "error creating field " + attrField.name() );
666-
mErrorMessage = QObject::tr( "Created field %1 not found (OGR error: %2)" )
667-
.arg( attrField.name(),
668-
QString::fromUtf8( CPLGetLastErrorMsg() ) );
669-
mError = ErrAttributeCreationFailed;
670-
return;
687+
QgsField attrField = fields.at( fldIdx );
688+
QString name( attrField.name() );
689+
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
690+
if ( ogrIdx >= 0 )
691+
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
671692
}
672693
}
673-
674-
existingIdxs.insert( ogrIdx );
675-
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
676-
}
677-
678-
if ( action == AppendToLayerNoNewFields )
679-
{
680-
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
681-
{
682-
QgsField attrField = fields.at( fldIdx );
683-
QString name( attrField.name() );
684-
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
685-
if ( ogrIdx >= 0 )
686-
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
687-
}
694+
break;
688695
}
689696

690697
QgsDebugMsg( QStringLiteral( "Done creating fields" ) );

0 commit comments

Comments
 (0)