Skip to content

Commit

Permalink
[ogr] Fix cannot create layers for geopackage datasets when
Browse files Browse the repository at this point in the history
the database does not yet exist
  • Loading branch information
nyalldawson committed Nov 23, 2017
1 parent 8775aff commit abcdd48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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." )
Expand Down
43 changes: 39 additions & 4 deletions tests/src/python/test_provider_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit abcdd48

Please sign in to comment.