Skip to content

Commit

Permalink
[bugfix] Editing a GPKG layer added by d&d leads
Browse files Browse the repository at this point in the history
... to error and data corruption

Fixes #16935 for GPKG (OGR), still  needs to check spatialite

Thanks to Matthias Kuhn for pointing me into the right direction
  • Loading branch information
elpaso committed Nov 20, 2017
1 parent c9b8ddc commit 73d6765
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,18 @@ void QgsOgrProvider::loadFields()
fdef.GetFieldIndex( fidColumn ) < 0;
if ( mFirstFieldIsFid )
{
QgsField fidField(
fidColumn,
QVariant::LongLong,
QStringLiteral( "Integer64" )
);
// Set constraints for feature id
QgsFieldConstraints constraints = fidField.constraints();
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
fidField.setConstraints( constraints );
mAttributeFields.append(
QgsField(
fidColumn,
QVariant::LongLong,
QStringLiteral( "Integer64" )
)
fidField
);
}

Expand Down
43 changes: 38 additions & 5 deletions tests/src/python/test_provider_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

import os
import tempfile
import shutil
import sys
import tempfile
import time
from osgeo import gdal, ogr

from qgis.core import QgsVectorLayer, QgsVectorLayerExporter, QgsFeature, QgsGeometry, QgsRectangle, QgsSettings
import qgis # NOQA
from osgeo import gdal, ogr
from qgis.core import (QgsFeature, QgsFieldConstraints, QgsGeometry,
QgsRectangle, QgsSettings, QgsVectorLayer,
QgsVectorLayerExporter, QgsPointXY)
from qgis.PyQt.QtCore import QCoreApplication
from qgis.testing import start_app, unittest

Expand Down Expand Up @@ -632,6 +633,38 @@ def testGeopackageLargeFID(self):
self.assertTrue(vl.deleteFeature(1234567890123))
self.assertTrue(vl.commitChanges())

def test_SplitFeature(self):
"""Test gpkg feature can be splitted"""
tmpfile = os.path.join(self.basetestpath, 'testGeopackageSplitFeatures.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('test', geom_type=ogr.wkbPolygon)
lyr.CreateField(ogr.FieldDefn('str_field', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 1,1 1,1 0,0 0))'))
lyr.CreateFeature(f)
f = None
ds = None

layer = QgsVectorLayer(u'{}'.format(tmpfile) + "|layername=" + "test", 'test', u'ogr')

# Check that pk field has unique constraint
fields = layer.fields()
pkfield = fields.at(0)
self.assertTrue(pkfield.constraints().constraints() & QgsFieldConstraints.ConstraintUnique)

self.assertTrue(layer.isValid())
self.assertTrue(layer.isSpatial())
self.assertEqual([f for f in layer.getFeatures()][0].geometry().asWkt(), 'Polygon ((0 0, 0 1, 1 1, 1 0, 0 0))')
layer.startEditing()
self.assertEqual(layer.splitFeatures([QgsPointXY(0.5, 0), QgsPointXY(0.5, 1)], 0), 0)
self.assertTrue(layer.commitChanges())
self.assertEqual(layer.featureCount(), 2)

layer = QgsVectorLayer(u'{}'.format(tmpfile) + "|layername=" + "test", 'test', u'ogr')
self.assertEqual(layer.featureCount(), 2)
self.assertEqual([f for f in layer.getFeatures()][0].geometry().asWkt(), 'Polygon ((0.5 0, 0.5 1, 1 1, 1 0, 0.5 0))')
self.assertEqual([f for f in layer.getFeatures()][1].geometry().asWkt(), 'Polygon ((0.5 1, 0.5 0, 0 0, 0 1, 0.5 1))')


if __name__ == '__main__':
unittest.main()

0 comments on commit 73d6765

Please sign in to comment.