Skip to content

Commit

Permalink
Fix drag and drop to GDB results in columns shifted by one
Browse files Browse the repository at this point in the history
Fixes #51435
  • Loading branch information
nyalldawson committed Mar 5, 2023
1 parent 1a18d9b commit 78199dd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -300,6 +300,17 @@ Qgis::VectorExportResult QgsOgrProvider::createEmptyLayer( const QString &uri,
}
}

QgsFields cleanedFields = fields;

if ( driverName == QLatin1String( "OpenFileGDB" ) )
{
const int objectIdIndex = cleanedFields.lookupField( QStringLiteral( "OBJECTID" ) );
if ( objectIdIndex >= 0 )
{
cleanedFields.remove( objectIdIndex );
}
}

QString newLayerName( layerName );

QgsVectorFileWriter::SaveVectorOptions saveOptions;
Expand All @@ -310,7 +321,7 @@ Qgis::VectorExportResult QgsOgrProvider::createEmptyLayer( const QString &uri,
saveOptions.layerOptions = layerOptions;
saveOptions.actionOnExistingFile = action;
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( uri, fields, wkbType, srs, QgsCoordinateTransformContext(), saveOptions, QgsFeatureSink::SinkFlags(), nullptr, &newLayerName ) );
std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( uri, cleanedFields, wkbType, srs, QgsCoordinateTransformContext(), saveOptions, QgsFeatureSink::SinkFlags(), nullptr, &newLayerName ) );
layerName = newLayerName;

QgsVectorFileWriter::WriterError error = writer->hasError();
Expand Down Expand Up @@ -341,7 +352,7 @@ Qgis::VectorExportResult QgsOgrProvider::createEmptyLayer( const QString &uri,
const QString ogrFidColumnName { OGR_L_GetFIDColumn( hLayer ) };
firstFieldIsFid = !( EQUAL( OGR_L_GetFIDColumn( hLayer ), "" ) ) &&
OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), ogrFidColumnName.toUtf8() ) < 0 &&
fields.indexFromName( ogrFidColumnName.toUtf8() ) < 0;
cleanedFields.indexFromName( ogrFidColumnName.toUtf8() ) < 0;
// At this point we must check if there is a real FID field in the the fields argument,
// because in that case we don't want to shift all fields (see issue GH #34333)
// Check for unique values should be performed in client code.
Expand Down

0 comments on commit 78199dd

Please sign in to comment.