Skip to content
Permalink
Browse files

[OGR provider] Make createEmptyLayer() work properly on GeoPackage wh…

…en creating/overwrite a new database (fixes #20040)
  • Loading branch information
rouault authored and nyalldawson committed Oct 6, 2018
1 parent 1aa9485 commit bd6c1115bbef29f06ca02840ac7ecadc2a8ce8e2
Showing with 49 additions and 20 deletions.
  1. +3 −1 src/providers/ogr/qgsogrprovider.cpp
  2. +46 −19 tests/src/python/test_provider_ogr_gpkg.py
@@ -299,11 +299,13 @@ QgsVectorLayerExporter::ExportError QgsOgrProvider::createEmptyLayer( const QStr
}
}
QString newLayerName( layerName );
std::unique_ptr< QgsVectorFileWriter > writer = qgis::make_unique< QgsVectorFileWriter >(
uri, encoding, fields, wkbType,
srs, driverName, dsOptions, layerOptions, nullptr,
QgsVectorFileWriter::NoSymbology, nullptr,
layerName, action );
layerName, action, &newLayerName );
layerName = newLayerName;
QgsVectorFileWriter::WriterError error = writer->hasError();
if ( error )
@@ -466,27 +466,24 @@ def testSimulatedDBManagerImport(self):
uri = 'point?field=f1:int'
uri += '&field=f2:double(6,4)'
uri += '&field=f3:string(20)'
lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(lyr.isValid())
f = QgsFeature(lyr.fields())
mem_lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(mem_lyr.isValid())
f = QgsFeature(mem_lyr.fields())
f['f1'] = 1
f['f2'] = 123.456
f['f3'] = '12345678.90123456789'
f2 = QgsFeature(lyr.fields())
f2 = QgsFeature(mem_lyr.fields())
f2['f1'] = 2
lyr.dataProvider().addFeatures([f, f2])
mem_lyr.dataProvider().addFeatures([f, f2])

# Test creating new DB
tmpfile = os.path.join(self.basetestpath, 'testSimulatedDBManagerImport.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
ds = None
options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'my_out_table'
err = QgsVectorLayerExporter.exportLayer(lyr, tmpfile, "ogr", lyr.crs(), False, options)
err = QgsVectorLayerExporter.exportLayer(mem_lyr, tmpfile, "ogr", mem_lyr.crs(), False, options)
self.assertEqual(err[0], QgsVectorLayerExporter.NoError,
'unexpected import error {0}'.format(err))
lyr = QgsVectorLayer(tmpfile + "|layername=my_out_table", "y", "ogr")
lyr = QgsVectorLayer(tmpfile, "y", "ogr")
self.assertTrue(lyr.isValid())
features = lyr.getFeatures()
f = next(features)
@@ -496,26 +493,56 @@ def testSimulatedDBManagerImport(self):
f = next(features)
self.assertEqual(f['f1'], 2)
features = None
del lyr

# Test updating existing DB, by adding a new layer
mem_lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(mem_lyr.isValid())
f = QgsFeature(mem_lyr.fields())
f['f1'] = 1
f['f2'] = 2
mem_lyr.dataProvider().addFeatures([f])

options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'my_out_table'
err = QgsVectorLayerExporter.exportLayer(mem_lyr, tmpfile, "ogr", mem_lyr.crs(), False, options)
self.assertEqual(err[0], QgsVectorLayerExporter.NoError,
'unexpected import error {0}'.format(err))
lyr = QgsVectorLayer(tmpfile + "|layername=my_out_table", "y", "ogr")
self.assertTrue(lyr.isValid())
features = lyr.getFeatures()
f = next(features)
self.assertEqual(f['f1'], 1)
self.assertEqual(f['f2'], 2)
features = None
del lyr

# Test overwriting without overwrite option
err = QgsVectorLayerExporter.exportLayer(lyr, tmpfile, "ogr", lyr.crs(), False, options)
err = QgsVectorLayerExporter.exportLayer(mem_lyr, tmpfile, "ogr", mem_lyr.crs(), False, options)
self.assertEqual(err[0], QgsVectorLayerExporter.ErrCreateDataSource)

# Test overwriting
lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(lyr.isValid())
f = QgsFeature(lyr.fields())
# Test overwriting, without specifying a layer name
mem_lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(mem_lyr.isValid())
f = QgsFeature(mem_lyr.fields())
f['f1'] = 3
lyr.dataProvider().addFeatures([f])
f['f2'] = 4
mem_lyr.dataProvider().addFeatures([f])

options = {}
options['driverName'] = 'GPKG'
options['overwrite'] = True
err = QgsVectorLayerExporter.exportLayer(lyr, tmpfile, "ogr", lyr.crs(), False, options)
err = QgsVectorLayerExporter.exportLayer(mem_lyr, tmpfile, "ogr", mem_lyr.crs(), False, options)
self.assertEqual(err[0], QgsVectorLayerExporter.NoError,
'unexpected import error {0}'.format(err))
lyr = QgsVectorLayer(tmpfile + "|layername=my_out_table", "y", "ogr")
lyr = QgsVectorLayer(tmpfile, "y", "ogr")
self.assertTrue(lyr.isValid())
features = lyr.getFeatures()
f = next(features)
self.assertEqual(f['f1'], 3)
self.assertEqual(f['f2'], 4)
features = None

def testExportLayerToExistingDatabase(self):

0 comments on commit bd6c111

Please sign in to comment.
You can’t perform that action at this time.