Skip to content
Permalink
Browse files

[ogr] Fix cannot create layers for geopackage datasets when

the database does not yet exist
  • Loading branch information
nyalldawson committed Nov 22, 2017
1 parent 8775aff commit abcdd48580e365d40535f5396ba5096c57a459c9
Showing with 42 additions and 7 deletions.
  1. +3 −3 src/providers/ogr/qgsogrprovider.cpp
  2. +39 −4 tests/src/python/test_provider_ogr_gpkg.py
@@ -282,14 +282,14 @@ QgsVectorLayerExporter::ExportError QgsOgrProvider::createEmptyLayer( const QStr
}
}
}
action = QgsVectorFileWriter::CreateOrOverwriteLayer;
if ( QFileInfo::exists( uri ) )
action = QgsVectorFileWriter::CreateOrOverwriteLayer;
}
}
if ( !overwrite && !update )
{
QFileInfo fi( uri );
if ( fi.exists() )
if ( QFileInfo::exists( uri ) )
{
if ( errorMessage )
*errorMessage += QObject::tr( "Unable to create the datasource. %1 exists and overwrite flag is false." )
@@ -20,10 +20,19 @@

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.core import (QgsFeature,
QgsCoordinateReferenceSystem,
QgsFields,
QgsField,
QgsFieldConstraints,
QgsGeometry,
QgsRectangle,
QgsSettings,
QgsVectorLayer,
QgsVectorLayerExporter,
QgsPointXY,
QgsWkbTypes)
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.testing import start_app, unittest


@@ -469,6 +478,32 @@ def testSimulatedDBManagerImport(self):
self.assertEqual(f['f1'], 3)
features = None

def testExportLayerToExistingDatabase(self):
fields = QgsFields()
fields.append(QgsField('f1', QVariant.Int))
tmpfile = os.path.join(self.basetestpath, 'testCreateNewGeopackage.gpkg')
options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'table1'
exporter = QgsVectorLayerExporter(tmpfile, "ogr", fields, QgsWkbTypes.Polygon, QgsCoordinateReferenceSystem(3111), False, options)
self.assertFalse(exporter.errorCode(),
'unexpected export error {}: {}'.format(exporter.errorCode(), exporter.errorMessage()))
options['layerName'] = 'table2'
exporter = QgsVectorLayerExporter(tmpfile, "ogr", fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(3113), False, options)
self.assertFalse(exporter.errorCode(),
'unexpected export error {} : {}'.format(exporter.errorCode(), exporter.errorMessage()))
del exporter
# make sure layers exist
lyr = QgsVectorLayer('{}|layername=table1'.format(tmpfile), "lyr1", "ogr")
self.assertTrue(lyr.isValid())
self.assertEqual(lyr.crs().authid(), 'EPSG:3111')
self.assertEqual(lyr.wkbType(), QgsWkbTypes.Polygon)
lyr2 = QgsVectorLayer('{}|layername=table2'.format(tmpfile), "lyr2", "ogr")
self.assertTrue(lyr2.isValid())
self.assertEqual(lyr2.crs().authid(), 'EPSG:3113')
self.assertEqual(lyr2.wkbType(), QgsWkbTypes.Point)

def testGeopackageTwoLayerEdition(self):
''' test https://issues.qgis.org/issues/17034 '''
tmpfile = os.path.join(self.basetestpath, 'testGeopackageTwoLayerEdition.gpkg')

0 comments on commit abcdd48

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