Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix fid regenerate on GPKG vector layer exported
Fixes #32927

(cherry picked from commit f9f00d3)
  • Loading branch information
elpaso authored and nyalldawson committed Nov 21, 2019
1 parent 074afdf commit 55ea8e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerexporter.cpp
Expand Up @@ -118,7 +118,7 @@ QgsVectorLayerExporter::QgsVectorLayerExporter( const QString &uri,
if ( sinkFlags.testFlag( QgsFeatureSink::SinkFlag::RegeneratePrimaryKey ) && path.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
{
QString fidName = options.value( QStringLiteral( "FID" ), QStringLiteral( "FID" ) ).toString();
int fidIdx = vectorProvider->fields().lookupField( fidName );
int fidIdx = fields.lookupField( fidName );
if ( fidIdx != -1 )
{
mOldToNewAttrIdx.remove( fidIdx );
Expand Down
35 changes: 35 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Expand Up @@ -1158,6 +1158,41 @@ def testRegenerateFid(self):
fids = set([f['fid'] for f in lyr.getFeatures()])
self.assertEqual(len(fids), 4)

def testExportWithoutFids(self):
""" Test export with a feature without fid, regression GH #32927
This test case is related to testRegenerateFid
"""

fields = QgsFields()
fields.append(QgsField('one', QVariant.Int))
fields.append(QgsField('two', QVariant.Int))
tmpfile = os.path.join(self.basetestpath, 'testExportWithoutFids.gpkg')
options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'output'
exporter = QgsVectorLayerExporter(tmpfile, "ogr", fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326), False, options, QgsFeatureSink.RegeneratePrimaryKey)
self.assertFalse(exporter.errorCode(),
'unexpected export error {}: {}'.format(exporter.errorCode(), exporter.errorMessage()))

feat = QgsFeature(fields)

feat['one'] = 100
feat['two'] = 200
feat.setGeometry(QgsGeometry.fromWkt('point(4 45)'))
exporter.addFeature(feat)

del exporter
# make sure layers exist
lyr = QgsVectorLayer('{}|layername=output'.format(tmpfile), "lyr1", "ogr")
self.assertTrue(lyr.isValid())
self.assertEqual(lyr.crs().authid(), 'EPSG:4326')
self.assertEqual(lyr.wkbType(), QgsWkbTypes.Point)
feat_out = next(lyr.getFeatures())
self.assertEqual(feat_out.attribute('two'), 200)
self.assertEqual(feat_out.attribute('one'), 100)

def testTransaction(self):

tmpfile = os.path.join(self.basetestpath, 'testTransaction.gpkg')
Expand Down

0 comments on commit 55ea8e7

Please sign in to comment.