@@ -492,190 +492,191 @@ void QgsVectorFileWriter::init( QString vectorFileName,
492
492
493
493
mFieldValueConverter = fieldValueConverter;
494
494
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 )
499
498
{
500
- QgsField attrField = fields.at ( fldIdx );
501
-
502
- if ( fieldValueConverter )
499
+ for ( int fldIdx = 0 ; fldIdx < fields.count (); ++fldIdx )
503
500
{
504
- attrField = fieldValueConverter->fieldDefinition ( fields.at ( fldIdx ) );
505
- }
501
+ QgsField attrField = fields.at ( fldIdx );
506
502
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 )
512
504
{
513
- mAttrIdxToOgrIdx .insert ( fldIdx, ogrIdx );
514
- continue ;
505
+ attrField = fieldValueConverter->fieldDefinition ( fields.at ( fldIdx ) );
515
506
}
516
- }
517
507
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 )
527
510
{
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
+ }
536
517
}
537
- case QVariant::String:
538
- ogrType = OFTString;
539
- if ( ogrWidth <= 0 || ogrWidth > 255 )
540
- ogrWidth = 255 ;
541
- break ;
542
518
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;
558
524
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:
570
528
{
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 ;
572
537
}
573
- break ;
574
-
575
- case QVariant::DateTime:
576
- if ( mOgrDriverName == QLatin1String ( " ESRI Shapefile" ) )
577
- {
538
+ case QVariant::String:
578
539
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 ;
586
543
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 ;
594
549
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 ;
601
555
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 ;
605
563
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
+ }
607
574
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 ;
608
594
}
609
595
610
- if ( i == 10 )
596
+ if ( mOgrDriverName == QLatin1String ( " SQLite " ) && name. compare ( QLatin1String ( " ogc_fid " ), Qt::CaseInsensitive ) == 0 )
611
597
{
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 );
616
602
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
+ ;
619
606
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
+ }
626
610
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
+ }
631
617
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
+ }
640
620
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
+ }
655
627
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
+ }
662
632
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 )
664
648
{
665
649
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)" )
667
651
.arg ( attrField.name (),
668
652
QString::fromUtf8 ( CPLGetLastErrorMsg () ) );
669
653
mError = ErrAttributeCreationFailed;
670
654
return ;
671
655
}
672
- }
673
656
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 ;
677
663
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 )
679
680
{
680
681
for ( int fldIdx = 0 ; fldIdx < fields.count (); ++fldIdx )
681
682
{
0 commit comments